position.js 33 KB

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