transform.js 70 KB

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