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