transform.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  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 { transform } from '../../../src/model/operation/transform';
  6. import Model from '../../../src/model/model';
  7. import RootElement from '../../../src/model/rootelement';
  8. import Node from '../../../src/model/node';
  9. import Position from '../../../src/model/position';
  10. import Range from '../../../src/model/range';
  11. import InsertOperation from '../../../src/model/operation/insertoperation';
  12. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  13. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  14. import MarkerOperation from '../../../src/model/operation/markeroperation';
  15. import MoveOperation from '../../../src/model/operation/moveoperation';
  16. import RenameOperation from '../../../src/model/operation/renameoperation';
  17. import NoOperation from '../../../src/model/operation/nooperation';
  18. describe( 'transform', () => {
  19. let model, doc, root, op, nodeA, nodeB, expected;
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = model.document;
  23. root = doc.createRoot();
  24. nodeA = new Node();
  25. nodeB = new Node();
  26. } );
  27. afterEach( () => {
  28. sinon.restore();
  29. } );
  30. function expectOperation( op, params ) {
  31. for ( const i in params ) {
  32. if ( params.hasOwnProperty( i ) ) {
  33. if ( i == 'type' ) {
  34. expect( op, 'type' ).to.be.instanceof( params[ i ] );
  35. }
  36. else if ( params[ i ] instanceof Array ) {
  37. expect( op[ i ].length, i ).to.equal( params[ i ].length );
  38. for ( let j = 0; j < params[ i ].length; j++ ) {
  39. expect( op[ i ][ j ] ).to.equal( params[ i ][ j ] );
  40. }
  41. } else if ( params[ i ] instanceof Position || params[ i ] instanceof Range ) {
  42. expect( op[ i ].isEqual( params[ i ] ), i ).to.be.true;
  43. } else {
  44. expect( op[ i ], i ).to.equal( params[ i ] );
  45. }
  46. }
  47. }
  48. }
  49. const strongContext = {
  50. aIsStrong: true
  51. };
  52. it( 'should throw an error when one of operations is invalid', () => {
  53. const nodeA = new Node();
  54. const nodeB = new Node();
  55. const position = new Position( root, [ 0 ] );
  56. const a = new InsertOperation( position, [ nodeA ], 0 );
  57. const b = new InsertOperation( position, [ nodeB ], 0 );
  58. // Modify an operation so it will throw an error.
  59. a.position = null;
  60. expect( () => {
  61. transform( a, b, {
  62. aIsStrong: true,
  63. aWasUndone: false,
  64. bWasUndone: false,
  65. abRelation: null,
  66. baRelation: null
  67. } );
  68. } ).to.throw();
  69. } );
  70. describe( 'InsertOperation', () => {
  71. let nodeC, nodeD, position;
  72. beforeEach( () => {
  73. nodeC = new Node();
  74. nodeD = new Node();
  75. position = new Position( root, [ 0, 2, 2 ] );
  76. op = new InsertOperation( position, [ nodeA, nodeB ], 0 );
  77. expected = {
  78. type: InsertOperation,
  79. position: position.clone()
  80. };
  81. } );
  82. describe( 'by InsertOperation', () => {
  83. it( 'target at different position: no position update', () => {
  84. const transformBy = new InsertOperation(
  85. new Position( root, [ 1, 3, 2 ] ),
  86. [ nodeC, nodeD ],
  87. 0
  88. );
  89. const transOp = transform( op, transformBy );
  90. expect( transOp.length ).to.equal( 1 );
  91. expectOperation( transOp[ 0 ], expected );
  92. } );
  93. it( 'target at offset before: increment offset', () => {
  94. const transformBy = new InsertOperation(
  95. new Position( root, [ 0, 2, 0 ] ),
  96. [ nodeC, nodeD ],
  97. 0
  98. );
  99. const transOp = transform( op, transformBy );
  100. expected.position.offset += 2;
  101. expect( transOp.length ).to.equal( 1 );
  102. expectOperation( transOp[ 0 ], expected );
  103. } );
  104. it( 'target at same offset and is important: increment offset', () => {
  105. const transformBy = new InsertOperation(
  106. new Position( root, [ 0, 2, 2 ] ),
  107. [ nodeC, nodeD ],
  108. 0
  109. );
  110. const transOp = transform( op, transformBy );
  111. expected.position.offset += 2;
  112. expect( transOp.length ).to.equal( 1 );
  113. expectOperation( transOp[ 0 ], expected );
  114. } );
  115. it( 'target at same offset and is less important: no position update', () => {
  116. const transformBy = new InsertOperation(
  117. new Position( root, [ 0, 2, 2 ] ),
  118. [ nodeC, nodeD ],
  119. 0
  120. );
  121. const transOp = transform( op, transformBy, strongContext );
  122. expect( transOp.length ).to.equal( 1 );
  123. expectOperation( transOp[ 0 ], expected );
  124. } );
  125. it( 'target at same offset and context.insertBefore = false: increment offset', () => {
  126. const transformBy = new InsertOperation(
  127. new Position( root, [ 0, 2, 2 ] ),
  128. [ nodeC, nodeD ],
  129. 0
  130. );
  131. const transOp = transform( op, transformBy );
  132. expected.position.offset += 2;
  133. expect( transOp.length ).to.equal( 1 );
  134. expectOperation( transOp[ 0 ], expected );
  135. } );
  136. it( 'target at offset after: no position update', () => {
  137. const transformBy = new InsertOperation(
  138. new Position( root, [ 0, 2, 3 ] ),
  139. [ nodeC, nodeD ],
  140. 0
  141. );
  142. const transOp = transform( op, transformBy );
  143. expect( transOp.length ).to.equal( 1 );
  144. expectOperation( transOp[ 0 ], expected );
  145. } );
  146. it( 'target before node from path: increment index on path', () => {
  147. const transformBy = new InsertOperation(
  148. new Position( root, [ 0, 1 ] ),
  149. [ nodeC, nodeD ],
  150. 0
  151. );
  152. const transOp = transform( op, transformBy );
  153. expected.position.path[ 1 ] += 2;
  154. expect( transOp.length ).to.equal( 1 );
  155. expectOperation( transOp[ 0 ], expected );
  156. } );
  157. it( 'target after node from path: no position update', () => {
  158. const transformBy = new InsertOperation(
  159. new Position( root, [ 0, 6 ] ),
  160. [ nodeC, nodeD ],
  161. 0
  162. );
  163. const transOp = transform( op, transformBy );
  164. expect( transOp.length ).to.equal( 1 );
  165. expectOperation( transOp[ 0 ], expected );
  166. } );
  167. } );
  168. describe( 'by AttributeOperation', () => {
  169. it( 'no position update', () => {
  170. const transformBy = new AttributeOperation(
  171. Range._createFromPositionAndShift( position, 2 ),
  172. 'foo',
  173. null,
  174. 'bar',
  175. 0
  176. );
  177. const transOp = transform( op, transformBy );
  178. expect( transOp.length ).to.equal( 1 );
  179. expectOperation( transOp[ 0 ], expected );
  180. } );
  181. } );
  182. describe( 'by RootAttributeOperation', () => {
  183. it( 'no position update', () => {
  184. const transformBy = new RootAttributeOperation(
  185. root,
  186. 'foo',
  187. null,
  188. 'bar',
  189. 0
  190. );
  191. const transOp = transform( op, transformBy );
  192. expect( transOp.length ).to.equal( 1 );
  193. expectOperation( transOp[ 0 ], expected );
  194. } );
  195. } );
  196. describe( 'by MoveOperation', () => {
  197. it( 'range and target are different than insert position: no position update', () => {
  198. const transformBy = new MoveOperation(
  199. new Position( root, [ 1, 3, 2 ] ),
  200. 2,
  201. new Position( root, [ 2, 1 ] ),
  202. 0
  203. );
  204. const transOp = transform( op, transformBy );
  205. expect( transOp.length ).to.equal( 1 );
  206. expectOperation( transOp[ 0 ], expected );
  207. } );
  208. it( 'range is before insert position offset: decrement offset', () => {
  209. const transformBy = new MoveOperation(
  210. new Position( root, [ 0, 2, 0 ] ),
  211. 1,
  212. new Position( root, [ 1, 1 ] ),
  213. 0
  214. );
  215. const transOp = transform( op, transformBy );
  216. expected.position.offset--;
  217. expect( transOp.length ).to.equal( 1 );
  218. expectOperation( transOp[ 0 ], expected );
  219. } );
  220. it( 'range offset is after insert position offset: no position update', () => {
  221. const transformBy = new MoveOperation(
  222. new Position( root, [ 0, 2, 5 ] ),
  223. 1,
  224. new Position( root, [ 1, 1 ] ),
  225. 0
  226. );
  227. const transOp = transform( op, transformBy );
  228. expect( transOp.length ).to.equal( 1 );
  229. expectOperation( transOp[ 0 ], expected );
  230. } );
  231. it( 'target offset before insert position offset: increment offset', () => {
  232. const transformBy = new MoveOperation(
  233. new Position( root, [ 1, 1 ] ),
  234. 2,
  235. new Position( root, [ 0, 2, 0 ] ),
  236. 0
  237. );
  238. const transOp = transform( op, transformBy );
  239. expected.position.offset += 2;
  240. expect( transOp.length ).to.equal( 1 );
  241. expectOperation( transOp[ 0 ], expected );
  242. } );
  243. it( 'target offset after insert position offset: no position update', () => {
  244. const transformBy = new MoveOperation(
  245. new Position( root, [ 1, 1 ] ),
  246. 2,
  247. new Position( root, [ 0, 2, 5 ] ),
  248. 0
  249. );
  250. const transOp = transform( op, transformBy );
  251. expect( transOp.length ).to.equal( 1 );
  252. expectOperation( transOp[ 0 ], expected );
  253. } );
  254. it( 'target offset same as insert position offset and is important: increment offset', () => {
  255. const transformBy = new MoveOperation(
  256. new Position( root, [ 1, 1 ] ),
  257. 2,
  258. new Position( root, [ 0, 2, 2 ] ),
  259. 0
  260. );
  261. const transOp = transform( op, transformBy );
  262. expected.position.offset += 2;
  263. expect( transOp.length ).to.equal( 1 );
  264. expectOperation( transOp[ 0 ], expected );
  265. } );
  266. it( 'target offset same as insert position offset and is less important: increment offset', () => {
  267. const transformBy = new MoveOperation(
  268. new Position( root, [ 1, 1 ] ),
  269. 2,
  270. new Position( root, [ 0, 2, 2 ] ),
  271. 0
  272. );
  273. const transOp = transform( op, transformBy, strongContext );
  274. expected.position.offset += 2;
  275. expect( transOp.length ).to.equal( 1 );
  276. expectOperation( transOp[ 0 ], expected );
  277. } );
  278. it( 'range is before node from insert position path: decrement index on path', () => {
  279. const transformBy = new MoveOperation(
  280. new Position( root, [ 0, 0 ] ),
  281. 1,
  282. new Position( root, [ 1, 0 ] ),
  283. 0
  284. );
  285. const transOp = transform( op, transformBy );
  286. expected.position.path[ 1 ] -= 1;
  287. expect( transOp.length ).to.equal( 1 );
  288. expectOperation( transOp[ 0 ], expected );
  289. } );
  290. it( 'range is after node from insert position path: no position update', () => {
  291. const transformBy = new MoveOperation(
  292. new Position( root, [ 0, 4 ] ),
  293. 2,
  294. new Position( root, [ 1, 0 ] ),
  295. 0
  296. );
  297. const transOp = transform( op, transformBy );
  298. expect( transOp.length ).to.equal( 1 );
  299. expectOperation( transOp[ 0 ], expected );
  300. } );
  301. it( 'target before node from insert position path: increment index on path', () => {
  302. const transformBy = new MoveOperation(
  303. new Position( root, [ 1, 0 ] ),
  304. 2,
  305. new Position( root, [ 0, 0 ] ),
  306. 0
  307. );
  308. const transOp = transform( op, transformBy );
  309. expected.position.path[ 1 ] += 2;
  310. expect( transOp.length ).to.equal( 1 );
  311. expectOperation( transOp[ 0 ], expected );
  312. } );
  313. it( 'target after node from insert position path: no position update', () => {
  314. const transformBy = new MoveOperation(
  315. new Position( root, [ 1, 0 ] ),
  316. 2,
  317. new Position( root, [ 0, 4 ] ),
  318. 0
  319. );
  320. const transOp = transform( op, transformBy );
  321. expect( transOp.length ).to.equal( 1 );
  322. expectOperation( transOp[ 0 ], expected );
  323. } );
  324. it( 'range has node that contains insert position: update position', () => {
  325. const transformBy = new MoveOperation(
  326. new Position( root, [ 0, 1 ] ),
  327. 2,
  328. new Position( root, [ 1, 1 ] ),
  329. 0
  330. );
  331. const transOp = transform( op, transformBy );
  332. expected.position.path = [ 1, 2, 2 ];
  333. expect( transOp.length ).to.equal( 1 );
  334. expectOperation( transOp[ 0 ], expected );
  335. } );
  336. it( 'range contains insert position (on same level): update position', () => {
  337. const transformBy = new MoveOperation(
  338. new Position( root, [ 0, 2, 0 ] ),
  339. 4,
  340. new Position( root, [ 1, 0 ] ),
  341. 0
  342. );
  343. const transOp = transform( op, transformBy );
  344. expected.position.path = [ 1, 2 ];
  345. expect( transOp.length ).to.equal( 1 );
  346. expectOperation( transOp[ 0 ], expected );
  347. } );
  348. } );
  349. describe( 'by NoOperation', () => {
  350. it( 'no operation update', () => {
  351. const transformBy = new NoOperation( 0 );
  352. const transOp = transform( op, transformBy );
  353. expect( transOp.length ).to.equal( 1 );
  354. expectOperation( transOp[ 0 ], expected );
  355. } );
  356. } );
  357. describe( 'by RenameOperation', () => {
  358. it( 'no position update', () => {
  359. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  360. const transOp = transform( op, transformBy );
  361. expect( transOp.length ).to.equal( 1 );
  362. expectOperation( transOp[ 0 ], expected );
  363. } );
  364. } );
  365. describe( 'by MarkerOperation', () => {
  366. it( 'no position update', () => {
  367. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  368. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  369. const transOp = transform( op, transformBy );
  370. expect( transOp.length ).to.equal( 1 );
  371. expectOperation( transOp[ 0 ], expected );
  372. } );
  373. } );
  374. } );
  375. describe( 'AttributeOperation', () => {
  376. let start, end, range;
  377. beforeEach( () => {
  378. start = new Position( root, [ 0, 2, 1 ] );
  379. end = new Position( root, [ 0, 2, 4 ] );
  380. range = new Range( start, end );
  381. op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
  382. expected = {
  383. type: AttributeOperation,
  384. range,
  385. key: 'foo',
  386. oldValue: 'abc',
  387. newValue: 'bar'
  388. };
  389. } );
  390. describe( 'by InsertOperation', () => {
  391. it( 'target at offset before: increment offset', () => {
  392. const transformBy = new InsertOperation(
  393. new Position( root, [ 0, 2, 0 ] ),
  394. [ nodeA, nodeB ],
  395. 0
  396. );
  397. const transOp = transform( op, transformBy );
  398. expected.range.start.offset += 2;
  399. expected.range.end.offset += 2;
  400. expect( transOp.length ).to.equal( 1 );
  401. expectOperation( transOp[ 0 ], expected );
  402. } );
  403. it( 'target at same offset: increment offset', () => {
  404. const transformBy = new InsertOperation(
  405. new Position( root, [ 0, 2, 1 ] ),
  406. [ nodeA, nodeB ],
  407. 0
  408. );
  409. const transOp = transform( op, transformBy );
  410. expected.range.start.offset += 2;
  411. expected.range.end.offset += 2;
  412. expect( transOp.length ).to.equal( 1 );
  413. expectOperation( transOp[ 0 ], expected );
  414. } );
  415. } );
  416. describe( 'by MoveOperation', () => {
  417. it( 'range offset is before change range start offset: decrement offset', () => {
  418. const transformBy = new MoveOperation(
  419. new Position( root, [ 0, 2, 0 ] ),
  420. 1,
  421. new Position( root, [ 2, 4, 1 ] ),
  422. 0
  423. );
  424. const transOp = transform( op, transformBy );
  425. expected.range.start.offset--;
  426. expected.range.end.offset--;
  427. expect( transOp.length ).to.equal( 1 );
  428. expectOperation( transOp[ 0 ], expected );
  429. } );
  430. it( 'target offset is before change range start offset: increment offset', () => {
  431. const transformBy = new MoveOperation(
  432. new Position( root, [ 2, 4, 1 ] ),
  433. 2,
  434. new Position( root, [ 0, 2, 0 ] ),
  435. 0
  436. );
  437. const transOp = transform( op, transformBy );
  438. expected.range.start.offset += 2;
  439. expected.range.end.offset += 2;
  440. expect( transOp.length ).to.equal( 1 );
  441. expectOperation( transOp[ 0 ], expected );
  442. } );
  443. it( 'range intersects on left with change range: split into two operations', () => {
  444. const transformBy = new MoveOperation(
  445. new Position( root, [ 0, 2, 2 ] ),
  446. 4,
  447. new Position( root, [ 2, 4, 1 ] ),
  448. 0
  449. );
  450. const transOp = transform( op, transformBy );
  451. expect( transOp.length ).to.equal( 2 );
  452. expected.range.end.offset -= 2;
  453. expectOperation( transOp[ 0 ], expected );
  454. expected.range.start.path = [ 2, 4, 1 ];
  455. expected.range.end.path = [ 2, 4, 3 ];
  456. expectOperation( transOp[ 1 ], expected );
  457. } );
  458. it( 'range intersects on right with change range: split into two operation', () => {
  459. const transformBy = new MoveOperation(
  460. new Position( root, [ 0, 2, 0 ] ),
  461. 2,
  462. new Position( root, [ 2, 4, 1 ] ),
  463. 0
  464. );
  465. const transOp = transform( op, transformBy );
  466. expect( transOp.length ).to.equal( 2 );
  467. expected.range.start.offset -= 1;
  468. expected.range.end.offset -= 2;
  469. expectOperation( transOp[ 0 ], expected );
  470. expected.range.start.path = [ 2, 4, 2 ];
  471. expected.range.end.path = [ 2, 4, 3 ];
  472. expectOperation( transOp[ 1 ], expected );
  473. } );
  474. it( 'range contains change range: update change range', () => {
  475. const transformBy = new MoveOperation(
  476. new Position( root, [ 0, 1 ] ),
  477. 3,
  478. new Position( root, [ 2, 4, 1 ] ),
  479. 0
  480. );
  481. const transOp = transform( op, transformBy );
  482. expected.range.start.path = [ 2, 4, 2, 1 ];
  483. expected.range.end.path = [ 2, 4, 2, 4 ];
  484. expect( transOp.length ).to.equal( 1 );
  485. expectOperation( transOp[ 0 ], expected );
  486. } );
  487. it( 'range is inside change range: split into two operations', () => {
  488. const transformBy = new MoveOperation(
  489. new Position( root, [ 0, 2, 2 ] ),
  490. 1,
  491. new Position( root, [ 2, 4, 1 ] ),
  492. 0
  493. );
  494. const transOp = transform( op, transformBy );
  495. expect( transOp.length ).to.equal( 3 );
  496. expected.range.start.path = [ 0, 2, 1 ];
  497. expected.range.end.path = [ 0, 2, 2 ];
  498. expectOperation( transOp[ 0 ], expected );
  499. expected.range.start.path = [ 0, 2, 2 ];
  500. expected.range.end.path = [ 0, 2, 3 ];
  501. expectOperation( transOp[ 1 ], expected );
  502. expected.range.start.path = [ 2, 4, 1 ];
  503. expected.range.end.path = [ 2, 4, 2 ];
  504. expectOperation( transOp[ 2 ], expected );
  505. } );
  506. it( 'range is same as change range: update change range', () => {
  507. const transformBy = new MoveOperation(
  508. new Position( root, [ 0, 2, 1 ] ),
  509. 3,
  510. new Position( root, [ 2, 4, 1 ] ),
  511. 0
  512. );
  513. const transOp = transform( op, transformBy );
  514. expected.range.start.path = [ 2, 4, 1 ];
  515. expected.range.end.path = [ 2, 4, 4 ];
  516. expect( transOp.length ).to.equal( 1 );
  517. expectOperation( transOp[ 0 ], expected );
  518. } );
  519. it( 'target inside change range: split into two operations', () => {
  520. const transformBy = new MoveOperation(
  521. new Position( root, [ 2, 4, 1 ] ),
  522. 2,
  523. new Position( root, [ 0, 2, 2 ] ),
  524. 0
  525. );
  526. const transOp = transform( op, transformBy );
  527. expect( transOp.length ).to.equal( 2 );
  528. expected.range.end.offset = 2;
  529. expectOperation( transOp[ 0 ], expected );
  530. expected.range.start.offset = 4;
  531. expected.range.end.offset = 6;
  532. expectOperation( transOp[ 1 ], expected );
  533. } );
  534. it( 'range intersects change range and target inside change range: split into three operations', () => {
  535. const transformBy = new MoveOperation(
  536. new Position( root, [ 0, 2, 0 ] ),
  537. 2,
  538. new Position( root, [ 0, 2, 3 ] ),
  539. 0
  540. );
  541. const transOp = transform( op, transformBy );
  542. expect( transOp.length ).to.equal( 3 );
  543. expected.range.start.path = [ 0, 2, 0 ];
  544. expected.range.end.path = [ 0, 2, 1 ];
  545. expectOperation( transOp[ 0 ], expected );
  546. expected.range.start.path = [ 0, 2, 3 ];
  547. expected.range.end.path = [ 0, 2, 4 ];
  548. expectOperation( transOp[ 1 ], expected );
  549. expected.range.start.path = [ 0, 2, 2 ];
  550. expected.range.end.path = [ 0, 2, 3 ];
  551. expectOperation( transOp[ 2 ], expected );
  552. } );
  553. } );
  554. } );
  555. describe( 'RootAttributeOperation', () => {
  556. const diffRoot = new RootElement( null );
  557. beforeEach( () => {
  558. expected = {
  559. type: RootAttributeOperation,
  560. key: 'foo',
  561. oldValue: 'abc',
  562. newValue: 'bar'
  563. };
  564. op = new RootAttributeOperation( root, 'foo', 'abc', 'bar', 0 );
  565. } );
  566. describe( 'by InsertOperation', () => {
  567. it( 'no operation update', () => {
  568. const transformBy = new InsertOperation(
  569. new Position( root, [ 0 ] ),
  570. 'a',
  571. 0
  572. );
  573. const transOp = transform( op, transformBy );
  574. expect( transOp.length ).to.equal( 1 );
  575. expectOperation( transOp[ 0 ], expected );
  576. } );
  577. } );
  578. describe( 'by AttributeOperation', () => {
  579. it( 'no operation update', () => {
  580. const transformBy = new AttributeOperation(
  581. new Range(
  582. new Position( root, [ 0 ] ),
  583. new Position( root, [ 1 ] )
  584. ),
  585. 'foo',
  586. 'bar',
  587. 'xyz',
  588. 0
  589. );
  590. const transOp = transform( op, transformBy );
  591. expect( transOp.length ).to.equal( 1 );
  592. expectOperation( transOp[ 0 ], expected );
  593. } );
  594. } );
  595. describe( 'by RootAttributeOperation', () => {
  596. it( 'changes different root: no operation update', () => {
  597. const transformBy = new RootAttributeOperation(
  598. diffRoot,
  599. 'foo',
  600. 'abc',
  601. 'xyz',
  602. 0
  603. );
  604. const transOp = transform( op, transformBy );
  605. expect( transOp.length ).to.equal( 1 );
  606. expectOperation( transOp[ 0 ], expected );
  607. } );
  608. it( 'changes different key: no operation update', () => {
  609. const transformBy = new RootAttributeOperation(
  610. root,
  611. 'abc',
  612. 'abc',
  613. 'xyz',
  614. 0
  615. );
  616. const transOp = transform( op, transformBy );
  617. expect( transOp.length ).to.equal( 1 );
  618. expectOperation( transOp[ 0 ], expected );
  619. } );
  620. it( 'sets same value for same key: convert to NoOperation', () => {
  621. const transformBy = new RootAttributeOperation(
  622. root,
  623. 'foo',
  624. 'abc',
  625. 'bar',
  626. 0
  627. );
  628. const transOp = transform( op, transformBy );
  629. expect( transOp.length ).to.equal( 1 );
  630. expectOperation( transOp[ 0 ], {
  631. type: NoOperation
  632. } );
  633. } );
  634. it( 'sets different value for same key on same root and is important: convert to NoOperation', () => {
  635. const transformBy = new RootAttributeOperation(
  636. root,
  637. 'foo',
  638. 'abc',
  639. 'xyz',
  640. 0
  641. );
  642. const transOp = transform( op, transformBy );
  643. expect( transOp.length ).to.equal( 1 );
  644. expectOperation( transOp[ 0 ], {
  645. type: NoOperation
  646. } );
  647. } );
  648. it( 'sets different value for same key on same root and is less important: change oldValue', () => {
  649. const transformBy = new RootAttributeOperation(
  650. root,
  651. 'foo',
  652. 'abc',
  653. 'xyz',
  654. 0
  655. );
  656. const transOp = transform( op, transformBy, strongContext );
  657. expected.oldValue = 'xyz';
  658. expect( transOp.length ).to.equal( 1 );
  659. expectOperation( transOp[ 0 ], expected );
  660. } );
  661. } );
  662. describe( 'by MoveOperation', () => {
  663. it( 'no operation update', () => {
  664. const transformBy = new MoveOperation(
  665. new Position( root, [ 0 ] ),
  666. 2,
  667. new Position( root, [ 1 ] ),
  668. 0
  669. );
  670. const transOp = transform( op, transformBy );
  671. expect( transOp.length ).to.equal( 1 );
  672. expectOperation( transOp[ 0 ], expected );
  673. } );
  674. } );
  675. describe( 'by NoOperation', () => {
  676. it( 'no operation update', () => {
  677. const transformBy = new NoOperation( 0 );
  678. const transOp = transform( op, transformBy );
  679. expect( transOp.length ).to.equal( 1 );
  680. expectOperation( transOp[ 0 ], expected );
  681. } );
  682. } );
  683. describe( 'by RenameOperation', () => {
  684. it( 'no position update', () => {
  685. const transformBy = new RenameOperation( new Position( root, [ 0 ] ), 'oldName', 'newName', 0 );
  686. const transOp = transform( op, transformBy );
  687. expect( transOp.length ).to.equal( 1 );
  688. expectOperation( transOp[ 0 ], expected );
  689. } );
  690. } );
  691. describe( 'by MarkerOperation', () => {
  692. it( 'no position update', () => {
  693. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  694. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  695. const transOp = transform( op, transformBy );
  696. expect( transOp.length ).to.equal( 1 );
  697. expectOperation( transOp[ 0 ], expected );
  698. } );
  699. } );
  700. } );
  701. describe( 'MoveOperation', () => {
  702. let sourcePosition, targetPosition, rangeEnd, howMany;
  703. beforeEach( () => {
  704. sourcePosition = new Position( root, [ 2, 2, 4 ] );
  705. targetPosition = new Position( root, [ 3, 3, 3 ] );
  706. howMany = 2;
  707. rangeEnd = sourcePosition.clone();
  708. rangeEnd.offset += howMany;
  709. op = new MoveOperation( sourcePosition, howMany, targetPosition, 0 );
  710. expected = {
  711. type: MoveOperation,
  712. sourcePosition: sourcePosition.clone(),
  713. targetPosition: targetPosition.clone(),
  714. howMany
  715. };
  716. } );
  717. describe( 'by InsertOperation', () => {
  718. it( 'target at different position than move range and target: no operation update', () => {
  719. const transformBy = new InsertOperation(
  720. new Position( root, [ 1, 3, 2 ] ),
  721. [ nodeA, nodeB ],
  722. 0
  723. );
  724. const transOp = transform( op, transformBy );
  725. expect( transOp.length ).to.equal( 1 );
  726. expectOperation( transOp[ 0 ], expected );
  727. } );
  728. it( 'target inside node from move range: no operation update', () => {
  729. const transformBy = new InsertOperation(
  730. new Position( root, [ 2, 2, 4, 1 ] ),
  731. [ nodeA, nodeB ],
  732. 0
  733. );
  734. const transOp = transform( op, transformBy );
  735. expect( transOp.length ).to.equal( 1 );
  736. expectOperation( transOp[ 0 ], expected );
  737. } );
  738. it( 'target at offset before range offset: increment range offset', () => {
  739. const transformBy = new InsertOperation(
  740. new Position( root, [ 2, 2, 0 ] ),
  741. [ nodeA, nodeB ],
  742. 0
  743. );
  744. const transOp = transform( op, transformBy );
  745. expected.sourcePosition.offset += 2;
  746. expect( transOp.length ).to.equal( 1 );
  747. expectOperation( transOp[ 0 ], expected );
  748. } );
  749. it( 'target at offset after range offset: no operation update', () => {
  750. const transformBy = new InsertOperation(
  751. new Position( root, [ 2, 2, 7 ] ),
  752. [ nodeA, nodeB ],
  753. 0
  754. );
  755. const transOp = transform( op, transformBy );
  756. expect( transOp.length ).to.equal( 1 );
  757. expectOperation( transOp[ 0 ], expected );
  758. } );
  759. it( 'target before node from range start path: increment index on range start path', () => {
  760. const transformBy = new InsertOperation(
  761. new Position( root, [ 2, 0 ] ),
  762. [ nodeA, nodeB ],
  763. 0
  764. );
  765. const transOp = transform( op, transformBy );
  766. expected.sourcePosition.path[ 1 ] += 2;
  767. expect( transOp.length ).to.equal( 1 );
  768. expectOperation( transOp[ 0 ], expected );
  769. } );
  770. it( 'target after node from range start path: no operation update', () => {
  771. const transformBy = new InsertOperation(
  772. new Position( root, [ 2, 3 ] ),
  773. [ nodeA, nodeB ],
  774. 0
  775. );
  776. const transOp = transform( op, transformBy );
  777. expect( transOp.length ).to.equal( 1 );
  778. expectOperation( transOp[ 0 ], expected );
  779. } );
  780. it( 'target offset before move target offset: increment target offset', () => {
  781. const transformBy = new InsertOperation(
  782. new Position( root, [ 3, 3, 2 ] ),
  783. [ nodeA, nodeB ],
  784. 0
  785. );
  786. const transOp = transform( op, transformBy );
  787. expected.targetPosition.offset += 2;
  788. expect( transOp.length ).to.equal( 1 );
  789. expectOperation( transOp[ 0 ], expected );
  790. } );
  791. it( 'target offset after move target offset: no operation update', () => {
  792. const transformBy = new InsertOperation(
  793. new Position( root, [ 3, 3, 4 ] ),
  794. [ nodeA, nodeB ],
  795. 0
  796. );
  797. const transOp = transform( op, transformBy );
  798. expect( transOp.length ).to.equal( 1 );
  799. expectOperation( transOp[ 0 ], expected );
  800. } );
  801. it( 'target before node from move target position path: increment index on move target position path', () => {
  802. const transformBy = new InsertOperation(
  803. new Position( root, [ 3, 3 ] ),
  804. [ nodeA, nodeB ],
  805. 0
  806. );
  807. const transOp = transform( op, transformBy );
  808. expected.targetPosition.path[ 1 ] += 2;
  809. expect( transOp.length ).to.equal( 1 );
  810. expectOperation( transOp[ 0 ], expected );
  811. } );
  812. it( 'target after node from move target position path: no operation update', () => {
  813. const transformBy = new InsertOperation(
  814. new Position( root, [ 3, 6 ] ),
  815. [ nodeA, nodeB ],
  816. 0
  817. );
  818. const transOp = transform( op, transformBy );
  819. expect( transOp.length ).to.equal( 1 );
  820. expectOperation( transOp[ 0 ], expected );
  821. } );
  822. it( 'target offset same as move target offset and is important: no operation update', () => {
  823. const transformBy = new InsertOperation(
  824. new Position( root, [ 3, 3, 3 ] ),
  825. [ nodeA, nodeB ],
  826. 0
  827. );
  828. const transOp = transform( op, transformBy );
  829. expect( transOp.length ).to.equal( 1 );
  830. expectOperation( transOp[ 0 ], expected );
  831. } );
  832. it( 'target offset same as move target offset and is less important: no operation update', () => {
  833. const transformBy = new InsertOperation(
  834. new Position( root, [ 3, 3, 3 ] ),
  835. [ nodeA, nodeB ],
  836. 0
  837. );
  838. const transOp = transform( op, transformBy, strongContext );
  839. expect( transOp.length ).to.equal( 1 );
  840. expectOperation( transOp[ 0 ], expected );
  841. } );
  842. it( 'target inside move range: expand range', () => {
  843. const transformBy = new InsertOperation(
  844. new Position( root, [ 2, 2, 5 ] ),
  845. [ nodeA, nodeB ],
  846. 0
  847. );
  848. const transOp = transform( op, transformBy );
  849. expect( transOp.length ).to.equal( 1 );
  850. expected.howMany = 4;
  851. expectOperation( transOp[ 0 ], expected );
  852. } );
  853. it( 'target at offset same as range end boundary: no operation update', () => {
  854. const transformBy = new InsertOperation(
  855. new Position( root, [ 2, 2, 6 ] ),
  856. [ nodeA, nodeB ],
  857. 0
  858. );
  859. const transOp = transform( op, transformBy );
  860. expect( transOp.length ).to.equal( 1 );
  861. expectOperation( transOp[ 0 ], expected );
  862. } );
  863. } );
  864. describe( 'by AttributeOperation', () => {
  865. it( 'no operation update', () => {
  866. const transformBy = new AttributeOperation(
  867. new Range( sourcePosition, rangeEnd ),
  868. 'abc',
  869. true,
  870. false,
  871. 0
  872. );
  873. const transOp = transform( op, transformBy );
  874. expect( transOp.length ).to.equal( 1 );
  875. expectOperation( transOp[ 0 ], expected );
  876. } );
  877. } );
  878. describe( 'by RootAttributeOperation', () => {
  879. it( 'no operation update', () => {
  880. const transformBy = new RootAttributeOperation(
  881. root,
  882. 'foo',
  883. null,
  884. 'bar',
  885. 0
  886. );
  887. const transOp = transform( op, transformBy );
  888. expect( transOp.length ).to.equal( 1 );
  889. expectOperation( transOp[ 0 ], expected );
  890. } );
  891. } );
  892. describe( 'by MoveOperation', () => {
  893. it( 'range and target different than transforming range and target: no operation update', () => {
  894. const transformBy = new MoveOperation(
  895. new Position( root, [ 1, 2 ] ),
  896. 3,
  897. new Position( root, [ 4, 1, 0 ] ),
  898. 0
  899. );
  900. const transOp = transform( op, transformBy );
  901. expect( transOp.length ).to.equal( 1 );
  902. expectOperation( transOp[ 0 ], expected );
  903. } );
  904. it( 'target offset before transforming range start offset: increment range offset', () => {
  905. const transformBy = new MoveOperation(
  906. new Position( root, [ 4, 1, 0 ] ),
  907. 2,
  908. new Position( root, [ 2, 2, 0 ] ),
  909. 0
  910. );
  911. const transOp = transform( op, transformBy );
  912. expected.sourcePosition.offset += 2;
  913. expect( transOp.length ).to.equal( 1 );
  914. expectOperation( transOp[ 0 ], expected );
  915. } );
  916. it( 'target offset after transforming range start offset: no operation update', () => {
  917. const transformBy = new MoveOperation(
  918. new Position( root, [ 4, 1, 0 ] ),
  919. 2,
  920. new Position( root, [ 2, 2, 7 ] ),
  921. 0
  922. );
  923. const transOp = transform( op, transformBy );
  924. expect( transOp.length ).to.equal( 1 );
  925. expectOperation( transOp[ 0 ], expected );
  926. } );
  927. it( 'range start offset before transforming range start offset: decrement range offset', () => {
  928. const transformBy = new MoveOperation(
  929. new Position( root, [ 2, 2, 0 ] ),
  930. 2,
  931. new Position( root, [ 4, 1, 0 ] ),
  932. 0
  933. );
  934. const transOp = transform( op, transformBy );
  935. expected.sourcePosition.offset -= 2;
  936. expect( transOp.length ).to.equal( 1 );
  937. expectOperation( transOp[ 0 ], expected );
  938. } );
  939. it( 'range start offset after transforming range start offset: no operation update', () => {
  940. const transformBy = new MoveOperation(
  941. new Position( root, [ 2, 2, 9 ] ),
  942. 2,
  943. new Position( root, [ 4, 1, 0 ] ),
  944. 0
  945. );
  946. const transOp = transform( op, transformBy, strongContext );
  947. expect( transOp.length ).to.equal( 1 );
  948. expectOperation( transOp[ 0 ], expected );
  949. } );
  950. it( 'target before node from transforming range start path: increment index on range start path', () => {
  951. const transformBy = new MoveOperation(
  952. new Position( root, [ 4, 1, 0 ] ),
  953. 2,
  954. new Position( root, [ 2, 1 ] ),
  955. 0
  956. );
  957. const transOp = transform( op, transformBy );
  958. expected.sourcePosition.path[ 1 ] += 2;
  959. expect( transOp.length ).to.equal( 1 );
  960. expectOperation( transOp[ 0 ], expected );
  961. } );
  962. it( 'target after node from transforming range start path: no operation update', () => {
  963. const transformBy = new MoveOperation(
  964. new Position( root, [ 4, 1, 0 ] ),
  965. 2,
  966. new Position( root, [ 2, 5 ] ),
  967. 0
  968. );
  969. const transOp = transform( op, transformBy );
  970. expect( transOp.length ).to.equal( 1 );
  971. expectOperation( transOp[ 0 ], expected );
  972. } );
  973. it( 'range before node from transforming range start path: decrement index on range start path', () => {
  974. const transformBy = new MoveOperation(
  975. new Position( root, [ 2, 0 ] ),
  976. 1,
  977. new Position( root, [ 4, 1, 0 ] ),
  978. 0
  979. );
  980. const transOp = transform( op, transformBy );
  981. expected.sourcePosition.path[ 1 ] -= 1;
  982. expect( transOp.length ).to.equal( 1 );
  983. expectOperation( transOp[ 0 ], expected );
  984. } );
  985. it( 'range after node from transforming range start path: no operation update', () => {
  986. const transformBy = new MoveOperation(
  987. new Position( root, [ 2, 3 ] ),
  988. 2,
  989. new Position( root, [ 4, 1, 0 ] ),
  990. 0
  991. );
  992. const transOp = transform( op, transformBy );
  993. expect( transOp.length ).to.equal( 1 );
  994. expectOperation( transOp[ 0 ], expected );
  995. } );
  996. it( 'target offset before transforming target offset: increment target offset', () => {
  997. const transformBy = new MoveOperation(
  998. new Position( root, [ 4, 1, 0 ] ),
  999. 2,
  1000. new Position( root, [ 3, 3, 0 ] ),
  1001. 0
  1002. );
  1003. const transOp = transform( op, transformBy );
  1004. expected.targetPosition.offset += 2;
  1005. expect( transOp.length ).to.equal( 1 );
  1006. expectOperation( transOp[ 0 ], expected );
  1007. } );
  1008. it( 'target offset after transforming target offset: no operation update', () => {
  1009. const transformBy = new MoveOperation(
  1010. new Position( root, [ 4, 1, 0 ] ),
  1011. 2,
  1012. new Position( root, [ 3, 3, 5 ] ),
  1013. 0
  1014. );
  1015. const transOp = transform( op, transformBy );
  1016. expect( transOp.length ).to.equal( 1 );
  1017. expectOperation( transOp[ 0 ], expected );
  1018. } );
  1019. it( 'range offset before transforming target offset: decrement target offset', () => {
  1020. const transformBy = new MoveOperation(
  1021. new Position( root, [ 3, 3, 0 ] ),
  1022. 2,
  1023. new Position( root, [ 4, 1, 0 ] ),
  1024. 0
  1025. );
  1026. const transOp = transform( op, transformBy );
  1027. expected.targetPosition.offset -= 2;
  1028. expect( transOp.length ).to.equal( 1 );
  1029. expectOperation( transOp[ 0 ], expected );
  1030. } );
  1031. it( 'range offset after transforming target offset: no operation update', () => {
  1032. const transformBy = new MoveOperation(
  1033. new Position( root, [ 3, 3, 5 ] ),
  1034. 2,
  1035. new Position( root, [ 4, 1, 0 ] ),
  1036. 0
  1037. );
  1038. const transOp = transform( op, transformBy );
  1039. expect( transOp.length ).to.equal( 1 );
  1040. expectOperation( transOp[ 0 ], expected );
  1041. } );
  1042. it( 'target before node from transforming target path: increment index on target path', () => {
  1043. const transformBy = new MoveOperation(
  1044. new Position( root, [ 4, 1, 0 ] ),
  1045. 2,
  1046. new Position( root, [ 3, 1 ] ),
  1047. 0
  1048. );
  1049. const transOp = transform( op, transformBy );
  1050. expected.targetPosition.path[ 1 ] += 2;
  1051. expect( transOp.length ).to.equal( 1 );
  1052. expectOperation( transOp[ 0 ], expected );
  1053. } );
  1054. it( 'target after node from transforming target path: no operation update', () => {
  1055. const transformBy = new MoveOperation(
  1056. new Position( root, [ 4, 1, 0 ] ),
  1057. 2,
  1058. new Position( root, [ 3, 5 ] ),
  1059. 0
  1060. );
  1061. const transOp = transform( op, transformBy );
  1062. expect( transOp.length ).to.equal( 1 );
  1063. expectOperation( transOp[ 0 ], expected );
  1064. } );
  1065. it( 'range before node from transforming target path: decrement index on target path', () => {
  1066. const transformBy = new MoveOperation(
  1067. new Position( root, [ 3, 0 ] ),
  1068. 2,
  1069. new Position( root, [ 4, 1, 0 ] ),
  1070. 0
  1071. );
  1072. const transOp = transform( op, transformBy );
  1073. expected.targetPosition.path[ 1 ] -= 2;
  1074. expect( transOp.length ).to.equal( 1 );
  1075. expectOperation( transOp[ 0 ], expected );
  1076. } );
  1077. it( 'range after node from transforming target path: no operation update', () => {
  1078. const transformBy = new MoveOperation(
  1079. new Position( root, [ 3, 5 ] ),
  1080. 2,
  1081. new Position( root, [ 4, 1, 0 ] ),
  1082. 0
  1083. );
  1084. const transOp = transform( op, transformBy );
  1085. expect( transOp.length ).to.equal( 1 );
  1086. expectOperation( transOp[ 0 ], expected );
  1087. } );
  1088. it( 'target inside transforming move range: split into two operations', () => {
  1089. const transformBy = new MoveOperation(
  1090. new Position( root, [ 4, 1, 0 ] ),
  1091. 2,
  1092. new Position( root, [ 2, 2, 5 ] ),
  1093. 0
  1094. );
  1095. const transOp = transform( op, transformBy );
  1096. expect( transOp.length ).to.equal( 2 );
  1097. expected.howMany = 1;
  1098. expectOperation( transOp[ 0 ], expected );
  1099. expected.sourcePosition.path = [ 2, 2, 6 ];
  1100. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1101. expectOperation( transOp[ 1 ], expected );
  1102. } );
  1103. it( 'target at start boundary of transforming move range: increment source offset', () => {
  1104. const transformBy = new MoveOperation(
  1105. new Position( root, [ 4, 1, 0 ] ),
  1106. 2,
  1107. new Position( root, [ 2, 2, 4 ] ),
  1108. 0
  1109. );
  1110. const transOp = transform( op, transformBy );
  1111. expect( transOp.length ).to.equal( 1 );
  1112. expected.sourcePosition.offset = 6;
  1113. expectOperation( transOp[ 0 ], expected );
  1114. } );
  1115. it( 'target at end boundary of transforming move range: no operation update', () => {
  1116. const transformBy = new MoveOperation(
  1117. new Position( root, [ 4, 1, 0 ] ),
  1118. 2,
  1119. new Position( root, [ 2, 2, 6 ] ),
  1120. 0
  1121. );
  1122. const transOp = transform( op, transformBy );
  1123. expect( transOp.length ).to.equal( 1 );
  1124. expectOperation( transOp[ 0 ], expected );
  1125. } );
  1126. it( 'target inside a node from transforming range: no operation update', () => {
  1127. const transformBy = new MoveOperation(
  1128. new Position( root, [ 4, 1, 0 ] ),
  1129. 2,
  1130. new Position( root, [ 2, 2, 5, 1 ] ),
  1131. 0
  1132. );
  1133. const transOp = transform( op, transformBy );
  1134. expect( transOp.length ).to.equal( 1 );
  1135. expectOperation( transOp[ 0 ], expected );
  1136. } );
  1137. it( 'range has node that contains transforming range: update range path', () => {
  1138. const transformBy = new MoveOperation(
  1139. new Position( root, [ 2, 1 ] ),
  1140. 3,
  1141. new Position( root, [ 4, 2 ] ),
  1142. 0
  1143. );
  1144. const transOp = transform( op, transformBy );
  1145. expected.sourcePosition.path = [ 4, 3, 4 ];
  1146. expect( transOp.length ).to.equal( 1 );
  1147. expectOperation( transOp[ 0 ], expected );
  1148. } );
  1149. it( 'range has node that contains transforming target: update target path', () => {
  1150. const transformBy = new MoveOperation(
  1151. new Position( root, [ 3, 2 ] ),
  1152. 3,
  1153. new Position( root, [ 0, 1 ] ),
  1154. 0
  1155. );
  1156. const transOp = transform( op, transformBy );
  1157. expected.targetPosition.path = [ 0, 2, 3 ];
  1158. expect( transOp.length ).to.equal( 1 );
  1159. expectOperation( transOp[ 0 ], expected );
  1160. } );
  1161. it( 'target inside a node from transforming range and vice versa: reverse transform-by operation', () => {
  1162. const transformBy = new MoveOperation(
  1163. new Position( root, [ 3, 2 ] ),
  1164. 3,
  1165. new Position( root, [ 2, 2, 5, 0 ] ),
  1166. 0
  1167. );
  1168. const transOp = transform( op, transformBy );
  1169. const reversed = transformBy.getReversed();
  1170. expected.sourcePosition = reversed.sourcePosition;
  1171. expected.targetPosition = reversed.targetPosition;
  1172. expected.howMany = reversed.howMany;
  1173. expect( transOp.length ).to.equal( 1 );
  1174. expectOperation( transOp[ 0 ], expected );
  1175. } );
  1176. it( 'range is same as transforming range and is important: convert to NoOperation', () => {
  1177. const transformBy = new MoveOperation(
  1178. op.sourcePosition,
  1179. op.howMany,
  1180. new Position( root, [ 4, 1, 0 ] ),
  1181. 0
  1182. );
  1183. const transOp = transform( op, transformBy );
  1184. expect( transOp.length ).to.equal( 1 );
  1185. expectOperation( transOp[ 0 ], {
  1186. type: NoOperation
  1187. } );
  1188. } );
  1189. it( 'range is same as transforming range and is less important: update range path', () => {
  1190. const transformBy = new MoveOperation(
  1191. op.sourcePosition,
  1192. op.howMany,
  1193. new Position( root, [ 4, 1, 0 ] ),
  1194. 0
  1195. );
  1196. const transOp = transform( op, transformBy, strongContext );
  1197. expected.sourcePosition.path = [ 4, 1, 0 ];
  1198. expect( transOp.length ).to.equal( 1 );
  1199. expectOperation( transOp[ 0 ], expected );
  1200. } );
  1201. it( 'range contains transforming range and is important: convert to NoOperation', () => {
  1202. const transformBy = new MoveOperation(
  1203. new Position( root, [ 2, 2, 3 ] ),
  1204. 4,
  1205. new Position( root, [ 4, 1, 0 ] ),
  1206. 0
  1207. );
  1208. const transOp = transform( op, transformBy );
  1209. expect( transOp.length ).to.equal( 1 );
  1210. expectOperation( transOp[ 0 ], {
  1211. type: NoOperation
  1212. } );
  1213. } );
  1214. it( 'range contains transforming range and is less important: update range path', () => {
  1215. const transformBy = new MoveOperation(
  1216. new Position( root, [ 2, 2, 3 ] ),
  1217. 4,
  1218. new Position( root, [ 4, 1, 0 ] ),
  1219. 0
  1220. );
  1221. const transOp = transform( op, transformBy, strongContext );
  1222. expected.sourcePosition.path = [ 4, 1, 1 ];
  1223. expect( transOp.length ).to.equal( 1 );
  1224. expectOperation( transOp[ 0 ], expected );
  1225. } );
  1226. it( 'range contains transforming range and target and is important: update range path and target', () => {
  1227. op.targetPosition.path = [ 2, 2, 7 ];
  1228. const transformBy = new MoveOperation(
  1229. new Position( root, [ 2, 2, 3 ] ),
  1230. 5,
  1231. new Position( root, [ 4, 1, 0 ] ),
  1232. 0
  1233. );
  1234. const transOp = transform( op, transformBy );
  1235. expect( transOp.length ).to.equal( 1 );
  1236. expected.sourcePosition.path = [ 4, 1, 1 ];
  1237. expected.targetPosition.path = [ 4, 1, 4 ];
  1238. expectOperation( transOp[ 0 ], expected );
  1239. } );
  1240. it( 'range contains transforming range and target and is less important: update range path and target', () => {
  1241. op.targetPosition.path = [ 2, 2, 7 ];
  1242. const transformBy = new MoveOperation(
  1243. new Position( root, [ 2, 2, 3 ] ),
  1244. 5,
  1245. new Position( root, [ 4, 1, 0 ] ),
  1246. 0
  1247. );
  1248. const transOp = transform( op, transformBy, strongContext );
  1249. expect( transOp.length ).to.equal( 1 );
  1250. expected.sourcePosition.path = [ 4, 1, 1 ];
  1251. expected.targetPosition.path = [ 4, 1, 4 ];
  1252. expectOperation( transOp[ 0 ], expected );
  1253. } );
  1254. it( 'range intersects on left side of transforming range and is important: shrink range', () => {
  1255. const transformBy = new MoveOperation(
  1256. new Position( root, [ 2, 2, 3 ] ),
  1257. 2,
  1258. new Position( root, [ 4, 1, 0 ] ),
  1259. 0
  1260. );
  1261. const transOp = transform( op, transformBy );
  1262. expected.sourcePosition.path = [ 2, 2, 3 ];
  1263. expected.howMany = 1;
  1264. expect( transOp.length ).to.equal( 1 );
  1265. expectOperation( transOp[ 0 ], expected );
  1266. } );
  1267. it( 'range intersects on left side of transforming range and is less important: split into two operations', () => {
  1268. // Get more test cases and better code coverage
  1269. const otherRoot = new RootElement( null );
  1270. const transformBy = new MoveOperation(
  1271. new Position( root, [ 2, 2, 3 ] ),
  1272. 2,
  1273. new Position( otherRoot, [ 4, 1, 0 ] ),
  1274. 0
  1275. );
  1276. const transOp = transform( op, transformBy, strongContext );
  1277. expect( transOp.length ).to.equal( 2 );
  1278. expected.sourcePosition = new Position( otherRoot, [ 4, 1, 1 ] );
  1279. expected.howMany = 1;
  1280. expectOperation( transOp[ 0 ], expected );
  1281. expected.sourcePosition = new Position( root, [ 2, 2, 3 ] );
  1282. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1283. expectOperation( transOp[ 1 ], expected );
  1284. } );
  1285. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1286. const transformBy = new MoveOperation(
  1287. new Position( root, [ 2, 2, 5 ] ),
  1288. 2,
  1289. new Position( root, [ 4, 1, 0 ] ),
  1290. 0
  1291. );
  1292. const transOp = transform( op, transformBy );
  1293. expected.howMany = 1;
  1294. expect( transOp.length ).to.equal( 1 );
  1295. expectOperation( transOp[ 0 ], expected );
  1296. } );
  1297. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1298. const transformBy = new MoveOperation(
  1299. new Position( root, [ 2, 2, 5 ] ),
  1300. 2,
  1301. new Position( root, [ 4, 1, 0 ] ),
  1302. 0
  1303. );
  1304. const transOp = transform( op, transformBy, strongContext );
  1305. expect( transOp.length ).to.equal( 2 );
  1306. expected.sourcePosition = sourcePosition;
  1307. expected.howMany = 1;
  1308. expectOperation( transOp[ 0 ], expected );
  1309. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1310. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1311. expectOperation( transOp[ 1 ], expected );
  1312. } );
  1313. it( 'range intersects on left side, target inside transforming range and is important: split into two operations', () => {
  1314. op.howMany = 4;
  1315. const transformBy = new MoveOperation(
  1316. new Position( root, [ 2, 2, 3 ] ),
  1317. 2,
  1318. new Position( root, [ 2, 2, 6 ] ),
  1319. 0
  1320. );
  1321. const transOp = transform( op, transformBy );
  1322. expect( transOp.length ).to.equal( 2 );
  1323. expected.sourcePosition.path = [ 2, 2, 3 ];
  1324. expected.howMany = 1;
  1325. expectOperation( transOp[ 0 ], expected );
  1326. expected.sourcePosition.path = [ 2, 2, 5 ];
  1327. expected.howMany = 2;
  1328. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1329. expectOperation( transOp[ 1 ], expected );
  1330. } );
  1331. it( 'range intersects on left side, target inside transforming range and is less important: split into two operations', () => {
  1332. op.howMany = 4;
  1333. const transformBy = new MoveOperation(
  1334. new Position( root, [ 2, 2, 3 ] ),
  1335. 2,
  1336. new Position( root, [ 2, 2, 6 ] ),
  1337. 0
  1338. );
  1339. const transOp = transform( op, transformBy, strongContext );
  1340. expect( transOp.length ).to.equal( 3 );
  1341. expected.sourcePosition.path = [ 2, 2, 3 ];
  1342. expected.howMany = 1;
  1343. expectOperation( transOp[ 0 ], expected );
  1344. expected.sourcePosition.path = [ 2, 2, 4 ];
  1345. expected.howMany = 1;
  1346. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1347. expectOperation( transOp[ 1 ], expected );
  1348. expected.sourcePosition.path = [ 2, 2, 4 ];
  1349. expected.howMany = 2;
  1350. expected.targetPosition = targetPosition.getShiftedBy( 2 );
  1351. expectOperation( transOp[ 2 ], expected );
  1352. } );
  1353. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1354. const transformBy = new MoveOperation(
  1355. new Position( root, [ 2, 2, 5 ] ),
  1356. 2,
  1357. new Position( root, [ 4, 1, 0 ] ),
  1358. 0
  1359. );
  1360. const transOp = transform( op, transformBy );
  1361. expected.howMany = 1;
  1362. expect( transOp.length ).to.equal( 1 );
  1363. expectOperation( transOp[ 0 ], expected );
  1364. } );
  1365. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1366. const transformBy = new MoveOperation(
  1367. new Position( root, [ 2, 2, 5 ] ),
  1368. 2,
  1369. new Position( root, [ 4, 1, 0 ] ),
  1370. 0
  1371. );
  1372. const transOp = transform( op, transformBy, strongContext );
  1373. expect( transOp.length ).to.equal( 2 );
  1374. expected.sourcePosition = sourcePosition;
  1375. expected.howMany = 1;
  1376. expectOperation( transOp[ 0 ], expected );
  1377. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1378. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1379. expectOperation( transOp[ 1 ], expected );
  1380. } );
  1381. it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
  1382. op.targetPosition.path = [ 2, 2, 7 ];
  1383. const transformBy = new MoveOperation(
  1384. new Position( root, [ 2, 2, 5 ] ),
  1385. 4,
  1386. new Position( root, [ 4, 1, 0 ] ),
  1387. 0
  1388. );
  1389. const transOp = transform( op, transformBy, strongContext );
  1390. expect( transOp.length ).to.equal( 2 );
  1391. expected.sourcePosition.path = [ 2, 2, 4 ];
  1392. expected.targetPosition.path = [ 4, 1, 2 ];
  1393. expected.howMany = 1;
  1394. expectOperation( transOp[ 0 ], expected );
  1395. expected.sourcePosition.path = [ 4, 1, 0 ];
  1396. expected.targetPosition.path = [ 4, 1, 3 ];
  1397. expectOperation( transOp[ 1 ], expected );
  1398. } );
  1399. it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
  1400. op.targetPosition.path = [ 2, 2, 7 ];
  1401. const transformBy = new MoveOperation(
  1402. new Position( root, [ 2, 2, 5 ] ),
  1403. 4,
  1404. new Position( root, [ 4, 1, 0 ] ),
  1405. 0
  1406. );
  1407. const transOp = transform( op, transformBy );
  1408. expect( transOp.length ).to.equal( 1 );
  1409. expected.sourcePosition.path = [ 2, 2, 4 ];
  1410. expected.targetPosition.path = [ 4, 1, 2 ];
  1411. expected.howMany = 1;
  1412. expectOperation( transOp[ 0 ], expected );
  1413. } );
  1414. it( 'range inside transforming range and is important: split into two operations', () => {
  1415. op.howMany = 4;
  1416. const transformBy = new MoveOperation(
  1417. new Position( root, [ 2, 2, 5 ] ),
  1418. 2,
  1419. new Position( root, [ 4, 1, 0 ] ),
  1420. 0
  1421. );
  1422. const transOp = transform( op, transformBy );
  1423. expect( transOp.length ).to.equal( 2 );
  1424. expected.sourcePosition.path = [ 2, 2, 4 ];
  1425. expected.howMany = 1;
  1426. expectOperation( transOp[ 0 ], expected );
  1427. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1428. expectOperation( transOp[ 1 ], expected );
  1429. } );
  1430. it( 'range inside transforming range and is less important: split into three operations', () => {
  1431. op.howMany = 4;
  1432. const transformBy = new MoveOperation(
  1433. new Position( root, [ 2, 2, 5 ] ),
  1434. 2,
  1435. new Position( root, [ 4, 1, 0 ] ),
  1436. 0
  1437. );
  1438. const transOp = transform( op, transformBy, strongContext );
  1439. expect( transOp.length ).to.equal( 3 );
  1440. expected.sourcePosition.path = [ 2, 2, 4 ];
  1441. expected.howMany = 1;
  1442. expectOperation( transOp[ 0 ], expected );
  1443. expected.sourcePosition.path = [ 4, 1, 0 ];
  1444. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1445. expected.howMany = 2;
  1446. expectOperation( transOp[ 1 ], expected );
  1447. expected.sourcePosition.path = [ 2, 2, 4 ];
  1448. expected.targetPosition = targetPosition.getShiftedBy( 3 );
  1449. expected.howMany = 1;
  1450. expectOperation( transOp[ 2 ], expected );
  1451. } );
  1452. it( 'range and target inside transforming range and is important: no operation update', () => {
  1453. op.howMany = 6;
  1454. const transformBy = new MoveOperation(
  1455. new Position( root, [ 2, 2, 5 ] ),
  1456. 2,
  1457. new Position( root, [ 2, 2, 9 ] ),
  1458. 0
  1459. );
  1460. const transOp = transform( op, transformBy );
  1461. expect( transOp.length ).to.equal( 1 );
  1462. expected.howMany = 6;
  1463. expectOperation( transOp[ 0 ], expected );
  1464. } );
  1465. it( 'range and target inside transforming range and is less important: no operation update', () => {
  1466. op.howMany = 6;
  1467. const transformBy = new MoveOperation(
  1468. new Position( root, [ 2, 2, 5 ] ),
  1469. 2,
  1470. new Position( root, [ 2, 2, 9 ] ),
  1471. 0
  1472. );
  1473. const transOp = transform( op, transformBy, strongContext );
  1474. expect( transOp.length ).to.equal( 1 );
  1475. expected.howMany = 6;
  1476. expectOperation( transOp[ 0 ], expected );
  1477. } );
  1478. } );
  1479. describe( 'by MoveOperation to graveyard', () => {
  1480. let transformBy;
  1481. beforeEach( () => {
  1482. transformBy = new MoveOperation(
  1483. op.sourcePosition.clone(),
  1484. op.howMany,
  1485. new Position( doc.graveyard, [ 0 ] ),
  1486. 0
  1487. );
  1488. } );
  1489. it( 'should skip context.aIsStrong and be less important than MoveOperation', () => {
  1490. const transOp = transform( op, transformBy, strongContext );
  1491. expect( transOp.length ).to.equal( 1 );
  1492. expectOperation( transOp[ 0 ], {
  1493. type: NoOperation
  1494. } );
  1495. } );
  1496. } );
  1497. describe( 'by NoOperation', () => {
  1498. it( 'no operation update', () => {
  1499. const transformBy = new NoOperation( 0 );
  1500. const transOp = transform( op, transformBy );
  1501. expect( transOp.length ).to.equal( 1 );
  1502. expectOperation( transOp[ 0 ], expected );
  1503. } );
  1504. } );
  1505. describe( 'by RenameOperation', () => {
  1506. it( 'no position update', () => {
  1507. const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', 0 );
  1508. const transOp = transform( op, transformBy );
  1509. expect( transOp.length ).to.equal( 1 );
  1510. expectOperation( transOp[ 0 ], expected );
  1511. } );
  1512. } );
  1513. describe( 'by MarkerOperation', () => {
  1514. it( 'no position update', () => {
  1515. const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
  1516. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1517. const transOp = transform( op, transformBy );
  1518. expect( transOp.length ).to.equal( 1 );
  1519. expectOperation( transOp[ 0 ], expected );
  1520. } );
  1521. } );
  1522. } );
  1523. describe( 'MoveOperation to graveyard', () => {
  1524. describe( 'by MoveOperation', () => {
  1525. it( 'should force removing content even if was less important', () => {
  1526. const op = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  1527. const targetPosition = op.targetPosition.clone();
  1528. const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), 0 );
  1529. const sourcePosition = transformBy.targetPosition.clone();
  1530. const transOp = transform( op, transformBy );
  1531. expect( transOp.length ).to.equal( 1 );
  1532. expectOperation( transOp[ 0 ], {
  1533. type: MoveOperation,
  1534. howMany: 2,
  1535. sourcePosition,
  1536. targetPosition
  1537. } );
  1538. } );
  1539. } );
  1540. } );
  1541. describe( 'NoOperation', () => {
  1542. beforeEach( () => {
  1543. op = new NoOperation( 0 );
  1544. expected = {
  1545. type: NoOperation
  1546. };
  1547. } );
  1548. describe( 'by InsertOperation', () => {
  1549. it( 'no operation update', () => {
  1550. const transformBy = new InsertOperation(
  1551. new Position( root, [ 0 ] ),
  1552. 'a',
  1553. 0
  1554. );
  1555. const transOp = transform( op, transformBy );
  1556. expect( transOp.length ).to.equal( 1 );
  1557. expectOperation( transOp[ 0 ], expected );
  1558. } );
  1559. } );
  1560. describe( 'by AttributeOperation', () => {
  1561. it( 'no operation update', () => {
  1562. const transformBy = new AttributeOperation(
  1563. new Range(
  1564. new Position( root, [ 0 ] ),
  1565. new Position( root, [ 1 ] )
  1566. ),
  1567. 'foo',
  1568. 'bar',
  1569. 'xyz',
  1570. 0
  1571. );
  1572. const transOp = transform( op, transformBy );
  1573. expect( transOp.length ).to.equal( 1 );
  1574. expectOperation( transOp[ 0 ], expected );
  1575. } );
  1576. } );
  1577. describe( 'by RootAttributeOperation', () => {
  1578. it( 'no operation update', () => {
  1579. const transformBy = new RootAttributeOperation(
  1580. root,
  1581. 'foo',
  1582. null,
  1583. 'bar',
  1584. 0
  1585. );
  1586. const transOp = transform( op, transformBy );
  1587. expect( transOp.length ).to.equal( 1 );
  1588. expectOperation( transOp[ 0 ], expected );
  1589. } );
  1590. } );
  1591. describe( 'by MoveOperation', () => {
  1592. it( 'no operation update', () => {
  1593. const transformBy = new MoveOperation(
  1594. new Position( root, [ 0 ] ),
  1595. 2,
  1596. new Position( root, [ 1 ] ),
  1597. 0
  1598. );
  1599. const transOp = transform( op, transformBy );
  1600. expect( transOp.length ).to.equal( 1 );
  1601. expectOperation( transOp[ 0 ], expected );
  1602. } );
  1603. } );
  1604. describe( 'by NoOperation', () => {
  1605. it( 'no operation update', () => {
  1606. const transformBy = new NoOperation( 0 );
  1607. const transOp = transform( op, transformBy );
  1608. expect( transOp.length ).to.equal( 1 );
  1609. expectOperation( transOp[ 0 ], expected );
  1610. } );
  1611. } );
  1612. describe( 'by RenameOperation', () => {
  1613. it( 'no position update', () => {
  1614. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  1615. const transOp = transform( op, transformBy );
  1616. expect( transOp.length ).to.equal( 1 );
  1617. expectOperation( transOp[ 0 ], expected );
  1618. } );
  1619. } );
  1620. describe( 'by MarkerOperation', () => {
  1621. it( 'no position update', () => {
  1622. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1623. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1624. const transOp = transform( op, transformBy );
  1625. expect( transOp.length ).to.equal( 1 );
  1626. expectOperation( transOp[ 0 ], expected );
  1627. } );
  1628. } );
  1629. } );
  1630. describe( 'RenameOperation', () => {
  1631. beforeEach( () => {
  1632. const position = new Position( root, [ 0, 2, 2 ] );
  1633. op = new RenameOperation( position, 'oldName', 'newName', 0 );
  1634. expected = {
  1635. position,
  1636. oldName: 'oldName',
  1637. newName: 'newName'
  1638. };
  1639. } );
  1640. describe( 'by InsertOperation', () => {
  1641. it( 'target before renamed element: offset update', () => {
  1642. const transformBy = new InsertOperation(
  1643. new Position( root, [ 0, 2, 1 ] ),
  1644. [ nodeA, nodeB ],
  1645. 0
  1646. );
  1647. const transOp = transform( op, transformBy );
  1648. expect( transOp.length ).to.equal( 1 );
  1649. expected.position.offset = 4;
  1650. expectOperation( transOp[ 0 ], expected );
  1651. } );
  1652. it( 'target after renamed element: no change', () => {
  1653. const transformBy = new InsertOperation(
  1654. new Position( root, [ 0, 2, 3 ] ),
  1655. [ nodeA, nodeB ],
  1656. 0
  1657. );
  1658. const transOp = transform( op, transformBy );
  1659. expect( transOp.length ).to.equal( 1 );
  1660. expectOperation( transOp[ 0 ], expected );
  1661. } );
  1662. it( 'target before a node on path to renamed element: path update', () => {
  1663. const transformBy = new InsertOperation(
  1664. new Position( root, [ 0, 1 ] ),
  1665. [ nodeA, nodeB ],
  1666. 0
  1667. );
  1668. const transOp = transform( op, transformBy );
  1669. expect( transOp.length ).to.equal( 1 );
  1670. expected.position.path = [ 0, 4, 2 ];
  1671. expectOperation( transOp[ 0 ], expected );
  1672. } );
  1673. it( 'target after a node on path to renamed element: no change', () => {
  1674. const transformBy = new InsertOperation(
  1675. new Position( root, [ 0, 3 ] ),
  1676. [ nodeA, nodeB ],
  1677. 0
  1678. );
  1679. const transOp = transform( op, transformBy );
  1680. expect( transOp.length ).to.equal( 1 );
  1681. expectOperation( transOp[ 0 ], expected );
  1682. } );
  1683. } );
  1684. describe( 'by AttributeOperation', () => {
  1685. it( 'no operation update', () => {
  1686. const transformBy = new AttributeOperation(
  1687. new Range(
  1688. new Position( root, [ 0, 2, 1 ] ),
  1689. new Position( root, [ 1, 3 ] )
  1690. ),
  1691. 'foo',
  1692. 'bar',
  1693. 'xyz',
  1694. 0
  1695. );
  1696. const transOp = transform( op, transformBy );
  1697. expect( transOp.length ).to.equal( 1 );
  1698. expectOperation( transOp[ 0 ], expected );
  1699. } );
  1700. } );
  1701. describe( 'by RootAttributeOperation', () => {
  1702. it( 'no operation update', () => {
  1703. const transformBy = new RootAttributeOperation(
  1704. root,
  1705. 'foo',
  1706. null,
  1707. 'bar',
  1708. 0
  1709. );
  1710. const transOp = transform( op, transformBy );
  1711. expect( transOp.length ).to.equal( 1 );
  1712. expectOperation( transOp[ 0 ], expected );
  1713. } );
  1714. } );
  1715. describe( 'by MarkerOperation', () => {
  1716. it( 'no operation update', () => {
  1717. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1718. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1719. const transOp = transform( op, transformBy );
  1720. expect( transOp.length ).to.equal( 1 );
  1721. expectOperation( transOp[ 0 ], expected );
  1722. } );
  1723. } );
  1724. describe( 'by RenameOperation', () => {
  1725. it( 'different element: no change', () => {
  1726. const transformBy = new RenameOperation(
  1727. new Position( root, [ 0, 2, 1 ] ),
  1728. 'foo',
  1729. 'bar',
  1730. 0
  1731. );
  1732. const transOp = transform( op, transformBy );
  1733. expect( transOp.length ).to.equal( 1 );
  1734. expectOperation( transOp[ 0 ], expected );
  1735. } );
  1736. it( 'same element and is important: convert to NoOperation', () => {
  1737. const transformBy = new RenameOperation(
  1738. new Position( root, [ 0, 2, 2 ] ),
  1739. 'oldName',
  1740. 'otherName',
  1741. 0
  1742. );
  1743. const transOp = transform( op, transformBy );
  1744. expect( transOp.length ).to.equal( 1 );
  1745. expectOperation( transOp[ 0 ], {
  1746. type: NoOperation
  1747. } );
  1748. } );
  1749. it( 'same element and is not important: change old name to new name', () => {
  1750. const transformBy = new RenameOperation(
  1751. new Position( root, [ 0, 2, 2 ] ),
  1752. 'oldName',
  1753. 'otherName',
  1754. 0
  1755. );
  1756. const transOp = transform( op, transformBy, strongContext );
  1757. expect( transOp.length ).to.equal( 1 );
  1758. expected.oldName = 'otherName';
  1759. expectOperation( transOp[ 0 ], expected );
  1760. } );
  1761. } );
  1762. describe( 'by MoveOperation', () => {
  1763. it( 'moved range before renamed element: update offset', () => {
  1764. const transformBy = new MoveOperation(
  1765. new Position( root, [ 0, 2, 0 ] ),
  1766. 2,
  1767. new Position( root, [ 2, 5 ] ),
  1768. 0
  1769. );
  1770. const transOp = transform( op, transformBy );
  1771. expect( transOp.length ).to.equal( 1 );
  1772. expected.position.offset = 0;
  1773. expectOperation( transOp[ 0 ], expected );
  1774. } );
  1775. it( 'moved range before an element on path to renamed element: update path', () => {
  1776. const transformBy = new MoveOperation(
  1777. new Position( root, [ 0, 0 ] ),
  1778. 2,
  1779. new Position( root, [ 2, 5 ] ),
  1780. 0
  1781. );
  1782. const transOp = transform( op, transformBy );
  1783. expect( transOp.length ).to.equal( 1 );
  1784. expected.position.path = [ 0, 0, 2 ];
  1785. expectOperation( transOp[ 0 ], expected );
  1786. } );
  1787. it( 'moved range contains renamed element: update path', () => {
  1788. const transformBy = new MoveOperation(
  1789. new Position( root, [ 0, 2, 1 ] ),
  1790. 3,
  1791. new Position( root, [ 2, 5 ] ),
  1792. 0
  1793. );
  1794. const transOp = transform( op, transformBy );
  1795. expect( transOp.length ).to.equal( 1 );
  1796. expected.position.path = [ 2, 6 ];
  1797. expectOperation( transOp[ 0 ], expected );
  1798. } );
  1799. it( 'moved range contains renamed element parent: updated path', () => {
  1800. const transformBy = new MoveOperation(
  1801. new Position( root, [ 0, 1 ] ),
  1802. 3,
  1803. new Position( root, [ 2, 5 ] ),
  1804. 0
  1805. );
  1806. const transOp = transform( op, transformBy );
  1807. expect( transOp.length ).to.equal( 1 );
  1808. expected.position.path = [ 2, 6, 2 ];
  1809. expectOperation( transOp[ 0 ], expected );
  1810. } );
  1811. it( 'move target before renamed element: update offset', () => {
  1812. const transformBy = new MoveOperation(
  1813. new Position( root, [ 2, 5 ] ),
  1814. 2,
  1815. new Position( root, [ 0, 2, 1 ] ),
  1816. 0
  1817. );
  1818. const transOp = transform( op, transformBy );
  1819. expect( transOp.length ).to.equal( 1 );
  1820. expected.position.offset = 4;
  1821. expectOperation( transOp[ 0 ], expected );
  1822. } );
  1823. it( 'move target before an element on path to renamed element: update path', () => {
  1824. const transformBy = new MoveOperation(
  1825. new Position( root, [ 0, 2, 0 ] ),
  1826. 2,
  1827. new Position( root, [ 2, 5 ] ),
  1828. 0
  1829. );
  1830. const transOp = transform( op, transformBy );
  1831. expect( transOp.length ).to.equal( 1 );
  1832. expected.position.offset = 0;
  1833. expectOperation( transOp[ 0 ], expected );
  1834. } );
  1835. } );
  1836. } );
  1837. describe( 'MarkerOperation', () => {
  1838. let oldRange, newRange;
  1839. beforeEach( () => {
  1840. oldRange = new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) );
  1841. newRange = new Range( Position._createAt( root, 10 ), Position._createAt( root, 12 ) );
  1842. op = new MarkerOperation( 'name', oldRange, newRange, model.markers, false, 0 );
  1843. expected = {
  1844. name: 'name',
  1845. oldRange,
  1846. newRange
  1847. };
  1848. } );
  1849. describe( 'by InsertOperation', () => {
  1850. it( 'insert position affecting oldRange: update oldRange', () => {
  1851. // Just CC things.
  1852. op.newRange = null;
  1853. const transformBy = new InsertOperation( Position._createAt( root, 0 ), [ nodeA, nodeB ], 0 );
  1854. const transOp = transform( op, transformBy );
  1855. expected.newRange = null;
  1856. expected.oldRange.start.offset = 3;
  1857. expected.oldRange.end.offset = 6;
  1858. expect( transOp.length ).to.equal( 1 );
  1859. expectOperation( transOp[ 0 ], expected );
  1860. } );
  1861. it( 'insert position affecting newRange: update newRange', () => {
  1862. // Just CC things.
  1863. op.oldRange = null;
  1864. const transformBy = new InsertOperation( Position._createAt( root, 8 ), [ nodeA, nodeB ], 0 );
  1865. const transOp = transform( op, transformBy );
  1866. expected.oldRange = null;
  1867. expected.newRange.start.offset = 12;
  1868. expected.newRange.end.offset = 14;
  1869. expect( transOp.length ).to.equal( 1 );
  1870. expectOperation( transOp[ 0 ], expected );
  1871. } );
  1872. } );
  1873. describe( 'by AttributeOperation', () => {
  1874. it( 'no operation update', () => {
  1875. const transformBy = new AttributeOperation(
  1876. new Range(
  1877. new Position( root, [ 2 ] ),
  1878. new Position( root, [ 11 ] )
  1879. ),
  1880. 'foo',
  1881. 'bar',
  1882. 'xyz',
  1883. 0
  1884. );
  1885. const transOp = transform( op, transformBy );
  1886. expect( transOp.length ).to.equal( 1 );
  1887. expectOperation( transOp[ 0 ], expected );
  1888. } );
  1889. } );
  1890. describe( 'by MoveOperation', () => {
  1891. it( 'moved range is before oldRange: update oldRange', () => {
  1892. // Just CC things.
  1893. op.newRange = null;
  1894. const transformBy = new MoveOperation( Position._createAt( root, 0 ), 1, Position._createAt( root, 20 ), 0 );
  1895. const transOp = transform( op, transformBy );
  1896. expected.newRange = null;
  1897. expected.oldRange.start.offset = 0;
  1898. expected.oldRange.end.offset = 3;
  1899. expect( transOp.length ).to.equal( 1 );
  1900. expectOperation( transOp[ 0 ], expected );
  1901. } );
  1902. it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
  1903. const transformBy = new MoveOperation( Position._createAt( root, 2 ), 2, Position._createAt( root, 20 ), 0 );
  1904. const transOp = transform( op, transformBy );
  1905. expected.oldRange.start.offset = 1;
  1906. expected.oldRange.end.offset = 2;
  1907. expected.newRange.start.offset = 8;
  1908. expected.newRange.end.offset = 10;
  1909. expect( transOp.length ).to.equal( 1 );
  1910. expectOperation( transOp[ 0 ], expected );
  1911. } );
  1912. it( 'target position is inside newRange: update newRange', () => {
  1913. // Just CC things.
  1914. op.oldRange = null;
  1915. const transformBy = new MoveOperation( Position._createAt( root, 20 ), 2, Position._createAt( root, 11 ), 0 );
  1916. const transOp = transform( op, transformBy );
  1917. expected.oldRange = null;
  1918. expected.newRange.start.offset = 10;
  1919. expected.newRange.end.offset = 14;
  1920. expect( transOp.length ).to.equal( 1 );
  1921. expectOperation( transOp[ 0 ], expected );
  1922. } );
  1923. it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
  1924. const transformBy = new MoveOperation( Position._createAt( root, 20 ), 4, Position._createAt( root, 2 ), 0 );
  1925. const transOp = transform( op, transformBy );
  1926. expected.oldRange.start.offset = 1;
  1927. expected.oldRange.end.offset = 8;
  1928. expected.newRange.start.offset = 14;
  1929. expected.newRange.end.offset = 16;
  1930. expect( transOp.length ).to.equal( 1 );
  1931. expectOperation( transOp[ 0 ], expected );
  1932. } );
  1933. } );
  1934. describe( 'by RootAttributeOperation', () => {
  1935. it( 'no operation update', () => {
  1936. const transformBy = new RootAttributeOperation(
  1937. root,
  1938. 'foo',
  1939. null,
  1940. 'bar',
  1941. 0
  1942. );
  1943. const transOp = transform( op, transformBy );
  1944. expect( transOp.length ).to.equal( 1 );
  1945. expectOperation( transOp[ 0 ], expected );
  1946. } );
  1947. } );
  1948. describe( 'by RenameOperation', () => {
  1949. it( 'no operation update', () => {
  1950. const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', 0 );
  1951. const transOp = transform( op, transformBy );
  1952. expect( transOp.length ).to.equal( 1 );
  1953. expectOperation( transOp[ 0 ], expected );
  1954. } );
  1955. } );
  1956. describe( 'by MarkerOperation', () => {
  1957. it( 'different marker name: no operation update', () => {
  1958. const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, model.markers, false, 0 );
  1959. const transOp = transform( op, transformBy );
  1960. expect( transOp.length ).to.equal( 1 );
  1961. expectOperation( transOp[ 0 ], expected );
  1962. } );
  1963. it( 'same marker name and is important: convert to NoOperation', () => {
  1964. const anotherRange = new Range( Position._createAt( root, 2 ), Position._createAt( root, 2 ) );
  1965. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, false, 0 );
  1966. const transOp = transform( op, transformBy );
  1967. expect( transOp.length ).to.equal( 1 );
  1968. expectOperation( transOp[ 0 ], {
  1969. type: NoOperation
  1970. } );
  1971. } );
  1972. it( 'same marker name and is less important: update oldRange parameter', () => {
  1973. const anotherRange = new Range( Position._createAt( root, 2 ), Position._createAt( root, 2 ) );
  1974. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, false, 0 );
  1975. const transOp = transform( op, transformBy, strongContext );
  1976. expected.oldRange = anotherRange;
  1977. expect( transOp.length ).to.equal( 1 );
  1978. expectOperation( transOp[ 0 ], expected );
  1979. } );
  1980. } );
  1981. } );
  1982. } );