position.js 49 KB

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