position.js 46 KB

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