position.js 29 KB

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