8
0

position.js 34 KB

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