position.js 24 KB

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