position.js 29 KB

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