position.js 24 KB

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