position.js 45 KB

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