position.js 46 KB

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