8
0

position.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  8. import Element from '/ckeditor5/engine/model/element.js';
  9. import Text from '/ckeditor5/engine/model/text.js';
  10. import Position from '/ckeditor5/engine/model/position.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  13. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  14. testUtils.createSinonSandbox();
  15. describe( 'position', () => {
  16. let doc, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r;
  17. // root
  18. // |- p Before: [ 0 ] After: [ 1 ]
  19. // |- ul Before: [ 1 ] After: [ 2 ]
  20. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  21. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  22. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  23. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  24. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  25. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  26. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  27. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  28. before( () => {
  29. doc = new Document();
  30. root = doc.createRoot();
  31. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  32. li1 = new Element( 'li', [], 'foz' );
  33. f = li1.getChild( 0 );
  34. o = li1.getChild( 1 );
  35. z = li1.getChild( 2 );
  36. li2 = new Element( 'li', [], 'bar' );
  37. b = li2.getChild( 0 );
  38. a = li2.getChild( 1 );
  39. r = li2.getChild( 2 );
  40. ul = new Element( 'ul', [], [ li1, li2 ] );
  41. p = new Element( 'p' );
  42. root.insertChildren( 0, [ p, ul ] );
  43. } );
  44. describe( 'constructor', () => {
  45. it( 'should create a position with path and document', () => {
  46. let position = new Position( root, [ 0 ] );
  47. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  48. expect( position ).to.have.property( 'root' ).that.equals( root );
  49. } );
  50. it( 'should accept DocumentFragment as a root', () => {
  51. const frag = new DocumentFragment();
  52. const pos = new Position( frag, [ 0 ] );
  53. expect( pos ).to.have.property( 'root', frag );
  54. } );
  55. it( 'should accept detached Element as a root', () => {
  56. const el = new Element( 'p' );
  57. const pos = new Position( el, [ 0 ] );
  58. expect( pos ).to.have.property( 'root', el );
  59. expect( pos.path ).to.deep.equal( [ 0 ] );
  60. } );
  61. it( 'should normalize attached Element as a root', () => {
  62. const pos = new Position( li1, [ 0, 2 ] );
  63. expect( pos ).to.have.property( 'root', root );
  64. expect( pos.isEqual( Position.createAt( li1, 0, 2 ) ) );
  65. } );
  66. it( 'should normalize Element from a detached branch as a root', () => {
  67. const rootEl = new Element( 'p', null, [ new Element( 'a' ) ] );
  68. const elA = rootEl.getChild( 0 );
  69. const pos = new Position( elA, [ 0 ] );
  70. expect( pos ).to.have.property( 'root', rootEl );
  71. expect( pos.isEqual( Position.createAt( elA, 0 ) ) );
  72. } );
  73. it( 'should throw error if given path is incorrect', () => {
  74. expect( () => {
  75. new Position( root, {} );
  76. } ).to.throw( CKEditorError, /position-path-incorrect/ );
  77. expect( () => {
  78. new Position( root, [] );
  79. } ).to.throw( CKEditorError, /position-path-incorrect/ );
  80. } );
  81. it( 'should throw error if given root is invalid', () => {
  82. expect( () => {
  83. new Position( new Text( 'a' ) );
  84. } ).to.throw( CKEditorError, /position-root-invalid/ );
  85. expect( () => {
  86. new Position();
  87. } ).to.throw( CKEditorError, /position-root-invalid/ );
  88. } );
  89. } );
  90. describe( 'createFromParentAndOffset', () => {
  91. it( 'should create positions form node and offset', () => {
  92. expect( Position.createFromParentAndOffset( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  93. expect( Position.createFromParentAndOffset( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  94. expect( Position.createFromParentAndOffset( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  95. expect( Position.createFromParentAndOffset( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  96. expect( Position.createFromParentAndOffset( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  97. expect( Position.createFromParentAndOffset( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  98. expect( Position.createFromParentAndOffset( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  99. expect( Position.createFromParentAndOffset( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  100. expect( Position.createFromParentAndOffset( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  101. expect( Position.createFromParentAndOffset( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  102. expect( Position.createFromParentAndOffset( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  103. } );
  104. it( 'throws when parent is not an element', () => {
  105. expect( () => {
  106. Position.createFromParentAndOffset( b, 0 );
  107. } ).to.throw( CKEditorError, /^position-parent-incorrect/ );
  108. } );
  109. it( 'works with a doc frag', () => {
  110. const frag = new DocumentFragment();
  111. expect( Position.createFromParentAndOffset( frag, 0 ) ).to.have.property( 'root', frag );
  112. } );
  113. } );
  114. describe( 'createAt', () => {
  115. it( 'should create positions from positions', () => {
  116. const spy = testUtils.sinon.spy( Position, 'createFromPosition' );
  117. expect( Position.createAt( Position.createAt( ul ) ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  118. expect( spy.calledOnce ).to.be.true;
  119. } );
  120. it( 'should create positions from node and offset', () => {
  121. expect( Position.createAt( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  122. expect( Position.createAt( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  123. expect( Position.createAt( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  124. } );
  125. it( 'should create positions from node and flag', () => {
  126. expect( Position.createAt( root, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  127. expect( Position.createAt( p, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  128. expect( Position.createAt( a, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  129. expect( Position.createAt( p, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  130. expect( Position.createAt( a, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  131. expect( Position.createAt( ul, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  132. } );
  133. } );
  134. describe( 'createBefore', () => {
  135. it( 'should create positions before elements', () => {
  136. expect( Position.createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  137. expect( Position.createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  138. expect( Position.createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  139. expect( Position.createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  140. expect( Position.createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  141. expect( Position.createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  142. expect( Position.createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  143. expect( Position.createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  144. expect( Position.createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  145. expect( Position.createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  146. } );
  147. it( 'should throw error if one try to create positions before root', () => {
  148. expect( () => {
  149. Position.createBefore( root );
  150. } ).to.throw( CKEditorError, /position-before-root/ );
  151. } );
  152. } );
  153. describe( 'createAfter', () => {
  154. it( 'should create positions after elements', () => {
  155. expect( Position.createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  156. expect( Position.createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  157. expect( Position.createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  158. expect( Position.createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  159. expect( Position.createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  160. expect( Position.createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  161. expect( Position.createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  162. expect( Position.createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  163. expect( Position.createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  164. expect( Position.createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  165. } );
  166. it( 'should throw error if one try to make positions after root', () => {
  167. expect( () => {
  168. Position.createAfter( root );
  169. } ).to.throw( CKEditorError, /position-after-root/ );
  170. } );
  171. } );
  172. describe( 'createFromPosition', () => {
  173. it( 'should create a copy of given position', () => {
  174. let original = new Position( root, [ 1, 2, 3 ] );
  175. let position = Position.createFromPosition( original );
  176. expect( position ).to.be.instanceof( Position );
  177. expect( position.isEqual( original ) ).to.be.true;
  178. expect( position ).not.to.be.equal( original );
  179. } );
  180. } );
  181. it( 'should have parent', () => {
  182. expect( new Position( root, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( root );
  183. expect( new Position( root, [ 1 ] ) ).to.have.property( 'parent' ).that.equals( root );
  184. expect( new Position( root, [ 2 ] ) ).to.have.property( 'parent' ).that.equals( root );
  185. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( p );
  186. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  187. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  188. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  189. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  190. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  191. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  192. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  193. } );
  194. it( 'should have offset', () => {
  195. expect( new Position( root, [ 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  196. expect( new Position( root, [ 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  197. expect( new Position( root, [ 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  198. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  199. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  200. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  201. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  202. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  203. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  204. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  205. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'offset' ).that.equals( 3 );
  206. } );
  207. it( 'should be able to set offset', () => {
  208. let position = new Position( root, [ 1, 0, 2 ] );
  209. position.offset = 4;
  210. expect( position.offset ).to.equal( 4 );
  211. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  212. } );
  213. it( 'should have nodeBefore', () => {
  214. expect( new Position( root, [ 0 ] ).nodeBefore ).to.be.null;
  215. expect( new Position( root, [ 1 ] ).nodeBefore ).to.equal( p );
  216. expect( new Position( root, [ 2 ] ).nodeBefore ).to.equal( ul );
  217. expect( new Position( root, [ 0, 0 ] ).nodeBefore ).to.null;
  218. expect( new Position( root, [ 1, 0 ] ).nodeBefore ).to.be.null;
  219. expect( new Position( root, [ 1, 1 ] ).nodeBefore ).to.equal( li1 );
  220. expect( new Position( root, [ 1, 2 ] ).nodeBefore ).to.equal( li2 );
  221. expect( new Position( root, [ 1, 0, 0 ] ).nodeBefore ).to.be.null;
  222. expect( new Position( root, [ 1, 0, 1 ] ).nodeBefore.character ).to.equal( 'f' );
  223. expect( new Position( root, [ 1, 0, 2 ] ).nodeBefore.character ).to.equal( 'o' );
  224. expect( new Position( root, [ 1, 0, 3 ] ).nodeBefore.character ).to.equal( 'z' );
  225. } );
  226. it( 'should have nodeAfter', () => {
  227. expect( new Position( root, [ 0 ] ).nodeAfter ).to.equal( p );
  228. expect( new Position( root, [ 1 ] ).nodeAfter ).to.equal( ul );
  229. expect( new Position( root, [ 2 ] ).nodeAfter ).to.be.null;
  230. expect( new Position( root, [ 0, 0 ] ).nodeAfter ).to.be.null;
  231. expect( new Position( root, [ 1, 0 ] ).nodeAfter ).to.equal( li1 );
  232. expect( new Position( root, [ 1, 1 ] ).nodeAfter ).to.equal( li2 );
  233. expect( new Position( root, [ 1, 2 ] ).nodeAfter ).to.be.null;
  234. expect( new Position( root, [ 1, 0, 0 ] ).nodeAfter.character ).to.equal( 'f' );
  235. expect( new Position( root, [ 1, 0, 1 ] ).nodeAfter.character ).to.equal( 'o' );
  236. expect( new Position( root, [ 1, 0, 2 ] ).nodeAfter.character ).to.equal( 'z' );
  237. expect( new Position( root, [ 1, 0, 3 ] ).nodeAfter ).to.be.null;
  238. } );
  239. it( 'should have proper parent path', () => {
  240. let position = new Position( root, [ 1, 2, 3 ] );
  241. expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
  242. } );
  243. describe( 'isBefore', () => {
  244. it( 'should return true if given position has same root and is before this position', () => {
  245. let position = new Position( root, [ 1, 1, 2 ] );
  246. let beforePosition = new Position( root, [ 1, 0 ] );
  247. expect( position.isAfter( beforePosition ) ).to.be.true;
  248. } );
  249. it( 'should return false if given position has same root and is not before this position', () => {
  250. let position = new Position( root, [ 1, 1, 2 ] );
  251. let afterPosition = new Position( root, [ 1, 2 ] );
  252. expect( position.isAfter( afterPosition ) ).to.be.false;
  253. } );
  254. it( 'should return false if given position has different root', () => {
  255. let position = new Position( root, [ 1, 1, 2 ] );
  256. let differentPosition = new Position( otherRoot, [ 1, 0 ] );
  257. expect( position.isAfter( differentPosition ) ).to.be.false;
  258. } );
  259. } );
  260. describe( 'isEqual', () => {
  261. it( 'should return true if given position has same path and root', () => {
  262. let position = new Position( root, [ 1, 1, 2 ] );
  263. let samePosition = new Position( root, [ 1, 1, 2 ] );
  264. expect( position.isEqual( samePosition ) ).to.be.true;
  265. } );
  266. it( 'should return false if given position has different path', () => {
  267. let position = new Position( root, [ 1, 1, 1 ] );
  268. let differentPosition = new Position( root, [ 1, 2, 2 ] );
  269. expect( position.isEqual( differentPosition ) ).to.be.false;
  270. } );
  271. it( 'should return false if given position has different root', () => {
  272. let position = new Position( root, [ 1, 1, 1 ] );
  273. let differentPosition = new Position( otherRoot, [ 1, 1, 1 ] );
  274. expect( position.isEqual( differentPosition ) ).to.be.false;
  275. } );
  276. } );
  277. describe( 'isAfter', () => {
  278. it( 'should return true if given position has same root and is after this position', () => {
  279. let position = new Position( root, [ 1, 1, 2 ] );
  280. let afterPosition = new Position( root, [ 1, 2 ] );
  281. expect( position.isBefore( afterPosition ) ).to.be.true;
  282. } );
  283. it( 'should return false if given position has same root and is not after this position', () => {
  284. let position = new Position( root, [ 1, 1, 2 ] );
  285. let beforePosition = new Position( root, [ 1, 0 ] );
  286. expect( position.isBefore( beforePosition ) ).to.be.false;
  287. } );
  288. it( 'should return false if given position has different root', () => {
  289. let position = new Position( root, [ 1, 1, 2 ] );
  290. let differentPosition = new Position( otherRoot, [ 1, 2 ] );
  291. expect( position.isBefore( differentPosition ) ).to.be.false;
  292. } );
  293. } );
  294. describe( 'isTouching', () => {
  295. it( 'should return true if positions are same', () => {
  296. let position = new Position( root, [ 1, 1, 1 ] );
  297. let result = position.isTouching( new Position( root, [ 1, 1, 1 ] ) );
  298. expect( result ).to.be.true;
  299. } );
  300. it( 'should return true if given position is in next node and there are no whole nodes before it', () => {
  301. let positionA = new Position( root, [ 1 ] );
  302. let positionB = new Position( root, [ 1, 0, 0 ] );
  303. expect( positionA.isTouching( positionB ) ).to.be.true;
  304. expect( positionB.isTouching( positionA ) ).to.be.true;
  305. } );
  306. it( 'should return true if given position is in previous node and there are no whole nodes after it', () => {
  307. let positionA = new Position( root, [ 2 ] );
  308. let positionB = new Position( root, [ 1, 1, 3 ] );
  309. expect( positionA.isTouching( positionB ) ).to.be.true;
  310. expect( positionB.isTouching( positionA ) ).to.be.true;
  311. } );
  312. it( 'should return true if positions are in different sub-trees but there are no whole nodes between them', () => {
  313. let positionA = new Position( root, [ 1, 0, 3 ] );
  314. let positionB = new Position( root, [ 1, 1, 0 ] );
  315. expect( positionA.isTouching( positionB ) ).to.be.true;
  316. expect( positionB.isTouching( positionA ) ).to.be.true;
  317. } );
  318. it( 'should return false if there are whole nodes between positions', () => {
  319. let positionA = new Position( root, [ 2 ] );
  320. let positionB = new Position( root, [ 1, 0, 3 ] );
  321. expect( positionA.isTouching( positionB ) ).to.be.false;
  322. expect( positionB.isTouching( positionA ) ).to.be.false;
  323. } );
  324. it( 'should return false if there are whole nodes between positions', () => {
  325. let positionA = new Position( root, [ 1, 0, 3 ] );
  326. let positionB = new Position( root, [ 1, 1, 1 ] );
  327. expect( positionA.isTouching( positionB ) ).to.be.false;
  328. expect( positionB.isTouching( positionA ) ).to.be.false;
  329. } );
  330. it( 'should return false if positions are in different roots', () => {
  331. let positionA = new Position( root, [ 1, 0, 3 ] );
  332. let positionB = new Position( otherRoot, [ 1, 1, 0 ] );
  333. expect( positionA.isTouching( positionB ) ).to.be.false;
  334. expect( positionB.isTouching( positionA ) ).to.be.false;
  335. } );
  336. } );
  337. describe( 'isAtStart', () => {
  338. it( 'should return true if position is at the beginning of its parent', () => {
  339. expect( new Position( root, [ 0 ] ).isAtStart() ).to.be.true;
  340. expect( new Position( root, [ 1 ] ).isAtStart() ).to.be.false;
  341. } );
  342. } );
  343. describe( 'isAtEnd', () => {
  344. it( 'should return true if position is at the end of its parent', () => {
  345. expect( new Position( root, [ root.getChildCount() ] ).isAtEnd() ).to.be.true;
  346. expect( new Position( root, [ 0 ] ).isAtEnd() ).to.be.false;
  347. } );
  348. } );
  349. describe( 'compareWith', () => {
  350. it( 'should return same if positions are same', () => {
  351. const position = new Position( root, [ 1, 2, 3 ] );
  352. const compared = new Position( root, [ 1, 2, 3 ] );
  353. expect( position.compareWith( compared ) ).to.equal( 'same' );
  354. } );
  355. it( 'should return before if the position is before compared one', () => {
  356. const position = new Position( root, [ 1, 2, 3 ] );
  357. const compared = new Position( root, [ 1, 3 ] );
  358. expect( position.compareWith( compared ) ).to.equal( 'before' );
  359. } );
  360. it( 'should return after if the position is after compared one', () => {
  361. const position = new Position( root, [ 1, 2, 3, 4 ] );
  362. const compared = new Position( root, [ 1, 2, 3 ] );
  363. expect( position.compareWith( compared ) ).to.equal( 'after' );
  364. } );
  365. it( 'should return different if positions are in different roots', () => {
  366. const position = new Position( root, [ 1, 2, 3 ] );
  367. const compared = new Position( otherRoot, [ 1, 2, 3 ] );
  368. expect( position.compareWith( compared ) ).to.equal( 'different' );
  369. } );
  370. } );
  371. describe( 'getTransformedByInsertion', () => {
  372. it( 'should return a new Position instance', () => {
  373. const position = new Position( root, [ 0 ] );
  374. const transformed = position.getTransformedByInsertion( new Position( root, [ 2 ] ), 4, false );
  375. expect( transformed ).not.to.equal( position );
  376. expect( transformed ).to.be.instanceof( Position );
  377. } );
  378. it( 'should increment offset if insertion is in the same parent and closer offset', () => {
  379. const position = new Position( root, [ 1, 2, 3 ] );
  380. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 2, 2 ] ), 2, false );
  381. expect( transformed.offset ).to.equal( 5 );
  382. } );
  383. it( 'should not increment offset if insertion position is in different root', () => {
  384. const position = new Position( root, [ 1, 2, 3 ] );
  385. const transformed = position.getTransformedByInsertion( new Position( otherRoot, [ 1, 2, 2 ] ), 2, false );
  386. expect( transformed.offset ).to.equal( 3 );
  387. } );
  388. it( 'should not increment offset if insertion is in the same parent and the same offset', () => {
  389. const position = new Position( root, [ 1, 2, 3 ] );
  390. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, false );
  391. expect( transformed.offset ).to.equal( 3 );
  392. } );
  393. it( 'should increment offset if insertion is in the same parent and the same offset and it is inserted before', () => {
  394. const position = new Position( root, [ 1, 2, 3 ] );
  395. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, true );
  396. expect( transformed.offset ).to.equal( 5 );
  397. } );
  398. it( 'should not increment offset if insertion is in the same parent and further offset', () => {
  399. const position = new Position( root, [ 1, 2, 3 ] );
  400. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 2, 4 ] ), 2, false );
  401. expect( transformed.offset ).to.equal( 3 );
  402. } );
  403. it( 'should update path if insertion position parent is a node from that path and offset is before next node on that path', () => {
  404. const position = new Position( root, [ 1, 2, 3 ] );
  405. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 2 ] ), 2, false );
  406. expect( transformed.path ).to.deep.equal( [ 1, 4, 3 ] );
  407. } );
  408. it( 'should not update path if insertion position parent is a node from that path and offset is after next node on that path', () => {
  409. const position = new Position( root, [ 1, 2, 3 ] );
  410. const transformed = position.getTransformedByInsertion( new Position( root, [ 1, 3 ] ), 2, false );
  411. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  412. } );
  413. } );
  414. describe( 'getTransformedByDeletion', () => {
  415. it( 'should return a new Position instance', () => {
  416. const position = new Position( root, [ 0 ] );
  417. const transformed = position.getTransformedByDeletion( new Position( root, [ 2 ] ), 4 );
  418. expect( transformed ).not.to.equal( position );
  419. expect( transformed ).to.be.instanceof( Position );
  420. } );
  421. it( 'should return null if original position is inside one of removed nodes', () => {
  422. const position = new Position( root, [ 1, 2 ] );
  423. const transformed = position.getTransformedByDeletion( new Position( root, [ 0 ] ), 2 );
  424. expect( transformed ).to.be.null;
  425. } );
  426. it( 'should decrement offset if deletion is in the same parent and closer offset', () => {
  427. const position = new Position( root, [ 1, 2, 7 ] );
  428. const transformed = position.getTransformedByDeletion( new Position( root, [ 1, 2, 2 ] ), 2 );
  429. expect( transformed.offset ).to.equal( 5 );
  430. } );
  431. it( 'should return null if original position is between removed nodes', () => {
  432. const position = new Position( root, [ 1, 2, 4 ] );
  433. const transformed = position.getTransformedByDeletion( new Position( root, [ 1, 2, 3 ] ), 5 );
  434. expect( transformed ).to.be.null;
  435. } );
  436. it( 'should not decrement offset if deletion position is in different root', () => {
  437. const position = new Position( root, [ 1, 2, 3 ] );
  438. const transformed = position.getTransformedByDeletion( new Position( otherRoot, [ 1, 2, 1 ] ), 2 );
  439. expect( transformed.offset ).to.equal( 3 );
  440. } );
  441. it( 'should not decrement offset if deletion is in the same parent and further offset', () => {
  442. const position = new Position( root, [ 1, 2, 3 ] );
  443. const transformed = position.getTransformedByDeletion( new Position( root, [ 1, 2, 4 ] ), 2 );
  444. expect( transformed.offset ).to.equal( 3 );
  445. } );
  446. it( 'should update path if deletion position parent is a node from that path and offset is before next node on that path', () => {
  447. const position = new Position( root, [ 1, 2, 3 ] );
  448. const transformed = position.getTransformedByDeletion( new Position( root, [ 1, 0 ] ), 2 );
  449. expect( transformed.path ).to.deep.equal( [ 1, 0, 3 ] );
  450. } );
  451. it( 'should not update path if deletion position parent is a node from that path and offset is after next node on that path', () => {
  452. const position = new Position( root, [ 1, 2, 3 ] );
  453. const transformed = position.getTransformedByDeletion( new Position( root, [ 1, 3 ] ), 2 );
  454. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  455. } );
  456. } );
  457. describe( 'getTransformedByMove', () => {
  458. it( 'should increment offset if a range was moved to the same parent and closer offset', () => {
  459. const position = new Position( root, [ 1, 2, 3 ] );
  460. const transformed = position.getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 1, 2, 0 ] ), 3, false );
  461. expect( transformed.path ).to.deep.equal( [ 1, 2, 6 ] );
  462. } );
  463. it( 'should decrement offset if a range was moved from the same parent and closer offset', () => {
  464. const position = new Position( root, [ 1, 2, 6 ] );
  465. const transformed = position.getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  466. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  467. } );
  468. it( 'should decrement offset if position was at the end of a range and move was not sticky', () => {
  469. const position = new Position( root, [ 1, 2, 3 ] );
  470. const transformed = position.getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  471. expect( transformed.path ).to.deep.equal( [ 1, 2, 0 ] );
  472. } );
  473. it( 'should update path if position was at the end of a range and move was sticky', () => {
  474. const position = new Position( root, [ 1, 2, 3 ] );
  475. const transformed = position.getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false, true );
  476. expect( transformed.path ).to.deep.equal( [ 5 ] );
  477. } );
  478. it( 'should update path if a range contained this position', () => {
  479. const position = new Position( root, [ 1, 2, 3 ] );
  480. const transformed = position.getTransformedByMove( new Position( root, [ 1, 1 ] ), new Position( root, [ 2, 1 ] ), 3, false );
  481. expect( transformed.path ).to.deep.equal( [ 2, 2, 3 ] );
  482. } );
  483. } );
  484. describe( '_getCombined', () => {
  485. it( 'should return correct combination of this and given positions', () => {
  486. const position = new Position( root, [ 1, 3, 4, 2 ] );
  487. const sourcePosition = new Position( root, [ 1, 1 ] );
  488. const targetPosition = new Position( root, [ 2, 5 ] );
  489. const combined = position._getCombined( sourcePosition, targetPosition );
  490. expect( combined.path ).to.deep.equal( [ 2, 7, 4, 2 ] );
  491. } );
  492. } );
  493. describe( 'getShiftedBy', () => {
  494. it( 'should return a new instance of Position with offset changed by shift value', () => {
  495. let position = new Position( root, [ 1, 2, 3 ] );
  496. let shifted = position.getShiftedBy( 2 );
  497. expect( shifted ).to.be.instanceof( Position );
  498. expect( shifted ).to.not.equal( position );
  499. expect( shifted.path ).to.deep.equal( [ 1, 2, 5 ] );
  500. } );
  501. it( 'should accept negative values', () => {
  502. let position = new Position( root, [ 1, 2, 3 ] );
  503. let shifted = position.getShiftedBy( -2 );
  504. expect( shifted.path ).to.deep.equal( [ 1, 2, 1 ] );
  505. } );
  506. it( 'should not let setting offset lower than zero', () => {
  507. let position = new Position( root, [ 1, 2, 3 ] );
  508. let shifted = position.getShiftedBy( -7 );
  509. expect( shifted.path ).to.deep.equal( [ 1, 2, 0 ] );
  510. } );
  511. } );
  512. describe( 'toJSON', () => {
  513. it( 'should serialize position', () => {
  514. let position = new Position( root, [ 0 ] );
  515. let serialized = jsonParseStringify( position );
  516. expect( serialized ).to.deep.equal( { root: 'main', path: [ 0 ] } );
  517. } );
  518. it( 'should serialize position from graveyard', () => {
  519. let position = new Position( doc.graveyard, [ 0 ] );
  520. let serialized = jsonParseStringify( position );
  521. expect( serialized ).to.deep.equal( { root: '$graveyard', path: [ 0 ] } );
  522. } );
  523. } );
  524. describe( 'fromJSON', () => {
  525. it( 'should create object with given document', () => {
  526. let deserialized = Position.fromJSON( { root: 'main', path: [ 0, 1, 2 ] }, doc );
  527. expect( deserialized.root ).to.equal( root );
  528. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  529. } );
  530. it( 'should create object from graveyard', () => {
  531. let deserialized = Position.fromJSON( { root: '$graveyard', path: [ 0, 1, 2 ] }, doc );
  532. expect( deserialized.root ).to.equal( doc.graveyard );
  533. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  534. } );
  535. it( 'should throw error when creating object in document that does not have provided root', () => {
  536. expect(
  537. () => {
  538. Position.fromJSON( { root: 'noroot', path: [ 0 ] }, doc );
  539. }
  540. ).to.throw( CKEditorError, /position-fromjson-no-root/ );
  541. } );
  542. } );
  543. } );