position.js 22 KB

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