position.js 27 KB

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