position.js 45 KB

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