position.js 48 KB

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