position.js 26 KB

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