8
0

position.js 46 KB

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