position.js 44 KB

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