8
0

position.js 45 KB

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