position.js 49 KB

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