position.js 50 KB

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