position.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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, foz, bar;
  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. 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. 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 have index', () => {
  212. expect( new Position( root, [ 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  213. expect( new Position( root, [ 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  214. expect( new Position( root, [ 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  215. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  216. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  217. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  218. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  219. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  220. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  221. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  222. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  223. } );
  224. it( 'should be able to set offset', () => {
  225. let position = new Position( root, [ 1, 0, 2 ] );
  226. position.offset = 4;
  227. expect( position.offset ).to.equal( 4 );
  228. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  229. } );
  230. it( 'should have nodeBefore if it is not inside a text node', () => {
  231. expect( new Position( root, [ 0 ] ).nodeBefore ).to.be.null;
  232. expect( new Position( root, [ 1 ] ).nodeBefore ).to.equal( p );
  233. expect( new Position( root, [ 2 ] ).nodeBefore ).to.equal( ul );
  234. expect( new Position( root, [ 0, 0 ] ).nodeBefore ).to.null;
  235. expect( new Position( root, [ 1, 0 ] ).nodeBefore ).to.be.null;
  236. expect( new Position( root, [ 1, 1 ] ).nodeBefore ).to.equal( li1 );
  237. expect( new Position( root, [ 1, 2 ] ).nodeBefore ).to.equal( li2 );
  238. expect( new Position( root, [ 1, 0, 0 ] ).nodeBefore ).to.be.null;
  239. expect( new Position( root, [ 1, 0, 1 ] ).nodeBefore ).to.be.null;
  240. expect( new Position( root, [ 1, 0, 2 ] ).nodeBefore ).to.be.null;
  241. expect( new Position( root, [ 1, 0, 3 ] ).nodeBefore.data ).to.equal( 'foz' );
  242. } );
  243. it( 'should have nodeAfter if it is not inside a text node', () => {
  244. expect( new Position( root, [ 0 ] ).nodeAfter ).to.equal( p );
  245. expect( new Position( root, [ 1 ] ).nodeAfter ).to.equal( ul );
  246. expect( new Position( root, [ 2 ] ).nodeAfter ).to.be.null;
  247. expect( new Position( root, [ 0, 0 ] ).nodeAfter ).to.be.null;
  248. expect( new Position( root, [ 1, 0 ] ).nodeAfter ).to.equal( li1 );
  249. expect( new Position( root, [ 1, 1 ] ).nodeAfter ).to.equal( li2 );
  250. expect( new Position( root, [ 1, 2 ] ).nodeAfter ).to.be.null;
  251. expect( new Position( root, [ 1, 0, 0 ] ).nodeAfter.data ).to.equal( 'foz' );
  252. expect( new Position( root, [ 1, 0, 1 ] ).nodeAfter ).to.be.null;
  253. expect( new Position( root, [ 1, 0, 2 ] ).nodeAfter ).to.be.null;
  254. expect( new Position( root, [ 1, 0, 3 ] ).nodeAfter ).to.be.null;
  255. } );
  256. it( 'should have a text node property if it is in text node', () => {
  257. expect( new Position( root, [ 0 ] ).textNode ).to.be.null;
  258. expect( new Position( root, [ 1 ] ).textNode ).to.be.null;
  259. expect( new Position( root, [ 2 ] ).textNode ).to.be.null;
  260. expect( new Position( root, [ 0, 0 ] ).textNode ).to.be.null;
  261. expect( new Position( root, [ 1, 0 ] ).textNode ).to.be.null;
  262. expect( new Position( root, [ 1, 1 ] ).textNode ).to.be.null;
  263. expect( new Position( root, [ 1, 2 ] ).textNode ).to.be.null;
  264. expect( new Position( root, [ 1, 0, 0 ] ).textNode ).to.be.null;
  265. expect( new Position( root, [ 1, 0, 1 ] ).textNode ).to.equal( foz );
  266. expect( new Position( root, [ 1, 0, 2 ] ).textNode ).to.equal( foz );
  267. expect( new Position( root, [ 1, 0, 3 ] ).textNode ).to.be.null;
  268. expect( new Position( root, [ 1, 1, 0 ] ).textNode ).to.be.null;
  269. expect( new Position( root, [ 1, 1, 1 ] ).textNode ).to.equal( bar );
  270. expect( new Position( root, [ 1, 1, 2 ] ).textNode ).to.equal( bar );
  271. expect( new Position( root, [ 1, 1, 3 ] ).textNode ).to.be.null;
  272. } );
  273. it( 'should have proper parent path', () => {
  274. let position = new Position( root, [ 1, 2, 3 ] );
  275. expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
  276. } );
  277. describe( 'isBefore', () => {
  278. it( 'should return true if given position has same root and is before this position', () => {
  279. let position = new Position( root, [ 1, 1, 2 ] );
  280. let beforePosition = new Position( root, [ 1, 0 ] );
  281. expect( position.isAfter( beforePosition ) ).to.be.true;
  282. } );
  283. it( 'should return false if given position has same root and is not before this position', () => {
  284. let position = new Position( root, [ 1, 1, 2 ] );
  285. let afterPosition = new Position( root, [ 1, 2 ] );
  286. expect( position.isAfter( afterPosition ) ).to.be.false;
  287. } );
  288. it( 'should return false if given position has different root', () => {
  289. let position = new Position( root, [ 1, 1, 2 ] );
  290. let differentPosition = new Position( otherRoot, [ 1, 0 ] );
  291. expect( position.isAfter( differentPosition ) ).to.be.false;
  292. } );
  293. } );
  294. describe( 'isEqual', () => {
  295. it( 'should return true if given position has same path and root', () => {
  296. let position = new Position( root, [ 1, 1, 2 ] );
  297. let samePosition = new Position( root, [ 1, 1, 2 ] );
  298. expect( position.isEqual( samePosition ) ).to.be.true;
  299. } );
  300. it( 'should return false if given position has different path', () => {
  301. let position = new Position( root, [ 1, 1, 1 ] );
  302. let differentPosition = new Position( root, [ 1, 2, 2 ] );
  303. expect( position.isEqual( differentPosition ) ).to.be.false;
  304. } );
  305. it( 'should return false if given position has different root', () => {
  306. let position = new Position( root, [ 1, 1, 1 ] );
  307. let differentPosition = new Position( otherRoot, [ 1, 1, 1 ] );
  308. expect( position.isEqual( differentPosition ) ).to.be.false;
  309. } );
  310. } );
  311. describe( 'isAfter', () => {
  312. it( 'should return true if given position has same root and is after this position', () => {
  313. let position = new Position( root, [ 1, 1, 2 ] );
  314. let afterPosition = new Position( root, [ 1, 2 ] );
  315. expect( position.isBefore( afterPosition ) ).to.be.true;
  316. } );
  317. it( 'should return false if given position has same root and is not after this position', () => {
  318. let position = new Position( root, [ 1, 1, 2 ] );
  319. let beforePosition = new Position( root, [ 1, 0 ] );
  320. expect( position.isBefore( beforePosition ) ).to.be.false;
  321. } );
  322. it( 'should return false if given position has different root', () => {
  323. let position = new Position( root, [ 1, 1, 2 ] );
  324. let differentPosition = new Position( otherRoot, [ 1, 2 ] );
  325. expect( position.isBefore( differentPosition ) ).to.be.false;
  326. } );
  327. } );
  328. describe( 'isTouching', () => {
  329. it( 'should return true if positions are same', () => {
  330. let position = new Position( root, [ 1, 1, 1 ] );
  331. let result = position.isTouching( new Position( root, [ 1, 1, 1 ] ) );
  332. expect( result ).to.be.true;
  333. } );
  334. it( 'should return true if given position is in next node and there are no whole nodes before it', () => {
  335. let positionA = new Position( root, [ 1 ] );
  336. let positionB = new Position( root, [ 1, 0, 0 ] );
  337. expect( positionA.isTouching( positionB ) ).to.be.true;
  338. expect( positionB.isTouching( positionA ) ).to.be.true;
  339. } );
  340. it( 'should return true if given position is in previous node and there are no whole nodes after it', () => {
  341. let positionA = new Position( root, [ 2 ] );
  342. let positionB = new Position( root, [ 1, 1, 3 ] );
  343. expect( positionA.isTouching( positionB ) ).to.be.true;
  344. expect( positionB.isTouching( positionA ) ).to.be.true;
  345. } );
  346. it( 'should return true if positions are in different sub-trees but there are no whole nodes between them', () => {
  347. let positionA = new Position( root, [ 1, 0, 3 ] );
  348. let positionB = new Position( root, [ 1, 1, 0 ] );
  349. expect( positionA.isTouching( positionB ) ).to.be.true;
  350. expect( positionB.isTouching( positionA ) ).to.be.true;
  351. } );
  352. it( 'should return false if there are whole nodes between positions', () => {
  353. let positionA = new Position( root, [ 2 ] );
  354. let positionB = new Position( root, [ 1, 0, 3 ] );
  355. expect( positionA.isTouching( positionB ) ).to.be.false;
  356. expect( positionB.isTouching( positionA ) ).to.be.false;
  357. } );
  358. it( 'should return false if there are whole nodes between positions', () => {
  359. let positionA = new Position( root, [ 1, 0, 3 ] );
  360. let positionB = new Position( root, [ 1, 1, 1 ] );
  361. expect( positionA.isTouching( positionB ) ).to.be.false;
  362. expect( positionB.isTouching( positionA ) ).to.be.false;
  363. } );
  364. it( 'should return false if positions are in different roots', () => {
  365. let positionA = new Position( root, [ 1, 0, 3 ] );
  366. let positionB = new Position( otherRoot, [ 1, 1, 0 ] );
  367. expect( positionA.isTouching( positionB ) ).to.be.false;
  368. expect( positionB.isTouching( positionA ) ).to.be.false;
  369. } );
  370. } );
  371. describe( 'isAtStart', () => {
  372. it( 'should return true if position is at the beginning of its parent', () => {
  373. expect( new Position( root, [ 0 ] ).isAtStart() ).to.be.true;
  374. expect( new Position( root, [ 1 ] ).isAtStart() ).to.be.false;
  375. } );
  376. } );
  377. describe( 'isAtEnd', () => {
  378. it( 'should return true if position is at the end of its parent', () => {
  379. expect( new Position( root, [ root.getMaxOffset() ] ).isAtEnd() ).to.be.true;
  380. expect( new Position( root, [ 0 ] ).isAtEnd() ).to.be.false;
  381. } );
  382. } );
  383. describe( 'compareWith', () => {
  384. it( 'should return same if positions are same', () => {
  385. const position = new Position( root, [ 1, 2, 3 ] );
  386. const compared = new Position( root, [ 1, 2, 3 ] );
  387. expect( position.compareWith( compared ) ).to.equal( 'same' );
  388. } );
  389. it( 'should return before if the position is before compared one', () => {
  390. const position = new Position( root, [ 1, 2, 3 ] );
  391. const compared = new Position( root, [ 1, 3 ] );
  392. expect( position.compareWith( compared ) ).to.equal( 'before' );
  393. } );
  394. it( 'should return after if the position is after compared one', () => {
  395. const position = new Position( root, [ 1, 2, 3, 4 ] );
  396. const compared = new Position( root, [ 1, 2, 3 ] );
  397. expect( position.compareWith( compared ) ).to.equal( 'after' );
  398. } );
  399. it( 'should return different if positions are in different roots', () => {
  400. const position = new Position( root, [ 1, 2, 3 ] );
  401. const compared = new Position( otherRoot, [ 1, 2, 3 ] );
  402. expect( position.compareWith( compared ) ).to.equal( 'different' );
  403. } );
  404. } );
  405. describe( '_getTransformedByInsertion', () => {
  406. it( 'should return a new Position instance', () => {
  407. const position = new Position( root, [ 0 ] );
  408. const transformed = position._getTransformedByInsertion( new Position( root, [ 2 ] ), 4, false );
  409. expect( transformed ).not.to.equal( position );
  410. expect( transformed ).to.be.instanceof( Position );
  411. } );
  412. it( 'should increment offset if insertion is in the same parent and closer offset', () => {
  413. const position = new Position( root, [ 1, 2, 3 ] );
  414. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 2 ] ), 2, false );
  415. expect( transformed.offset ).to.equal( 5 );
  416. } );
  417. it( 'should not increment offset if insertion position is in different root', () => {
  418. const position = new Position( root, [ 1, 2, 3 ] );
  419. const transformed = position._getTransformedByInsertion( new Position( otherRoot, [ 1, 2, 2 ] ), 2, false );
  420. expect( transformed.offset ).to.equal( 3 );
  421. } );
  422. it( 'should not increment offset if insertion is in the same parent and the same offset', () => {
  423. const position = new Position( root, [ 1, 2, 3 ] );
  424. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, false );
  425. expect( transformed.offset ).to.equal( 3 );
  426. } );
  427. it( 'should increment offset if insertion is in the same parent and the same offset and it is inserted before', () => {
  428. const position = new Position( root, [ 1, 2, 3 ] );
  429. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2, true );
  430. expect( transformed.offset ).to.equal( 5 );
  431. } );
  432. it( 'should not increment offset if insertion is in the same parent and further offset', () => {
  433. const position = new Position( root, [ 1, 2, 3 ] );
  434. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 4 ] ), 2, false );
  435. expect( transformed.offset ).to.equal( 3 );
  436. } );
  437. it( 'should update path if insertion position parent is a node from that path and offset is before next node on that path', () => {
  438. const position = new Position( root, [ 1, 2, 3 ] );
  439. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2 ] ), 2, false );
  440. expect( transformed.path ).to.deep.equal( [ 1, 4, 3 ] );
  441. } );
  442. it( 'should not update path if insertion position parent is a node from that path and offset is after next node on that path', () => {
  443. const position = new Position( root, [ 1, 2, 3 ] );
  444. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 3 ] ), 2, false );
  445. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  446. } );
  447. } );
  448. describe( '_getTransformedByDeletion', () => {
  449. it( 'should return a new Position instance', () => {
  450. const position = new Position( root, [ 0 ] );
  451. const transformed = position._getTransformedByDeletion( new Position( root, [ 2 ] ), 4 );
  452. expect( transformed ).not.to.equal( position );
  453. expect( transformed ).to.be.instanceof( Position );
  454. } );
  455. it( 'should return null if original position is inside one of removed nodes', () => {
  456. const position = new Position( root, [ 1, 2 ] );
  457. const transformed = position._getTransformedByDeletion( new Position( root, [ 0 ] ), 2 );
  458. expect( transformed ).to.be.null;
  459. } );
  460. it( 'should decrement offset if deletion is in the same parent and closer offset', () => {
  461. const position = new Position( root, [ 1, 2, 7 ] );
  462. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 2 ] ), 2 );
  463. expect( transformed.offset ).to.equal( 5 );
  464. } );
  465. it( 'should return null if original position is between removed nodes', () => {
  466. const position = new Position( root, [ 1, 2, 4 ] );
  467. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 3 ] ), 5 );
  468. expect( transformed ).to.be.null;
  469. } );
  470. it( 'should not decrement offset if deletion position is in different root', () => {
  471. const position = new Position( root, [ 1, 2, 3 ] );
  472. const transformed = position._getTransformedByDeletion( new Position( otherRoot, [ 1, 2, 1 ] ), 2 );
  473. expect( transformed.offset ).to.equal( 3 );
  474. } );
  475. it( 'should not decrement offset if deletion is in the same parent and further offset', () => {
  476. const position = new Position( root, [ 1, 2, 3 ] );
  477. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 4 ] ), 2 );
  478. expect( transformed.offset ).to.equal( 3 );
  479. } );
  480. it( 'should update path if deletion position parent is a node from that path and offset is before next node on that path', () => {
  481. const position = new Position( root, [ 1, 2, 3 ] );
  482. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 0 ] ), 2 );
  483. expect( transformed.path ).to.deep.equal( [ 1, 0, 3 ] );
  484. } );
  485. it( 'should not update path if deletion position parent is a node from that path and offset is after next node on that path', () => {
  486. const position = new Position( root, [ 1, 2, 3 ] );
  487. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 3 ] ), 2 );
  488. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  489. } );
  490. } );
  491. describe( '_getTransformedByMove', () => {
  492. it( 'should increment offset if a range was moved to the same parent and closer offset', () => {
  493. const position = new Position( root, [ 1, 2, 3 ] );
  494. const transformed = position._getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 1, 2, 0 ] ), 3, false );
  495. expect( transformed.path ).to.deep.equal( [ 1, 2, 6 ] );
  496. } );
  497. it( 'should decrement offset if a range was moved from the same parent and closer offset', () => {
  498. const position = new Position( root, [ 1, 2, 6 ] );
  499. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  500. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  501. } );
  502. it( 'should decrement offset if position was at the end of a range and move was not sticky', () => {
  503. const position = new Position( root, [ 1, 2, 3 ] );
  504. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  505. expect( transformed.path ).to.deep.equal( [ 1, 2, 0 ] );
  506. } );
  507. it( 'should update path if position was at the end of a range and move was sticky', () => {
  508. const position = new Position( root, [ 1, 2, 3 ] );
  509. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false, true );
  510. expect( transformed.path ).to.deep.equal( [ 5 ] );
  511. } );
  512. it( 'should update path if a range contained this position', () => {
  513. const position = new Position( root, [ 1, 2, 3 ] );
  514. const transformed = position._getTransformedByMove( new Position( root, [ 1, 1 ] ), new Position( root, [ 2, 1 ] ), 3, false );
  515. expect( transformed.path ).to.deep.equal( [ 2, 2, 3 ] );
  516. } );
  517. } );
  518. describe( '_getCombined', () => {
  519. it( 'should return correct combination of this and given positions', () => {
  520. const position = new Position( root, [ 1, 3, 4, 2 ] );
  521. const sourcePosition = new Position( root, [ 1, 1 ] );
  522. const targetPosition = new Position( root, [ 2, 5 ] );
  523. const combined = position._getCombined( sourcePosition, targetPosition );
  524. expect( combined.path ).to.deep.equal( [ 2, 7, 4, 2 ] );
  525. } );
  526. } );
  527. describe( 'getShiftedBy', () => {
  528. it( 'should return a new instance of Position with offset changed by shift value', () => {
  529. let position = new Position( root, [ 1, 2, 3 ] );
  530. let shifted = position.getShiftedBy( 2 );
  531. expect( shifted ).to.be.instanceof( Position );
  532. expect( shifted ).to.not.equal( position );
  533. expect( shifted.path ).to.deep.equal( [ 1, 2, 5 ] );
  534. } );
  535. it( 'should accept negative values', () => {
  536. let position = new Position( root, [ 1, 2, 3 ] );
  537. let shifted = position.getShiftedBy( -2 );
  538. expect( shifted.path ).to.deep.equal( [ 1, 2, 1 ] );
  539. } );
  540. it( 'should not let setting offset lower than zero', () => {
  541. let position = new Position( root, [ 1, 2, 3 ] );
  542. let shifted = position.getShiftedBy( -7 );
  543. expect( shifted.path ).to.deep.equal( [ 1, 2, 0 ] );
  544. } );
  545. } );
  546. describe( 'toJSON', () => {
  547. it( 'should serialize position', () => {
  548. let position = new Position( root, [ 0 ] );
  549. let serialized = jsonParseStringify( position );
  550. expect( serialized ).to.deep.equal( { root: 'main', path: [ 0 ] } );
  551. } );
  552. it( 'should serialize position from graveyard', () => {
  553. let position = new Position( doc.graveyard, [ 0 ] );
  554. let serialized = jsonParseStringify( position );
  555. expect( serialized ).to.deep.equal( { root: '$graveyard', path: [ 0 ] } );
  556. } );
  557. } );
  558. describe( 'fromJSON', () => {
  559. it( 'should create object with given document', () => {
  560. let deserialized = Position.fromJSON( { root: 'main', path: [ 0, 1, 2 ] }, doc );
  561. expect( deserialized.root ).to.equal( root );
  562. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  563. } );
  564. it( 'should create object from graveyard', () => {
  565. let deserialized = Position.fromJSON( { root: '$graveyard', path: [ 0, 1, 2 ] }, doc );
  566. expect( deserialized.root ).to.equal( doc.graveyard );
  567. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  568. } );
  569. it( 'should throw error when creating object in document that does not have provided root', () => {
  570. expect(
  571. () => {
  572. Position.fromJSON( { root: 'noroot', path: [ 0 ] }, doc );
  573. }
  574. ).to.throw( CKEditorError, /position-fromjson-no-root/ );
  575. } );
  576. } );
  577. } );