position.js 24 KB

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