position.js 46 KB

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