position.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  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 Range from '../../src/model/range';
  12. import MarkerOperation from '../../src/model/operation/markeroperation';
  13. import AttributeOperation from '../../src/model/operation/attributeoperation';
  14. import InsertOperation from '../../src/model/operation/insertoperation';
  15. import MoveOperation from '../../src/model/operation/moveoperation';
  16. import RenameOperation from '../../src/model/operation/renameoperation';
  17. import MergeOperation from '../../src/model/operation/mergeoperation';
  18. import SplitOperation from '../../src/model/operation/splitoperation';
  19. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  20. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  21. describe( 'Position', () => {
  22. let doc, model, root, otherRoot, p, ul, li1, li2, f, o, z, b, a, r, foz, bar;
  23. testUtils.createSinonSandbox();
  24. // root
  25. // |- p Before: [ 0 ] After: [ 1 ]
  26. // |- ul Before: [ 1 ] After: [ 2 ]
  27. // |- li Before: [ 1, 0 ] After: [ 1, 1 ]
  28. // | |- f Before: [ 1, 0, 0 ] After: [ 1, 0, 1 ]
  29. // | |- o Before: [ 1, 0, 1 ] After: [ 1, 0, 2 ]
  30. // | |- z Before: [ 1, 0, 2 ] After: [ 1, 0, 3 ]
  31. // |- li Before: [ 1, 1 ] After: [ 1, 2 ]
  32. // |- b Before: [ 1, 1, 0 ] After: [ 1, 1, 1 ]
  33. // |- a Before: [ 1, 1, 1 ] After: [ 1, 1, 2 ]
  34. // |- r Before: [ 1, 1, 2 ] After: [ 1, 1, 3 ]
  35. before( () => {
  36. model = new Model();
  37. doc = model.document;
  38. root = doc.createRoot();
  39. otherRoot = doc.createRoot( '$root', 'otherRoot' );
  40. foz = new Text( 'foz' );
  41. li1 = new Element( 'li', [], foz );
  42. f = new TextProxy( foz, 0, 1 );
  43. o = new TextProxy( foz, 1, 1 );
  44. z = new TextProxy( foz, 2, 1 );
  45. bar = new Text( 'bar' );
  46. li2 = new Element( 'li', [], bar );
  47. b = new TextProxy( bar, 0, 1 );
  48. a = new TextProxy( bar, 1, 1 );
  49. r = new TextProxy( bar, 2, 1 );
  50. ul = new Element( 'ul', [], [ li1, li2 ] );
  51. p = new Element( 'p' );
  52. root._insertChild( 0, [ p, ul ] );
  53. } );
  54. describe( 'constructor()', () => {
  55. it( 'should create a position with path and document', () => {
  56. const position = new Position( root, [ 0 ] );
  57. expect( position ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  58. expect( position ).to.have.property( 'root' ).that.equals( root );
  59. } );
  60. it( 'should accept DocumentFragment as a root', () => {
  61. const frag = new DocumentFragment();
  62. const pos = new Position( frag, [ 0 ] );
  63. expect( pos ).to.have.property( 'root', frag );
  64. } );
  65. it( 'should accept detached Element as a root', () => {
  66. const el = new Element( 'p' );
  67. const pos = new Position( el, [ 0 ] );
  68. expect( pos ).to.have.property( 'root', el );
  69. expect( pos.path ).to.deep.equal( [ 0 ] );
  70. } );
  71. it( 'should normalize attached Element as a root', () => {
  72. const pos = new Position( li1, [ 0, 2 ] );
  73. expect( pos ).to.have.property( 'root', root );
  74. expect( pos.isEqual( Position.createAt( li1, 0, 2 ) ) );
  75. } );
  76. it( 'should normalize Element from a detached branch as a root', () => {
  77. const rootEl = new Element( 'p', null, [ new Element( 'a' ) ] );
  78. const elA = rootEl.getChild( 0 );
  79. const pos = new Position( elA, [ 0 ] );
  80. expect( pos ).to.have.property( 'root', rootEl );
  81. expect( pos.isEqual( Position.createAt( elA, 0 ) ) );
  82. } );
  83. it( 'should throw error if given path is incorrect', () => {
  84. expect( () => {
  85. new Position( root, {} ); // eslint-disable-line no-new
  86. } ).to.throw( CKEditorError, /model-position-path-incorrect/ );
  87. expect( () => {
  88. new Position( root, [] ); // eslint-disable-line no-new
  89. } ).to.throw( CKEditorError, /model-position-path-incorrect/ );
  90. } );
  91. it( 'should throw error if given root is invalid', () => {
  92. expect( () => {
  93. new Position( new Text( 'a' ) ); // eslint-disable-line no-new
  94. } ).to.throw( CKEditorError, /model-position-root-invalid/ );
  95. expect( () => {
  96. new Position(); // eslint-disable-line no-new
  97. } ).to.throw();
  98. } );
  99. } );
  100. describe( 'createFromParentAndOffset()', () => {
  101. it( 'should create positions form node and offset', () => {
  102. expect( Position.createFromParentAndOffset( root, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  103. expect( Position.createFromParentAndOffset( root, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  104. expect( Position.createFromParentAndOffset( root, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  105. expect( Position.createFromParentAndOffset( p, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 0, 0 ] );
  106. expect( Position.createFromParentAndOffset( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  107. expect( Position.createFromParentAndOffset( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  108. expect( Position.createFromParentAndOffset( ul, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  109. expect( Position.createFromParentAndOffset( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  110. expect( Position.createFromParentAndOffset( li1, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  111. expect( Position.createFromParentAndOffset( li1, 2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  112. expect( Position.createFromParentAndOffset( li1, 3 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  113. } );
  114. it( 'throws when parent is not an element', () => {
  115. expect( () => {
  116. Position.createFromParentAndOffset( b, 0 );
  117. } ).to.throw( CKEditorError, /^model-position-parent-incorrect/ );
  118. } );
  119. it( 'works with a doc frag', () => {
  120. const frag = new DocumentFragment();
  121. expect( Position.createFromParentAndOffset( frag, 0 ) ).to.have.property( 'root', frag );
  122. } );
  123. } );
  124. describe( 'createAt()', () => {
  125. it( 'should throw if no offset is passed', () => {
  126. expect( () => Position.createAt( ul ) ).to.throw( CKEditorError, /model-position-createAt-offset-required/ );
  127. } );
  128. it( 'should create positions from positions', () => {
  129. const spy = testUtils.sinon.spy( Position, 'createFromPosition' );
  130. expect( Position.createAt( Position.createAt( ul, 0 ) ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  131. expect( spy.calledOnce ).to.be.true;
  132. } );
  133. it( 'should create positions from node and offset', () => {
  134. expect( Position.createAt( ul, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  135. expect( Position.createAt( li1, 0 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  136. expect( Position.createAt( ul, 1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  137. } );
  138. it( 'should create positions from node and flag', () => {
  139. expect( Position.createAt( root, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  140. expect( Position.createAt( p, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  141. expect( Position.createAt( a, 'before' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  142. expect( Position.createAt( p, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  143. expect( Position.createAt( a, 'after' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  144. expect( Position.createAt( ul, 'end' ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  145. } );
  146. } );
  147. describe( 'createBefore()', () => {
  148. it( 'should create positions before elements', () => {
  149. expect( Position.createBefore( p ) ).to.have.property( 'path' ).that.deep.equals( [ 0 ] );
  150. expect( Position.createBefore( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  151. expect( Position.createBefore( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0 ] );
  152. expect( Position.createBefore( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 0 ] );
  153. expect( Position.createBefore( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  154. expect( Position.createBefore( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  155. expect( Position.createBefore( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  156. expect( Position.createBefore( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 0 ] );
  157. expect( Position.createBefore( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  158. expect( Position.createBefore( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  159. } );
  160. it( 'should throw error if one try to create positions before root', () => {
  161. expect( () => {
  162. Position.createBefore( root );
  163. } ).to.throw( CKEditorError, /model-position-before-root/ );
  164. } );
  165. } );
  166. describe( 'createAfter()', () => {
  167. it( 'should create positions after elements', () => {
  168. expect( Position.createAfter( p ) ).to.have.property( 'path' ).that.deep.equals( [ 1 ] );
  169. expect( Position.createAfter( ul ) ).to.have.property( 'path' ).that.deep.equals( [ 2 ] );
  170. expect( Position.createAfter( li1 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1 ] );
  171. expect( Position.createAfter( f ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 1 ] );
  172. expect( Position.createAfter( o ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 2 ] );
  173. expect( Position.createAfter( z ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 0, 3 ] );
  174. expect( Position.createAfter( li2 ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 2 ] );
  175. expect( Position.createAfter( b ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 1 ] );
  176. expect( Position.createAfter( a ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 2 ] );
  177. expect( Position.createAfter( r ) ).to.have.property( 'path' ).that.deep.equals( [ 1, 1, 3 ] );
  178. } );
  179. it( 'should throw error if one try to make positions after root', () => {
  180. expect( () => {
  181. Position.createAfter( root );
  182. } ).to.throw( CKEditorError, /model-position-after-root/ );
  183. } );
  184. } );
  185. describe( 'createFromPosition()', () => {
  186. it( 'should create a copy of given position', () => {
  187. const original = new Position( root, [ 1, 2, 3 ] );
  188. const position = Position.createFromPosition( original );
  189. expect( position ).to.be.instanceof( Position );
  190. expect( position.isEqual( original ) ).to.be.true;
  191. expect( position ).not.to.be.equal( original );
  192. } );
  193. } );
  194. it( 'should have parent', () => {
  195. expect( new Position( root, [ 0 ] ) ).to.have.property( 'parent' ).that.equals( root );
  196. expect( new Position( root, [ 1 ] ) ).to.have.property( 'parent' ).that.equals( root );
  197. expect( new Position( root, [ 2 ] ) ).to.have.property( 'parent' ).that.equals( root );
  198. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( p );
  199. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  200. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  201. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'parent' ).that.equals( ul );
  202. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  203. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  204. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  205. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'parent' ).that.equals( li1 );
  206. } );
  207. it( 'should have offset', () => {
  208. expect( new Position( root, [ 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  209. expect( new Position( root, [ 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  210. expect( new Position( root, [ 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  211. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  212. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  213. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  214. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  215. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'offset' ).that.equals( 0 );
  216. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'offset' ).that.equals( 1 );
  217. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'offset' ).that.equals( 2 );
  218. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'offset' ).that.equals( 3 );
  219. } );
  220. it( 'should have index', () => {
  221. expect( new Position( root, [ 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  222. expect( new Position( root, [ 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  223. expect( new Position( root, [ 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  224. expect( new Position( root, [ 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  225. expect( new Position( root, [ 1, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  226. expect( new Position( root, [ 1, 1 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  227. expect( new Position( root, [ 1, 2 ] ) ).to.have.property( 'index' ).that.equals( 2 );
  228. expect( new Position( root, [ 1, 0, 0 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  229. expect( new Position( root, [ 1, 0, 1 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  230. expect( new Position( root, [ 1, 0, 2 ] ) ).to.have.property( 'index' ).that.equals( 0 );
  231. expect( new Position( root, [ 1, 0, 3 ] ) ).to.have.property( 'index' ).that.equals( 1 );
  232. } );
  233. it( 'should be able to set offset', () => {
  234. const position = new Position( root, [ 1, 0, 2 ] );
  235. position.offset = 4;
  236. expect( position.offset ).to.equal( 4 );
  237. expect( position.path ).to.deep.equal( [ 1, 0, 4 ] );
  238. } );
  239. it( 'should have nodeBefore if it is not inside a text node', () => {
  240. expect( new Position( root, [ 0 ] ).nodeBefore ).to.be.null;
  241. expect( new Position( root, [ 1 ] ).nodeBefore ).to.equal( p );
  242. expect( new Position( root, [ 2 ] ).nodeBefore ).to.equal( ul );
  243. expect( new Position( root, [ 0, 0 ] ).nodeBefore ).to.null;
  244. expect( new Position( root, [ 1, 0 ] ).nodeBefore ).to.be.null;
  245. expect( new Position( root, [ 1, 1 ] ).nodeBefore ).to.equal( li1 );
  246. expect( new Position( root, [ 1, 2 ] ).nodeBefore ).to.equal( li2 );
  247. expect( new Position( root, [ 1, 0, 0 ] ).nodeBefore ).to.be.null;
  248. expect( new Position( root, [ 1, 0, 1 ] ).nodeBefore ).to.be.null;
  249. expect( new Position( root, [ 1, 0, 2 ] ).nodeBefore ).to.be.null;
  250. expect( new Position( root, [ 1, 0, 3 ] ).nodeBefore.data ).to.equal( 'foz' );
  251. } );
  252. it( 'should have nodeAfter if it is not inside a text node', () => {
  253. expect( new Position( root, [ 0 ] ).nodeAfter ).to.equal( p );
  254. expect( new Position( root, [ 1 ] ).nodeAfter ).to.equal( ul );
  255. expect( new Position( root, [ 2 ] ).nodeAfter ).to.be.null;
  256. expect( new Position( root, [ 0, 0 ] ).nodeAfter ).to.be.null;
  257. expect( new Position( root, [ 1, 0 ] ).nodeAfter ).to.equal( li1 );
  258. expect( new Position( root, [ 1, 1 ] ).nodeAfter ).to.equal( li2 );
  259. expect( new Position( root, [ 1, 2 ] ).nodeAfter ).to.be.null;
  260. expect( new Position( root, [ 1, 0, 0 ] ).nodeAfter.data ).to.equal( 'foz' );
  261. expect( new Position( root, [ 1, 0, 1 ] ).nodeAfter ).to.be.null;
  262. expect( new Position( root, [ 1, 0, 2 ] ).nodeAfter ).to.be.null;
  263. expect( new Position( root, [ 1, 0, 3 ] ).nodeAfter ).to.be.null;
  264. } );
  265. it( 'should have a text node property if it is in text node', () => {
  266. expect( new Position( root, [ 0 ] ).textNode ).to.be.null;
  267. expect( new Position( root, [ 1 ] ).textNode ).to.be.null;
  268. expect( new Position( root, [ 2 ] ).textNode ).to.be.null;
  269. expect( new Position( root, [ 0, 0 ] ).textNode ).to.be.null;
  270. expect( new Position( root, [ 1, 0 ] ).textNode ).to.be.null;
  271. expect( new Position( root, [ 1, 1 ] ).textNode ).to.be.null;
  272. expect( new Position( root, [ 1, 2 ] ).textNode ).to.be.null;
  273. expect( new Position( root, [ 1, 0, 0 ] ).textNode ).to.be.null;
  274. expect( new Position( root, [ 1, 0, 1 ] ).textNode ).to.equal( foz );
  275. expect( new Position( root, [ 1, 0, 2 ] ).textNode ).to.equal( foz );
  276. expect( new Position( root, [ 1, 0, 3 ] ).textNode ).to.be.null;
  277. expect( new Position( root, [ 1, 1, 0 ] ).textNode ).to.be.null;
  278. expect( new Position( root, [ 1, 1, 1 ] ).textNode ).to.equal( bar );
  279. expect( new Position( root, [ 1, 1, 2 ] ).textNode ).to.equal( bar );
  280. expect( new Position( root, [ 1, 1, 3 ] ).textNode ).to.be.null;
  281. } );
  282. it( 'should have proper parent path', () => {
  283. const position = new Position( root, [ 1, 2, 3 ] );
  284. expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
  285. } );
  286. describe( 'isBefore()', () => {
  287. it( 'should return true if given position has same root and is before this position', () => {
  288. const position = new Position( root, [ 1, 1, 2 ] );
  289. const beforePosition = new Position( root, [ 1, 0 ] );
  290. expect( position.isAfter( beforePosition ) ).to.be.true;
  291. } );
  292. it( 'should return false if given position has same root and is not before this position', () => {
  293. const position = new Position( root, [ 1, 1, 2 ] );
  294. const afterPosition = new Position( root, [ 1, 2 ] );
  295. expect( position.isAfter( afterPosition ) ).to.be.false;
  296. } );
  297. it( 'should return false if given position has different root', () => {
  298. const position = new Position( root, [ 1, 1, 2 ] );
  299. const differentPosition = new Position( otherRoot, [ 1, 0 ] );
  300. expect( position.isAfter( differentPosition ) ).to.be.false;
  301. } );
  302. } );
  303. describe( 'isEqual()', () => {
  304. it( 'should return true if given position has same path and root', () => {
  305. const position = new Position( root, [ 1, 1, 2 ] );
  306. const samePosition = new Position( root, [ 1, 1, 2 ] );
  307. expect( position.isEqual( samePosition ) ).to.be.true;
  308. } );
  309. it( 'should return false if given position has different path', () => {
  310. const position = new Position( root, [ 1, 1, 1 ] );
  311. const differentPosition = new Position( root, [ 1, 2, 2 ] );
  312. expect( position.isEqual( differentPosition ) ).to.be.false;
  313. } );
  314. it( 'should return false if given position has different root', () => {
  315. const position = new Position( root, [ 1, 1, 1 ] );
  316. const differentPosition = new Position( otherRoot, [ 1, 1, 1 ] );
  317. expect( position.isEqual( differentPosition ) ).to.be.false;
  318. } );
  319. } );
  320. describe( 'isAfter()', () => {
  321. it( 'should return true if given position has same root and is after this position', () => {
  322. const position = new Position( root, [ 1, 1, 2 ] );
  323. const afterPosition = new Position( root, [ 1, 2 ] );
  324. expect( position.isBefore( afterPosition ) ).to.be.true;
  325. } );
  326. it( 'should return false if given position has same root and is not after this position', () => {
  327. const position = new Position( root, [ 1, 1, 2 ] );
  328. const beforePosition = new Position( root, [ 1, 0 ] );
  329. expect( position.isBefore( beforePosition ) ).to.be.false;
  330. } );
  331. it( 'should return false if given position has different root', () => {
  332. const position = new Position( root, [ 1, 1, 2 ] );
  333. const differentPosition = new Position( otherRoot, [ 1, 2 ] );
  334. expect( position.isBefore( differentPosition ) ).to.be.false;
  335. } );
  336. } );
  337. describe( 'isTouching()', () => {
  338. it( 'should return true if positions are same', () => {
  339. const position = new Position( root, [ 1, 1, 1 ] );
  340. const result = position.isTouching( new Position( root, [ 1, 1, 1 ] ) );
  341. expect( result ).to.be.true;
  342. } );
  343. it( 'should return true if given position is in next node and there are no whole nodes before it', () => {
  344. const positionA = new Position( root, [ 1 ] );
  345. const positionB = new Position( root, [ 1, 0, 0 ] );
  346. expect( positionA.isTouching( positionB ) ).to.be.true;
  347. expect( positionB.isTouching( positionA ) ).to.be.true;
  348. } );
  349. it( 'should return true if given position is in previous node and there are no whole nodes after it', () => {
  350. const positionA = new Position( root, [ 2 ] );
  351. const positionB = new Position( root, [ 1, 1, 3 ] );
  352. expect( positionA.isTouching( positionB ) ).to.be.true;
  353. expect( positionB.isTouching( positionA ) ).to.be.true;
  354. } );
  355. it( 'should return true if positions are in different sub-trees but there are no whole nodes between them', () => {
  356. const positionA = new Position( root, [ 1, 0, 3 ] );
  357. const positionB = new Position( root, [ 1, 1, 0 ] );
  358. expect( positionA.isTouching( positionB ) ).to.be.true;
  359. expect( positionB.isTouching( positionA ) ).to.be.true;
  360. } );
  361. it( 'should return false if there are whole nodes between positions', () => {
  362. const positionA = new Position( root, [ 2 ] );
  363. const positionB = new Position( root, [ 1, 0, 3 ] );
  364. expect( positionA.isTouching( positionB ) ).to.be.false;
  365. expect( positionB.isTouching( positionA ) ).to.be.false;
  366. } );
  367. it( 'should return false if there are whole nodes between positions (same depth)', () => {
  368. const positionA = new Position( root, [ 1, 0 ] );
  369. const positionB = new Position( root, [ 1, 1 ] );
  370. expect( positionA.isTouching( positionB ) ).to.be.false;
  371. expect( positionB.isTouching( positionA ) ).to.be.false;
  372. } );
  373. it( 'should return false if there are whole nodes between positions', () => {
  374. const positionA = new Position( root, [ 1, 0, 3 ] );
  375. const positionB = new Position( root, [ 1, 1, 1 ] );
  376. expect( positionA.isTouching( positionB ) ).to.be.false;
  377. expect( positionB.isTouching( positionA ) ).to.be.false;
  378. } );
  379. it( 'should return false if positions are in different roots', () => {
  380. const positionA = new Position( root, [ 1, 0, 3 ] );
  381. const positionB = new Position( otherRoot, [ 1, 1, 0 ] );
  382. expect( positionA.isTouching( positionB ) ).to.be.false;
  383. expect( positionB.isTouching( positionA ) ).to.be.false;
  384. } );
  385. } );
  386. describe( 'hasSameParentAs()', () => {
  387. it( 'should return false if positions have different roots', () => {
  388. const posA = new Position( root, [ 1, 2 ] );
  389. const posB = new Position( doc.graveyard, [ 1, 0 ] );
  390. expect( posA.hasSameParentAs( posB ) ).to.be.false;
  391. expect( posB.hasSameParentAs( posA ) ).to.be.false;
  392. } );
  393. it( 'should return false if positions have different parents', () => {
  394. const posA = new Position( root, [ 0, 1 ] );
  395. const posB = new Position( root, [ 1, 1 ] );
  396. expect( posA.hasSameParentAs( posB ) ).to.be.false;
  397. expect( posB.hasSameParentAs( posA ) ).to.be.false;
  398. } );
  399. it( 'should return true if positions have same parent', () => {
  400. const posA = new Position( root, [ 0, 4, 8 ] );
  401. const posB = new Position( root, [ 0, 4, 2 ] );
  402. expect( posA.hasSameParentAs( posB ) ).to.be.true;
  403. expect( posB.hasSameParentAs( posA ) ).to.be.true;
  404. } );
  405. } );
  406. describe( 'isAtStart()', () => {
  407. it( 'should return true if position is at the beginning of its parent', () => {
  408. expect( new Position( root, [ 0 ] ).isAtStart ).to.be.true;
  409. expect( new Position( root, [ 1 ] ).isAtStart ).to.be.false;
  410. } );
  411. } );
  412. describe( 'isAtEnd()', () => {
  413. it( 'should return true if position is at the end of its parent', () => {
  414. expect( new Position( root, [ root.maxOffset ] ).isAtEnd ).to.be.true;
  415. expect( new Position( root, [ 0 ] ).isAtEnd ).to.be.false;
  416. } );
  417. } );
  418. describe( 'getAncestors()', () => {
  419. it( 'should return position parent element and it\'s ancestors', () => {
  420. expect( new Position( root, [ 1, 1, 1 ] ).getAncestors() ).to.deep.equal( [ root, ul, li2 ] );
  421. } );
  422. it( 'should return DocumentFragment if position is directly in document fragment', () => {
  423. const docFrag = new DocumentFragment();
  424. expect( new Position( docFrag, [ 0 ] ).getAncestors() ).to.deep.equal( [ docFrag ] );
  425. } );
  426. } );
  427. describe( 'getCommonPath()', () => {
  428. it( 'returns the common part', () => {
  429. const pos1 = new Position( root, [ 1, 0, 0 ] );
  430. const pos2 = new Position( root, [ 1, 0, 1 ] );
  431. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [ 1, 0 ] );
  432. } );
  433. it( 'returns the common part when paths are equal', () => {
  434. const pos1 = new Position( root, [ 1, 0, 1 ] );
  435. const pos2 = new Position( root, [ 1, 0, 1 ] );
  436. const commonPath = pos1.getCommonPath( pos2 );
  437. // Ensure that we have a clone
  438. expect( commonPath ).to.not.equal( pos1.path );
  439. expect( commonPath ).to.not.equal( pos2.path );
  440. expect( commonPath ).to.deep.equal( [ 1, 0, 1 ] );
  441. } );
  442. it( 'returns empty array when paths totally differ', () => {
  443. const pos1 = new Position( root, [ 1, 1 ] );
  444. const pos2 = new Position( root, [ 0 ] );
  445. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
  446. } );
  447. it( 'returns empty array when roots differ, but paths are the same', () => {
  448. const pos1 = new Position( root, [ 1, 1 ] );
  449. const pos2 = new Position( otherRoot, [ 1, 1 ] );
  450. expect( pos1.getCommonPath( pos2 ) ).to.deep.equal( [] );
  451. } );
  452. } );
  453. describe( 'compareWith()', () => {
  454. it( 'should return same if positions are same', () => {
  455. const position = new Position( root, [ 1, 2, 3 ] );
  456. const compared = new Position( root, [ 1, 2, 3 ] );
  457. expect( position.compareWith( compared ) ).to.equal( 'same' );
  458. } );
  459. it( 'should return before if the position is before compared one', () => {
  460. const position = new Position( root, [ 1, 2, 3 ] );
  461. const compared = new Position( root, [ 1, 3 ] );
  462. expect( position.compareWith( compared ) ).to.equal( 'before' );
  463. } );
  464. it( 'should return after if the position is after compared one', () => {
  465. const position = new Position( root, [ 1, 2, 3, 4 ] );
  466. const compared = new Position( root, [ 1, 2, 3 ] );
  467. expect( position.compareWith( compared ) ).to.equal( 'after' );
  468. } );
  469. it( 'should return different if positions are in different roots', () => {
  470. const position = new Position( root, [ 1, 2, 3 ] );
  471. const compared = new Position( otherRoot, [ 1, 2, 3 ] );
  472. expect( position.compareWith( compared ) ).to.equal( 'different' );
  473. } );
  474. } );
  475. describe( 'getLastMatchingPosition()', () => {
  476. it( 'should skip forward', () => {
  477. let position = new Position( root, [ 1, 0, 0 ] );
  478. position = position.getLastMatchingPosition( value => value.type == 'text' );
  479. expect( position.path ).to.deep.equal( [ 1, 0, 3 ] );
  480. } );
  481. it( 'should skip backward', () => {
  482. let position = new Position( root, [ 1, 0, 2 ] );
  483. position = position.getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
  484. expect( position.path ).to.deep.equal( [ 1, 0, 0 ] );
  485. } );
  486. } );
  487. // Note: We don't create model element structure in these tests because this method
  488. // is used by OT so it must not check the structure.
  489. describe( 'getTransformedByOperation()', () => {
  490. let pos;
  491. beforeEach( () => {
  492. pos = new Position( root, [ 3, 2 ] );
  493. } );
  494. describe( 'by AttributeOperation', () => {
  495. it( 'nothing should change', () => {
  496. const op = new AttributeOperation( Range.createFromParentsAndOffsets( root, 1, root, 6 ), 'key', true, false, 1 );
  497. const transformed = pos.getTransformedByOperation( op );
  498. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  499. } );
  500. } );
  501. describe( 'by InsertOperation', () => {
  502. it( 'should use _getTransformedByInsertion', () => {
  503. sinon.spy( pos, '_getTransformedByInsertion' );
  504. const op = new InsertOperation( new Position( root, [ 1 ] ), [ new Element( 'paragraph' ) ], 1 );
  505. pos.getTransformedByOperation( op );
  506. expect( pos._getTransformedByInsertion.calledWithExactly( op.position, op.howMany ) ).to.be.true;
  507. } );
  508. } );
  509. describe( 'by MarkerOperation', () => {
  510. it( 'nothing should change', () => {
  511. const op = new MarkerOperation(
  512. 'marker', null, Range.createFromParentsAndOffsets( root, 1, root, 6 ), model.markers, true, 1
  513. );
  514. const transformed = pos.getTransformedByOperation( op );
  515. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  516. } );
  517. } );
  518. describe( 'by MoveOperation', () => {
  519. it( 'should use _getTransformedByMove', () => {
  520. sinon.spy( pos, '_getTransformedByMove' );
  521. const op = new MoveOperation( new Position( root, [ 1 ] ), 2, new Position( root, [ 5 ] ), 1 );
  522. pos.getTransformedByOperation( op );
  523. expect( pos._getTransformedByMove.calledWithExactly( op.sourcePosition, op.targetPosition, op.howMany ) ).to.be.true;
  524. } );
  525. } );
  526. describe( 'by RenameOperation', () => {
  527. it( 'nothing should change', () => {
  528. const op = new RenameOperation( new Position( root, [ 3 ] ), 'old', 'new', 1 );
  529. const transformed = pos.getTransformedByOperation( op );
  530. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  531. } );
  532. } );
  533. describe( 'by SplitOperation', () => {
  534. it( 'transformed position is at the split position', () => {
  535. const op = new SplitOperation( new Position( root, [ 3, 2 ] ), 3, null, 1 );
  536. const transformed = pos.getTransformedByOperation( op );
  537. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  538. } );
  539. it( 'transformed position is after the split position', () => {
  540. const op = new SplitOperation( new Position( root, [ 3, 1 ] ), 3, null, 1 );
  541. const transformed = pos.getTransformedByOperation( op );
  542. expect( transformed.path ).to.deep.equal( [ 4, 1 ] );
  543. } );
  544. it( 'transformed position is before the split position', () => {
  545. const op = new SplitOperation( new Position( root, [ 3, 3 ] ), 3, null, 1 );
  546. const transformed = pos.getTransformedByOperation( op );
  547. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  548. } );
  549. it( 'transformed position is after the split element', () => {
  550. const op = new SplitOperation( new Position( root, [ 3, 1, 5 ] ), 3, null, 1 );
  551. const transformed = pos.getTransformedByOperation( op );
  552. expect( transformed.path ).to.deep.equal( [ 3, 3 ] );
  553. } );
  554. it( 'transformed position is before the split element', () => {
  555. const op = new SplitOperation( new Position( root, [ 3, 3, 5 ] ), 3, null, 1 );
  556. const transformed = pos.getTransformedByOperation( op );
  557. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  558. } );
  559. it( 'transformed position is in graveyard and split position uses graveyard element', () => {
  560. pos = new Position( doc.graveyard, [ 1 ] );
  561. const op = new SplitOperation( new Position( root, [ 3, 2 ] ), 3, new Position( doc.graveyard, [ 0 ] ), 1 );
  562. const transformed = pos.getTransformedByOperation( op );
  563. expect( transformed.path ).to.deep.equal( [ 0 ] );
  564. } );
  565. } );
  566. describe( 'by MergeOperation', () => {
  567. it( 'position is inside merged element', () => {
  568. const op = new MergeOperation(
  569. new Position( root, [ 3, 0 ] ), 3, new Position( root, [ 2, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  570. );
  571. const transformed = pos.getTransformedByOperation( op );
  572. expect( transformed.path ).to.deep.equal( [ 2, 4 ] );
  573. } );
  574. it( 'position is inside merged-to element', () => {
  575. const op = new MergeOperation(
  576. new Position( root, [ 4, 0 ] ), 3, new Position( root, [ 3, 5 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  577. );
  578. const transformed = pos.getTransformedByOperation( op );
  579. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  580. } );
  581. it( 'position is before merged element', () => {
  582. const op = new MergeOperation(
  583. new Position( root, [ 3, 2, 0 ] ), 3, new Position( root, [ 3, 1, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  584. );
  585. const transformed = pos.getTransformedByOperation( op );
  586. expect( transformed.path ).to.deep.equal( [ 3, 2 ] );
  587. } );
  588. it( 'position is after merged element', () => {
  589. const op = new MergeOperation(
  590. new Position( root, [ 3, 1, 0 ] ), 3, new Position( root, [ 3, 0, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  591. );
  592. const transformed = pos.getTransformedByOperation( op );
  593. expect( transformed.path ).to.deep.equal( [ 3, 1 ] );
  594. } );
  595. it( 'position is inside graveyard', () => {
  596. pos = new Position( doc.graveyard, [ 0 ] );
  597. const op = new MergeOperation(
  598. new Position( root, [ 3, 1, 0 ] ), 3, new Position( root, [ 3, 0, 2 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  599. );
  600. const transformed = pos.getTransformedByOperation( op );
  601. expect( transformed.path ).to.deep.equal( [ 1 ] );
  602. } );
  603. it( 'merge source position is before merge target position and position is in merged element', () => {
  604. const op = new MergeOperation(
  605. new Position( root, [ 3, 0 ] ), 3, new Position( root, [ 4, 5 ] ), new Position( doc.graveyard, [ 0 ] ), 1
  606. );
  607. const transformed = pos.getTransformedByOperation( op );
  608. expect( transformed.path ).to.deep.equal( [ 3, 7 ] );
  609. } );
  610. } );
  611. } );
  612. describe( '_getTransformedByInsertion()', () => {
  613. it( 'should return a new Position instance', () => {
  614. const position = new Position( root, [ 0 ] );
  615. const transformed = position._getTransformedByInsertion( new Position( root, [ 2 ] ), 4 );
  616. expect( transformed ).not.to.equal( position );
  617. expect( transformed ).to.be.instanceof( Position );
  618. } );
  619. it( 'should increment offset if insertion is in the same parent and closer offset', () => {
  620. const position = new Position( root, [ 1, 2, 3 ] );
  621. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 2 ] ), 2 );
  622. expect( transformed.offset ).to.equal( 5 );
  623. } );
  624. it( 'should not increment offset if insertion position is in different root', () => {
  625. const position = new Position( root, [ 1, 2, 3 ] );
  626. const transformed = position._getTransformedByInsertion( new Position( otherRoot, [ 1, 2, 2 ] ), 2 );
  627. expect( transformed.offset ).to.equal( 3 );
  628. } );
  629. it( 'should increment offset if insertion is in the same parent and the same offset', () => {
  630. const position = new Position( root, [ 1, 2, 3 ] );
  631. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2 );
  632. expect( transformed.offset ).to.equal( 5 );
  633. } );
  634. it( 'should increment offset if insertion is in the same parent and the same offset and it is inserted before', () => {
  635. const position = new Position( root, [ 1, 2, 3 ] );
  636. position.stickiness = 'toNext';
  637. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 3 ] ), 2 );
  638. expect( transformed.offset ).to.equal( 5 );
  639. } );
  640. it( 'should not increment offset if insertion is in the same parent and further offset', () => {
  641. const position = new Position( root, [ 1, 2, 3 ] );
  642. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2, 4 ] ), 2 );
  643. expect( transformed.offset ).to.equal( 3 );
  644. } );
  645. it( 'should update path if insertion position parent is a node from that path and offset is before next node on that path', () => {
  646. const position = new Position( root, [ 1, 2, 3 ] );
  647. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 2 ] ), 2 );
  648. expect( transformed.path ).to.deep.equal( [ 1, 4, 3 ] );
  649. } );
  650. it( 'should not update path if insertion position parent is a node from that path and offset is ' +
  651. 'after next node on that path', () => {
  652. const position = new Position( root, [ 1, 2, 3 ] );
  653. const transformed = position._getTransformedByInsertion( new Position( root, [ 1, 3 ] ), 2 );
  654. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  655. } );
  656. it( 'should not update if insertion is in different path', () => {
  657. const position = new Position( root, [ 1, 1 ] );
  658. const transformed = position._getTransformedByInsertion( new Position( root, [ 2, 0 ] ), 2 );
  659. expect( transformed.path ).to.deep.equal( [ 1, 1 ] );
  660. } );
  661. } );
  662. describe( '_getTransformedByDeletion()', () => {
  663. it( 'should return a new Position instance', () => {
  664. const position = new Position( root, [ 0 ] );
  665. const transformed = position._getTransformedByDeletion( new Position( root, [ 2 ] ), 4 );
  666. expect( transformed ).not.to.equal( position );
  667. expect( transformed ).to.be.instanceof( Position );
  668. } );
  669. it( 'should return null if original position is inside one of removed nodes', () => {
  670. const position = new Position( root, [ 1, 2 ] );
  671. const transformed = position._getTransformedByDeletion( new Position( root, [ 0 ] ), 2 );
  672. expect( transformed ).to.be.null;
  673. } );
  674. it( 'should decrement offset if deletion is in the same parent and closer offset', () => {
  675. const position = new Position( root, [ 1, 2, 7 ] );
  676. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 2 ] ), 2 );
  677. expect( transformed.offset ).to.equal( 5 );
  678. } );
  679. it( 'should return null if original position is between removed nodes', () => {
  680. const position = new Position( root, [ 1, 2, 4 ] );
  681. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 3 ] ), 5 );
  682. expect( transformed ).to.be.null;
  683. } );
  684. it( 'should not decrement offset if deletion position is in different root', () => {
  685. const position = new Position( root, [ 1, 2, 3 ] );
  686. const transformed = position._getTransformedByDeletion( new Position( otherRoot, [ 1, 2, 1 ] ), 2 );
  687. expect( transformed.offset ).to.equal( 3 );
  688. } );
  689. it( 'should not decrement offset if deletion is in the same parent and further offset', () => {
  690. const position = new Position( root, [ 1, 2, 3 ] );
  691. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 2, 4 ] ), 2 );
  692. expect( transformed.offset ).to.equal( 3 );
  693. } );
  694. it( 'should update path if deletion position parent is a node from that path and offset is before next node on that path', () => {
  695. const position = new Position( root, [ 1, 2, 3 ] );
  696. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 0 ] ), 2 );
  697. expect( transformed.path ).to.deep.equal( [ 1, 0, 3 ] );
  698. } );
  699. it( 'should not update path if deletion position parent is a node from that path and ' +
  700. 'offset is after next node on that path', () => {
  701. const position = new Position( root, [ 1, 2, 3 ] );
  702. const transformed = position._getTransformedByDeletion( new Position( root, [ 1, 3 ] ), 2 );
  703. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  704. } );
  705. } );
  706. describe( '_getTransformedByMove()', () => {
  707. it( 'should increment offset if a range was moved to the same parent and closer offset', () => {
  708. const position = new Position( root, [ 1, 2, 3 ] );
  709. const transformed = position._getTransformedByMove( new Position( root, [ 2 ] ), new Position( root, [ 1, 2, 0 ] ), 3, false );
  710. expect( transformed.path ).to.deep.equal( [ 1, 2, 6 ] );
  711. } );
  712. it( 'should decrement offset if a range was moved from the same parent and closer offset', () => {
  713. const position = new Position( root, [ 1, 2, 6 ] );
  714. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  715. expect( transformed.path ).to.deep.equal( [ 1, 2, 3 ] );
  716. } );
  717. it( 'should decrement offset if position was at the end of a range and move was not sticky', () => {
  718. const position = new Position( root, [ 1, 2, 3 ] );
  719. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  720. expect( transformed.path ).to.deep.equal( [ 1, 2, 0 ] );
  721. } );
  722. it( 'should update path if position was at the end of a range and move was sticky', () => {
  723. const position = new Position( root, [ 1, 2, 3 ] );
  724. position.stickiness = 'toPrevious';
  725. const transformed = position._getTransformedByMove( new Position( root, [ 1, 2, 0 ] ), new Position( root, [ 2 ] ), 3, false );
  726. expect( transformed.path ).to.deep.equal( [ 5 ] );
  727. } );
  728. it( 'should update path if a range contained this position', () => {
  729. const position = new Position( root, [ 1, 2, 3 ] );
  730. const transformed = position._getTransformedByMove( new Position( root, [ 1, 1 ] ), new Position( root, [ 2, 1 ] ), 3, false );
  731. expect( transformed.path ).to.deep.equal( [ 2, 2, 3 ] );
  732. } );
  733. it( 'should not update if targetPosition is equal to sourcePosition (because nothing is really moving)', () => {
  734. const position = new Position( root, [ 3 ] );
  735. const transformed = position._getTransformedByMove( new Position( root, [ 3 ] ), new Position( root, [ 3 ] ), 3, false );
  736. expect( transformed.path ).to.deep.equal( [ 3 ] );
  737. } );
  738. it( 'should update if position is before moved range and sticks to next node', () => {
  739. const position = new Position( root, [ 2, 1 ] );
  740. position.stickiness = 'toNext';
  741. const transformed = position._getTransformedByMove( new Position( root, [ 2, 1 ] ), new Position( root, [ 3, 3 ] ), 2, false );
  742. expect( transformed.path ).to.deep.equal( [ 3, 3 ] );
  743. } );
  744. } );
  745. describe( '_getCombined()', () => {
  746. it( 'should return correct combination of this and given positions', () => {
  747. const position = new Position( root, [ 1, 3, 4, 2 ] );
  748. const sourcePosition = new Position( root, [ 1, 1 ] );
  749. const targetPosition = new Position( root, [ 2, 5 ] );
  750. const combined = position._getCombined( sourcePosition, targetPosition );
  751. expect( combined.path ).to.deep.equal( [ 2, 7, 4, 2 ] );
  752. } );
  753. } );
  754. describe( 'getShiftedBy()', () => {
  755. it( 'should return a new instance of Position with offset changed by shift value', () => {
  756. const position = new Position( root, [ 1, 2, 3 ] );
  757. const shifted = position.getShiftedBy( 2 );
  758. expect( shifted ).to.be.instanceof( Position );
  759. expect( shifted ).to.not.equal( position );
  760. expect( shifted.path ).to.deep.equal( [ 1, 2, 5 ] );
  761. } );
  762. it( 'should accept negative values', () => {
  763. const position = new Position( root, [ 1, 2, 3 ] );
  764. const shifted = position.getShiftedBy( -2 );
  765. expect( shifted.path ).to.deep.equal( [ 1, 2, 1 ] );
  766. } );
  767. it( 'should not let setting offset lower than zero', () => {
  768. const position = new Position( root, [ 1, 2, 3 ] );
  769. const shifted = position.getShiftedBy( -7 );
  770. expect( shifted.path ).to.deep.equal( [ 1, 2, 0 ] );
  771. } );
  772. } );
  773. describe( 'toJSON()', () => {
  774. it( 'should serialize position', () => {
  775. const position = new Position( root, [ 0 ] );
  776. const serialized = position.toJSON();
  777. expect( serialized ).to.deep.equal( { root: 'main', path: [ 0 ], stickiness: 'toNone' } );
  778. } );
  779. it( 'should serialize position from graveyard', () => {
  780. const position = new Position( doc.graveyard, [ 0 ] );
  781. position.stickiness = 'toPrevious';
  782. const serialized = position.toJSON();
  783. expect( serialized ).to.deep.equal( { root: '$graveyard', path: [ 0 ], stickiness: 'toPrevious' } );
  784. } );
  785. } );
  786. describe( 'fromJSON()', () => {
  787. it( 'should create object with given document', () => {
  788. const deserialized = Position.fromJSON( { root: 'main', path: [ 0, 1, 2 ] }, doc );
  789. expect( deserialized.root ).to.equal( root );
  790. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  791. } );
  792. it( 'should create object from graveyard', () => {
  793. const deserialized = Position.fromJSON( { root: '$graveyard', path: [ 0, 1, 2 ] }, doc );
  794. expect( deserialized.root ).to.equal( doc.graveyard );
  795. expect( deserialized.path ).to.deep.equal( [ 0, 1, 2 ] );
  796. } );
  797. it( 'should throw error when creating object in document that does not have provided root', () => {
  798. expect(
  799. () => {
  800. Position.fromJSON( { root: 'noroot', path: [ 0 ] }, doc );
  801. }
  802. ).to.throw( CKEditorError, /model-position-fromjson-no-root/ );
  803. } );
  804. } );
  805. describe( 'getCommonAncestor()', () => {
  806. it( 'returns null when roots of both positions are not the same', () => {
  807. const pos1 = new Position( root, [ 0 ] );
  808. const pos2 = new Position( otherRoot, [ 0 ] );
  809. test( pos1, pos2, null );
  810. } );
  811. it( 'for two the same positions returns the parent element #1', () => {
  812. const fPosition = new Position( root, [ 1, 0, 0 ] );
  813. const otherPosition = new Position( root, [ 1, 0, 0 ] );
  814. test( fPosition, otherPosition, li1 );
  815. } );
  816. it( 'for two the same positions returns the parent element #2', () => {
  817. const model = new Model();
  818. const doc = model.document;
  819. const root = doc.createRoot();
  820. const p = new Element( 'p', null, 'foobar' );
  821. root._appendChild( p );
  822. const postion = new Position( root, [ 0, 3 ] ); // <p>foo^bar</p>
  823. test( postion, postion, p );
  824. } );
  825. it( 'for two positions in the same element returns the element', () => {
  826. const fPosition = new Position( root, [ 1, 0, 0 ] );
  827. const zPosition = new Position( root, [ 1, 0, 2 ] );
  828. test( fPosition, zPosition, li1 );
  829. } );
  830. it( 'works when one positions is nested deeper than the other', () => {
  831. const zPosition = new Position( root, [ 1, 0, 2 ] );
  832. const liPosition = new Position( root, [ 1, 1 ] );
  833. test( liPosition, zPosition, ul );
  834. } );
  835. // Checks if by mistake someone didn't use getCommonPath() + getNodeByPath().
  836. it( 'works if position is located before an element', () => {
  837. const model = new Model();
  838. const doc = model.document;
  839. const root = doc.createRoot();
  840. const p = new Element( 'p', null, new Element( 'a' ) );
  841. root._appendChild( p );
  842. const postion = new Position( root, [ 0, 0 ] ); // <p>^<a></a></p>
  843. test( postion, postion, p );
  844. } );
  845. it( 'works fine with positions located in DocumentFragment', () => {
  846. const docFrag = new DocumentFragment( [ p, ul ] );
  847. const zPosition = new Position( docFrag, [ 1, 0, 2 ] );
  848. const afterLiPosition = new Position( docFrag, [ 1, 2 ] );
  849. test( zPosition, afterLiPosition, ul );
  850. } );
  851. function test( positionA, positionB, lca ) {
  852. expect( positionA.getCommonAncestor( positionB ) ).to.equal( lca );
  853. expect( positionB.getCommonAncestor( positionA ) ).to.equal( lca );
  854. }
  855. } );
  856. } );