transform.js 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transform from '../../../src/model/operation/transform';
  6. import Document from '../../../src/model/document';
  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 doc, root, op, nodeA, nodeB, expected, baseVersion;
  21. beforeEach( () => {
  22. doc = new Document();
  23. root = doc.createRoot();
  24. nodeA = new Node();
  25. nodeB = new Node();
  26. baseVersion = 0;
  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 ], baseVersion );
  54. expected = {
  55. type: InsertOperation,
  56. position: Position.createFromPosition( position ),
  57. baseVersion: baseVersion + 1
  58. };
  59. } );
  60. describe( 'by InsertOperation', () => {
  61. it( 'target at different position: no position update', () => {
  62. const transformBy = new InsertOperation(
  63. new Position( root, [ 1, 3, 2 ] ),
  64. [ nodeC, nodeD ],
  65. baseVersion
  66. );
  67. const transOp = transform( op, transformBy );
  68. expect( transOp.length ).to.equal( 1 );
  69. expectOperation( transOp[ 0 ], expected );
  70. } );
  71. it( 'target at offset before: increment offset', () => {
  72. const transformBy = new InsertOperation(
  73. new Position( root, [ 0, 2, 0 ] ),
  74. [ nodeC, nodeD ],
  75. baseVersion
  76. );
  77. const transOp = transform( op, transformBy );
  78. expected.position.offset += 2;
  79. expect( transOp.length ).to.equal( 1 );
  80. expectOperation( transOp[ 0 ], expected );
  81. } );
  82. it( 'target at same offset and is important: increment offset', () => {
  83. const transformBy = new InsertOperation(
  84. new Position( root, [ 0, 2, 2 ] ),
  85. [ nodeC, nodeD ],
  86. baseVersion
  87. );
  88. const transOp = transform( op, transformBy );
  89. expected.position.offset += 2;
  90. expect( transOp.length ).to.equal( 1 );
  91. expectOperation( transOp[ 0 ], expected );
  92. } );
  93. it( 'target at same offset and is less important: no position update', () => {
  94. const transformBy = new InsertOperation(
  95. new Position( root, [ 0, 2, 2 ] ),
  96. [ nodeC, nodeD ],
  97. baseVersion
  98. );
  99. const transOp = transform( transformBy, { isStrong: true } );
  100. expect( transOp.length ).to.equal( 1 );
  101. expectOperation( transOp[ 0 ], expected );
  102. } );
  103. it( 'target at same offset and context.insertBefore = true: no position update', () => {
  104. const transformBy = new InsertOperation(
  105. new Position( root, [ 0, 2, 2 ] ),
  106. [ nodeC, nodeD ],
  107. baseVersion
  108. );
  109. const transOp = transform( transformBy, { isStrong: false, insertBefore: false } );
  110. expect( transOp.length ).to.equal( 1 );
  111. expectOperation( transOp[ 0 ], expected );
  112. } );
  113. it( 'target at same offset and context.insertBefore = false: increment offset', () => {
  114. const transformBy = new InsertOperation(
  115. new Position( root, [ 0, 2, 2 ] ),
  116. [ nodeC, nodeD ],
  117. baseVersion
  118. );
  119. const transOp = transform( op, transformBy );
  120. expected.position.offset += 2;
  121. expect( transOp.length ).to.equal( 1 );
  122. expectOperation( transOp[ 0 ], expected );
  123. } );
  124. it( 'target at offset after: no position update', () => {
  125. const transformBy = new InsertOperation(
  126. new Position( root, [ 0, 2, 3 ] ),
  127. [ nodeC, nodeD ],
  128. baseVersion
  129. );
  130. const transOp = transform( op, transformBy );
  131. expect( transOp.length ).to.equal( 1 );
  132. expectOperation( transOp[ 0 ], expected );
  133. } );
  134. it( 'target before node from path: increment index on path', () => {
  135. const transformBy = new InsertOperation(
  136. new Position( root, [ 0, 1 ] ),
  137. [ nodeC, nodeD ],
  138. baseVersion
  139. );
  140. const transOp = transform( op, transformBy );
  141. expected.position.path[ 1 ] += 2;
  142. expect( transOp.length ).to.equal( 1 );
  143. expectOperation( transOp[ 0 ], expected );
  144. } );
  145. it( 'target after node from path: no position update', () => {
  146. const transformBy = new InsertOperation(
  147. new Position( root, [ 0, 6 ] ),
  148. [ nodeC, nodeD ],
  149. baseVersion
  150. );
  151. const transOp = transform( op, transformBy );
  152. expect( transOp.length ).to.equal( 1 );
  153. expectOperation( transOp[ 0 ], expected );
  154. } );
  155. } );
  156. describe( 'by AttributeOperation', () => {
  157. it( 'no position update', () => {
  158. const transformBy = new AttributeOperation(
  159. Range.createFromPositionAndShift( position, 2 ),
  160. 'foo',
  161. null,
  162. 'bar',
  163. baseVersion
  164. );
  165. const transOp = transform( op, transformBy );
  166. expect( transOp.length ).to.equal( 1 );
  167. expectOperation( transOp[ 0 ], expected );
  168. } );
  169. } );
  170. describe( 'by RootAttributeOperation', () => {
  171. it( 'no position update', () => {
  172. const transformBy = new RootAttributeOperation(
  173. root,
  174. 'foo',
  175. null,
  176. 'bar',
  177. baseVersion
  178. );
  179. const transOp = transform( op, transformBy );
  180. expect( transOp.length ).to.equal( 1 );
  181. expectOperation( transOp[ 0 ], expected );
  182. } );
  183. } );
  184. describe( 'by MoveOperation', () => {
  185. it( 'range and target are different than insert position: no position update', () => {
  186. const transformBy = new MoveOperation(
  187. new Position( root, [ 1, 3, 2 ] ),
  188. 2,
  189. new Position( root, [ 2, 1 ] ),
  190. baseVersion
  191. );
  192. const transOp = transform( op, transformBy );
  193. expect( transOp.length ).to.equal( 1 );
  194. expectOperation( transOp[ 0 ], expected );
  195. } );
  196. it( 'range is before insert position offset: decrement offset', () => {
  197. const transformBy = new MoveOperation(
  198. new Position( root, [ 0, 2, 0 ] ),
  199. 1,
  200. new Position( root, [ 1, 1 ] ),
  201. baseVersion
  202. );
  203. const transOp = transform( op, transformBy );
  204. expected.position.offset--;
  205. expect( transOp.length ).to.equal( 1 );
  206. expectOperation( transOp[ 0 ], expected );
  207. } );
  208. it( 'range offset is after insert position offset: no position update', () => {
  209. const transformBy = new MoveOperation(
  210. new Position( root, [ 0, 2, 5 ] ),
  211. 1,
  212. new Position( root, [ 1, 1 ] ),
  213. baseVersion
  214. );
  215. const transOp = transform( op, transformBy );
  216. expect( transOp.length ).to.equal( 1 );
  217. expectOperation( transOp[ 0 ], expected );
  218. } );
  219. it( 'target offset before insert position offset: increment offset', () => {
  220. const transformBy = new MoveOperation(
  221. new Position( root, [ 1, 1 ] ),
  222. 2,
  223. new Position( root, [ 0, 2, 0 ] ),
  224. baseVersion
  225. );
  226. const transOp = transform( op, transformBy );
  227. expected.position.offset += 2;
  228. expect( transOp.length ).to.equal( 1 );
  229. expectOperation( transOp[ 0 ], expected );
  230. } );
  231. it( 'target offset after insert position offset: no position update', () => {
  232. const transformBy = new MoveOperation(
  233. new Position( root, [ 1, 1 ] ),
  234. 2,
  235. new Position( root, [ 0, 2, 5 ] ),
  236. baseVersion
  237. );
  238. const transOp = transform( op, transformBy );
  239. expect( transOp.length ).to.equal( 1 );
  240. expectOperation( transOp[ 0 ], expected );
  241. } );
  242. it( 'target offset same as insert position offset and is important: increment offset', () => {
  243. const transformBy = new MoveOperation(
  244. new Position( root, [ 1, 1 ] ),
  245. 2,
  246. new Position( root, [ 0, 2, 2 ] ),
  247. baseVersion
  248. );
  249. const transOp = transform( op, transformBy );
  250. expected.position.offset += 2;
  251. expect( transOp.length ).to.equal( 1 );
  252. expectOperation( transOp[ 0 ], expected );
  253. } );
  254. it( 'target offset same as insert position offset and is less important: no position update', () => {
  255. const transformBy = new MoveOperation(
  256. new Position( root, [ 1, 1 ] ),
  257. 2,
  258. new Position( root, [ 0, 2, 2 ] ),
  259. baseVersion
  260. );
  261. const transOp = transform( op, transformBy, { isStrong: true } );
  262. expect( transOp.length ).to.equal( 1 );
  263. expectOperation( transOp[ 0 ], expected );
  264. } );
  265. it( 'target offset same as insert position offset and context.insertBefore = true: increment offset', () => {
  266. const transformBy = new MoveOperation(
  267. new Position( root, [ 1, 1 ] ),
  268. 2,
  269. new Position( root, [ 0, 2, 2 ] ),
  270. baseVersion
  271. );
  272. const transOp = transform( op, transformBy, { isStrong: true, insertBefore: true } );
  273. expected.position.offset += 2;
  274. expect( transOp.length ).to.equal( 1 );
  275. expectOperation( transOp[ 0 ], expected );
  276. } );
  277. it( 'target offset same as insert position offset and context.insertBefore = false: no position update', () => {
  278. const transformBy = new MoveOperation(
  279. new Position( root, [ 1, 1 ] ),
  280. 2,
  281. new Position( root, [ 0, 2, 2 ] ),
  282. baseVersion
  283. );
  284. const transOp = transform( op, transformBy, { isStrong: false, insertBefore: false } );
  285. expect( transOp.length ).to.equal( 1 );
  286. expectOperation( transOp[ 0 ], expected );
  287. } );
  288. it( 'range is before node from insert position path: decrement index on path', () => {
  289. const transformBy = new MoveOperation(
  290. new Position( root, [ 0, 0 ] ),
  291. 1,
  292. new Position( root, [ 1, 0 ] ),
  293. baseVersion
  294. );
  295. const transOp = transform( op, transformBy );
  296. expected.position.path[ 1 ] -= 1;
  297. expect( transOp.length ).to.equal( 1 );
  298. expectOperation( transOp[ 0 ], expected );
  299. } );
  300. it( 'range is after node from insert position path: no position update', () => {
  301. const transformBy = new MoveOperation(
  302. new Position( root, [ 0, 4 ] ),
  303. 2,
  304. new Position( root, [ 1, 0 ] ),
  305. baseVersion
  306. );
  307. const transOp = transform( op, transformBy );
  308. expect( transOp.length ).to.equal( 1 );
  309. expectOperation( transOp[ 0 ], expected );
  310. } );
  311. it( 'target before node from insert position path: increment index on path', () => {
  312. const transformBy = new MoveOperation(
  313. new Position( root, [ 1, 0 ] ),
  314. 2,
  315. new Position( root, [ 0, 0 ] ),
  316. baseVersion
  317. );
  318. const transOp = transform( op, transformBy );
  319. expected.position.path[ 1 ] += 2;
  320. expect( transOp.length ).to.equal( 1 );
  321. expectOperation( transOp[ 0 ], expected );
  322. } );
  323. it( 'target after node from insert position path: no position update', () => {
  324. const transformBy = new MoveOperation(
  325. new Position( root, [ 1, 0 ] ),
  326. 2,
  327. new Position( root, [ 0, 4 ] ),
  328. baseVersion
  329. );
  330. const transOp = transform( op, transformBy );
  331. expect( transOp.length ).to.equal( 1 );
  332. expectOperation( transOp[ 0 ], expected );
  333. } );
  334. it( 'range has node that contains insert position: update position', () => {
  335. const transformBy = new MoveOperation(
  336. new Position( root, [ 0, 1 ] ),
  337. 2,
  338. new Position( root, [ 1, 1 ] ),
  339. baseVersion
  340. );
  341. const transOp = transform( op, transformBy );
  342. expected.position.path = [ 1, 2, 2 ];
  343. expect( transOp.length ).to.equal( 1 );
  344. expectOperation( transOp[ 0 ], expected );
  345. } );
  346. it( 'range contains insert position (on same level): update position', () => {
  347. const transformBy = new MoveOperation(
  348. new Position( root, [ 0, 2, 0 ] ),
  349. 4,
  350. new Position( root, [ 1, 0 ] ),
  351. baseVersion
  352. );
  353. const transOp = transform( op, transformBy );
  354. expected.position.path = [ 1, 2 ];
  355. expect( transOp.length ).to.equal( 1 );
  356. expectOperation( transOp[ 0 ], expected );
  357. } );
  358. } );
  359. describe( 'by NoOperation', () => {
  360. it( 'no operation update', () => {
  361. const transformBy = new NoOperation( baseVersion );
  362. const transOp = transform( op, transformBy );
  363. expect( transOp.length ).to.equal( 1 );
  364. expectOperation( transOp[ 0 ], expected );
  365. } );
  366. } );
  367. describe( 'by RenameOperation', () => {
  368. it( 'no position update', () => {
  369. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', baseVersion );
  370. const transOp = transform( op, transformBy );
  371. expect( transOp.length ).to.equal( 1 );
  372. expectOperation( transOp[ 0 ], expected );
  373. } );
  374. } );
  375. describe( 'by MarkerOperation', () => {
  376. it( 'no position update', () => {
  377. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  378. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  379. const transOp = transform( op, transformBy );
  380. expect( transOp.length ).to.equal( 1 );
  381. expectOperation( transOp[ 0 ], expected );
  382. } );
  383. } );
  384. } );
  385. describe( 'AttributeOperation', () => {
  386. let start, end, range;
  387. beforeEach( () => {
  388. expected = {
  389. type: AttributeOperation,
  390. key: 'foo',
  391. oldValue: 'abc',
  392. newValue: 'bar',
  393. baseVersion: baseVersion + 1
  394. };
  395. } );
  396. describe( 'with multi-level range', () => {
  397. beforeEach( () => {
  398. start = new Position( root, [ 1, 2 ] );
  399. end = new Position( root, [ 2, 2, 4 ] );
  400. range = new Range( start, end );
  401. op = new AttributeOperation( range, 'foo', 'abc', 'bar', baseVersion );
  402. expected.range = new Range( start, end );
  403. } );
  404. describe( 'by InsertOperation', () => {
  405. it( 'target at different position: no operation update', () => {
  406. const transformBy = new InsertOperation(
  407. new Position( root, [ 3, 3, 2 ] ),
  408. [ nodeA, nodeB ],
  409. baseVersion
  410. );
  411. const transOp = transform( op, transformBy );
  412. expect( transOp.length ).to.equal( 1 );
  413. expectOperation( transOp[ 0 ], expected );
  414. } );
  415. it( 'target at offset before: increment offset', () => {
  416. const transformBy = new InsertOperation(
  417. new Position( root, [ 1, 1 ] ),
  418. [ nodeA, nodeB ],
  419. baseVersion
  420. );
  421. const transOp = transform( op, transformBy );
  422. expected.range.start.offset += 2;
  423. expect( transOp.length ).to.equal( 1 );
  424. expectOperation( transOp[ 0 ], expected );
  425. } );
  426. it( 'target at same offset: increment offset', () => {
  427. const transformBy = new InsertOperation(
  428. new Position( root, [ 1, 2 ] ),
  429. [ nodeA, nodeB ],
  430. baseVersion
  431. );
  432. const transOp = transform( op, transformBy );
  433. expected.range.start.offset += 2;
  434. expect( transOp.length ).to.equal( 1 );
  435. expectOperation( transOp[ 0 ], expected );
  436. } );
  437. it( 'target at offset after: no operation update', () => {
  438. const transformBy = new InsertOperation(
  439. new Position( root, [ 3, 2 ] ),
  440. [ nodeA, nodeB ],
  441. baseVersion
  442. );
  443. const transOp = transform( op, transformBy );
  444. expect( transOp.length ).to.equal( 1 );
  445. expectOperation( transOp[ 0 ], expected );
  446. } );
  447. it( 'target before node from path: increment index on path', () => {
  448. const transformBy = new InsertOperation(
  449. new Position( root, [ 0 ] ),
  450. [ nodeA, nodeB ],
  451. baseVersion
  452. );
  453. const transOp = transform( op, transformBy );
  454. expected.range.start.path[ 0 ] += 2;
  455. expected.range.end.path[ 0 ] += 2;
  456. expect( transOp.length ).to.equal( 1 );
  457. expectOperation( transOp[ 0 ], expected );
  458. } );
  459. it( 'target after node from path: no position change', () => {
  460. const transformBy = new InsertOperation(
  461. new Position( root, [ 2, 6 ] ),
  462. [ nodeA, nodeB ],
  463. baseVersion
  464. );
  465. const transOp = transform( op, transformBy );
  466. expect( transOp.length ).to.equal( 1 );
  467. expectOperation( transOp[ 0 ], expected );
  468. } );
  469. it( 'target inside change range: split into two operations', () => {
  470. const transformBy = new InsertOperation(
  471. new Position( root, [ 1, 3, 1 ] ),
  472. [ nodeA, nodeB ],
  473. baseVersion
  474. );
  475. const transOp = transform( op, transformBy );
  476. expect( transOp.length ).to.equal( 2 );
  477. expected.range.start.path = [ 1, 3, 3 ];
  478. expectOperation( transOp[ 0 ], expected );
  479. expected.range.start = op.range.start;
  480. expected.range.end.path = [ 1, 3, 1 ];
  481. expected.baseVersion++;
  482. expectOperation( transOp[ 1 ], expected );
  483. } );
  484. } );
  485. describe( 'by AttributeOperation', () => {
  486. it( 'attributes have different key: no operation update', () => {
  487. const transformBy = new AttributeOperation(
  488. Range.createFromRange( range ),
  489. 'differentKey',
  490. true,
  491. false,
  492. baseVersion
  493. );
  494. const transOp = transform( op, transformBy );
  495. expect( transOp.length ).to.equal( 1 );
  496. expectOperation( transOp[ 0 ], expected );
  497. } );
  498. it( 'same key and value: convert to NoOperation', () => {
  499. const transformBy = new AttributeOperation(
  500. Range.createFromRange( range ),
  501. 'foo',
  502. 'abc',
  503. 'bar',
  504. baseVersion
  505. );
  506. const transOp = transform( op, transformBy );
  507. expect( transOp.length ).to.equal( 1 );
  508. expectOperation( transOp[ 0 ], {
  509. type: NoOperation,
  510. baseVersion: baseVersion + 1
  511. } );
  512. } );
  513. describe( 'that is less important and', () => {
  514. it( 'both operations removes same attribute: change old value to null', () => {
  515. op.newValue = null;
  516. expected.newValue = null;
  517. const transformBy = new AttributeOperation(
  518. new Range( new Position( root, [ 1, 1, 4 ] ), new Position( root, [ 3 ] ) ),
  519. 'foo',
  520. 'another',
  521. null,
  522. baseVersion
  523. );
  524. const transOp = transform( op, transformBy, { isStrong: true } );
  525. expected.oldValue = null;
  526. expect( transOp.length ).to.equal( 1 );
  527. } );
  528. it( 'range does not intersect original range: no operation update', () => {
  529. const transformBy = new AttributeOperation(
  530. new Range( new Position( root, [ 3, 0 ] ), new Position( root, [ 4 ] ) ),
  531. 'foo',
  532. 'abc',
  533. null,
  534. baseVersion
  535. );
  536. const transOp = transform( op, transformBy, { isStrong: true } );
  537. expect( transOp.length ).to.equal( 1 );
  538. expectOperation( transOp[ 0 ], expected );
  539. } );
  540. it( 'range contains original range: update oldValue', () => {
  541. const transformBy = new AttributeOperation(
  542. new Range( new Position( root, [ 1, 1, 4 ] ), new Position( root, [ 3 ] ) ),
  543. 'foo',
  544. 'abc',
  545. null,
  546. baseVersion
  547. );
  548. const transOp = transform( op, transformBy, { isStrong: true } );
  549. expected.oldValue = null;
  550. expect( transOp.length ).to.equal( 1 );
  551. expectOperation( transOp[ 0 ], expected );
  552. } );
  553. // [ original range < ] range transformed by >
  554. it( 'range intersects on left: split into two operations, update oldValue', () => {
  555. // Get more test cases and better code coverage
  556. op.newValue = null;
  557. expected.newValue = null;
  558. const transformBy = new AttributeOperation(
  559. new Range( new Position( root, [ 1, 4, 2 ] ), new Position( root, [ 3 ] ) ),
  560. 'foo',
  561. null,
  562. // Get more test cases and better code coverage
  563. 'another',
  564. baseVersion
  565. );
  566. const transOp = transform( op, transformBy, { isStrong: true } );
  567. expect( transOp.length ).to.equal( 2 );
  568. expected.range.end.path = [ 1, 4, 2 ];
  569. expectOperation( transOp[ 0 ], expected );
  570. expected.range.start.path = [ 1, 4, 2 ];
  571. expected.range.end = op.range.end;
  572. expected.oldValue = 'another';
  573. expected.baseVersion++;
  574. expectOperation( transOp[ 1 ], expected );
  575. } );
  576. // [ range transformed by < ] original range >
  577. it( 'range intersects on right: split into two operations, update oldValue', () => {
  578. const transformBy = new AttributeOperation(
  579. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 1 ] ) ),
  580. 'foo',
  581. 'abc',
  582. null,
  583. baseVersion
  584. );
  585. const transOp = transform( op, transformBy, { isStrong: true } );
  586. expect( transOp.length ).to.equal( 2 );
  587. expected.range.start.path = [ 2, 1 ];
  588. expectOperation( transOp[ 0 ], expected );
  589. expected.range.start = op.range.start;
  590. expected.range.end.path = [ 2, 1 ];
  591. expected.oldValue = null;
  592. expected.baseVersion++;
  593. expectOperation( transOp[ 1 ], expected );
  594. } );
  595. it( 'range is inside original range: split into three operations, update oldValue', () => {
  596. const transformBy = new AttributeOperation(
  597. new Range( new Position( root, [ 1, 4, 1 ] ), new Position( root, [ 2, 1 ] ) ),
  598. 'foo',
  599. 'abc',
  600. null,
  601. baseVersion
  602. );
  603. const transOp = transform( op, transformBy, { isStrong: true } );
  604. expect( transOp.length ).to.equal( 3 );
  605. expected.range.end.path = [ 1, 4, 1 ];
  606. expectOperation( transOp[ 0 ], expected );
  607. expected.range.start.path = [ 2, 1 ];
  608. expected.range.end = op.range.end;
  609. expected.baseVersion++;
  610. expectOperation( transOp[ 1 ], expected );
  611. expected.range.start.path = [ 1, 4, 1 ];
  612. expected.range.end.path = [ 2, 1 ];
  613. expected.oldValue = null;
  614. expected.baseVersion++;
  615. expectOperation( transOp[ 2 ], expected );
  616. } );
  617. } );
  618. describe( 'that is more important and', () => {
  619. it( 'both operations removes same attribute: convert to NoOperation', () => {
  620. op.newValue = null;
  621. expected.newValue = null;
  622. const transformBy = new AttributeOperation(
  623. new Range( new Position( root, [ 1, 1, 4 ] ), new Position( root, [ 3 ] ) ),
  624. 'foo',
  625. 'another',
  626. null,
  627. baseVersion
  628. );
  629. const transOp = transform( op, transformBy );
  630. expect( transOp.length ).to.equal( 1 );
  631. expectOperation( transOp[ 0 ], {
  632. type: NoOperation,
  633. baseVersion: baseVersion + 1
  634. } );
  635. } );
  636. it( 'range does not intersect original range: no operation update', () => {
  637. const transformBy = new AttributeOperation(
  638. new Range( new Position( root, [ 3, 0 ] ), new Position( root, [ 4 ] ) ),
  639. 'foo',
  640. 'abc',
  641. null,
  642. baseVersion
  643. );
  644. const transOp = transform( op, transformBy );
  645. expect( transOp.length ).to.equal( 1 );
  646. expectOperation( transOp[ 0 ], expected );
  647. } );
  648. it( 'range contains original range: convert to NoOperation', () => {
  649. const transformBy = new AttributeOperation(
  650. new Range( new Position( root, [ 1, 1, 4 ] ), new Position( root, [ 3 ] ) ),
  651. 'foo',
  652. 'abc',
  653. null,
  654. baseVersion
  655. );
  656. const transOp = transform( op, transformBy );
  657. expect( transOp.length ).to.equal( 1 );
  658. expectOperation( transOp[ 0 ], {
  659. type: NoOperation,
  660. baseVersion: baseVersion + 1
  661. } );
  662. } );
  663. // [ original range < ] range transformed by >
  664. it( 'range intersects on left: shrink original range', () => {
  665. const transformBy = new AttributeOperation(
  666. new Range( new Position( root, [ 1, 4, 2 ] ), new Position( root, [ 3 ] ) ),
  667. 'foo',
  668. 'abc',
  669. null,
  670. baseVersion
  671. );
  672. const transOp = transform( op, transformBy );
  673. expected.range.end.path = [ 1, 4, 2 ];
  674. expect( transOp.length ).to.equal( 1 );
  675. expectOperation( transOp[ 0 ], expected );
  676. } );
  677. // [ range transformed by < ] original range >
  678. it( 'range intersects on right: shrink original range', () => {
  679. const transformBy = new AttributeOperation(
  680. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2, 1 ] ) ),
  681. 'foo',
  682. 'abc',
  683. null,
  684. baseVersion
  685. );
  686. const transOp = transform( op, transformBy );
  687. expected.range.start.path = [ 2, 1 ];
  688. expect( transOp.length ).to.equal( 1 );
  689. expectOperation( transOp[ 0 ], expected );
  690. } );
  691. it( 'range is inside original range: split into two operations', () => {
  692. const transformBy = new AttributeOperation(
  693. new Range( new Position( root, [ 1, 4, 1 ] ), new Position( root, [ 2, 1 ] ) ),
  694. 'foo',
  695. 'abc',
  696. null,
  697. baseVersion
  698. );
  699. const transOp = transform( op, transformBy );
  700. expect( transOp.length ).to.equal( 2 );
  701. expected.range.end.path = [ 1, 4, 1 ];
  702. expectOperation( transOp[ 0 ], expected );
  703. expected.range.start.path = [ 2, 1 ];
  704. expected.range.end = op.range.end;
  705. expected.baseVersion++;
  706. expectOperation( transOp[ 1 ], expected );
  707. } );
  708. } );
  709. } );
  710. describe( 'by RootAttributeOperation', () => {
  711. it( 'no operation update', () => {
  712. const transformBy = new RootAttributeOperation(
  713. root,
  714. 'foo',
  715. null,
  716. 'bar',
  717. baseVersion
  718. );
  719. const transOp = transform( op, transformBy );
  720. expect( transOp.length ).to.equal( 1 );
  721. expectOperation( transOp[ 0 ], expected );
  722. } );
  723. } );
  724. describe( 'by MoveOperation', () => {
  725. it( 'range and target are different than change range: no operation update', () => {
  726. const transformBy = new MoveOperation(
  727. new Position( root, [ 1, 1, 2 ] ),
  728. 2,
  729. new Position( root, [ 3, 4 ] ),
  730. baseVersion
  731. );
  732. const transOp = transform( op, transformBy );
  733. expect( transOp.length ).to.equal( 1 );
  734. expectOperation( transOp[ 0 ], expected );
  735. } );
  736. it( 'range offset is before change range start offset: decrement offset', () => {
  737. const transformBy = new MoveOperation(
  738. new Position( root, [ 1, 0 ] ),
  739. 2,
  740. new Position( root, [ 3, 4, 1 ] ),
  741. baseVersion
  742. );
  743. const transOp = transform( op, transformBy );
  744. expected.range.start.offset -= 2;
  745. expect( transOp.length ).to.equal( 1 );
  746. expectOperation( transOp[ 0 ], expected );
  747. } );
  748. it( 'target offset is before change range start offset: increment offset', () => {
  749. const transformBy = new MoveOperation(
  750. new Position( root, [ 3, 4, 1 ] ),
  751. 2,
  752. new Position( root, [ 1, 0 ] ),
  753. baseVersion
  754. );
  755. const transOp = transform( op, transformBy );
  756. expected.range.start.offset += 2;
  757. expect( transOp.length ).to.equal( 1 );
  758. expectOperation( transOp[ 0 ], expected );
  759. } );
  760. it( 'range is before node from path to change range: decrement index on path', () => {
  761. const transformBy = new MoveOperation(
  762. new Position( root, [ 0 ] ),
  763. 1,
  764. new Position( root, [ 2, 4, 1 ] ),
  765. baseVersion
  766. );
  767. const transOp = transform( op, transformBy );
  768. expected.range.start.path[ 0 ]--;
  769. expected.range.end.path[ 0 ]--;
  770. expect( transOp.length ).to.equal( 1 );
  771. expectOperation( transOp[ 0 ], expected );
  772. } );
  773. it( 'range is after node from path to change range: no position change', () => {
  774. const transformBy = new MoveOperation(
  775. new Position( root, [ 3 ] ),
  776. 2,
  777. new Position( root, [ 0, 1 ] ),
  778. baseVersion
  779. );
  780. const transOp = transform( op, transformBy );
  781. expect( transOp.length ).to.equal( 1 );
  782. expectOperation( transOp[ 0 ], expected );
  783. } );
  784. it( 'target before node from path to change range: increment index on path', () => {
  785. const transformBy = new MoveOperation(
  786. new Position( root, [ 3, 4, 1 ] ),
  787. 2,
  788. new Position( root, [ 1, 0 ] ),
  789. baseVersion
  790. );
  791. const transOp = transform( op, transformBy );
  792. expected.range.start.path[ 1 ] += 2;
  793. expect( transOp.length ).to.equal( 1 );
  794. expectOperation( transOp[ 0 ], expected );
  795. } );
  796. it( 'target after node from path to change range: no position change', () => {
  797. const transformBy = new MoveOperation(
  798. new Position( root, [ 3, 4, 1 ] ),
  799. 2,
  800. new Position( root, [ 3 ] ),
  801. baseVersion
  802. );
  803. const transOp = transform( op, transformBy );
  804. expect( transOp.length ).to.equal( 1 );
  805. expectOperation( transOp[ 0 ], expected );
  806. } );
  807. it( 'range intersects on left with change range: split into two operations', () => {
  808. const transformBy = new MoveOperation(
  809. new Position( root, [ 2, 1 ] ),
  810. 3,
  811. new Position( root, [ 4 ] ),
  812. baseVersion
  813. );
  814. const transOp = transform( op, transformBy );
  815. expect( transOp.length ).to.equal( 2 );
  816. expected.range.end.path = [ 2, 1 ];
  817. expectOperation( transOp[ 0 ], expected );
  818. expected.range.start.path = [ 4 ];
  819. expected.range.end.path = [ 5, 4 ];
  820. expected.baseVersion++;
  821. expectOperation( transOp[ 1 ], expected );
  822. } );
  823. it( 'range intersects on right with change range: split into two operation', () => {
  824. const transformBy = new MoveOperation(
  825. new Position( root, [ 1, 1 ] ),
  826. 3,
  827. new Position( root, [ 0, 0 ] ),
  828. baseVersion
  829. );
  830. const transOp = transform( op, transformBy );
  831. expect( transOp.length ).to.equal( 2 );
  832. expected.range.start.path = [ 1, 1 ];
  833. expectOperation( transOp[ 0 ], expected );
  834. expected.range.start.path = [ 0, 1 ];
  835. expected.range.end.path = [ 0, 3 ];
  836. expected.baseVersion++;
  837. expectOperation( transOp[ 1 ], expected );
  838. } );
  839. it( 'range contains change range: update change range', () => {
  840. const transformBy = new MoveOperation(
  841. new Position( root, [ 1 ] ),
  842. 2,
  843. new Position( root, [ 3, 4, 1 ] ),
  844. baseVersion
  845. );
  846. const transOp = transform( op, transformBy );
  847. expected.range.start.path = [ 1, 4, 1, 2 ];
  848. expected.range.end.path = [ 1, 4, 2, 2, 4 ];
  849. expect( transOp.length ).to.equal( 1 );
  850. expectOperation( transOp[ 0 ], expected );
  851. } );
  852. it( 'range is inside change range: split into two operations', () => {
  853. const transformBy = new MoveOperation(
  854. new Position( root, [ 1, 4 ] ),
  855. 2,
  856. new Position( root, [ 3, 2 ] ),
  857. baseVersion
  858. );
  859. const transOp = transform( op, transformBy );
  860. expect( transOp.length ).to.equal( 2 );
  861. expectOperation( transOp[ 0 ], expected );
  862. expected.range.start.path = [ 3, 2 ];
  863. expected.range.end.path = [ 3, 4 ];
  864. expected.baseVersion++;
  865. expectOperation( transOp[ 1 ], expected );
  866. } );
  867. it( 'target inside change range: split into two operations', () => {
  868. const transformBy = new MoveOperation(
  869. new Position( root, [ 3, 4, 1 ] ),
  870. 2,
  871. new Position( root, [ 1, 4 ] ),
  872. baseVersion
  873. );
  874. const transOp = transform( op, transformBy );
  875. expect( transOp.length ).to.equal( 2 );
  876. expected.range.start.path = [ 1, 6 ];
  877. expectOperation( transOp[ 0 ], expected );
  878. expected.range.start = op.range.start;
  879. expected.range.end.path = [ 1, 4 ];
  880. expected.baseVersion++;
  881. expectOperation( transOp[ 1 ], expected );
  882. } );
  883. it( 'range intersects change range and target inside change range: split into three operations', () => {
  884. const transformBy = new MoveOperation(
  885. new Position( root, [ 1, 1 ] ),
  886. 3,
  887. new Position( root, [ 2 ] ),
  888. baseVersion
  889. );
  890. const transOp = transform( op, transformBy );
  891. expect( transOp.length ).to.equal( 3 );
  892. expected.range.start.path = [ 5 ];
  893. expected.range.end.path = [ 5, 2, 4 ];
  894. expectOperation( transOp[ 0 ], expected );
  895. expected.range.start.path = [ 1, 1 ];
  896. expected.range.end.path = [ 2 ];
  897. expected.baseVersion++;
  898. expectOperation( transOp[ 1 ], expected );
  899. expected.range.start.path = [ 3 ];
  900. expected.range.end.path = [ 5 ];
  901. expected.baseVersion++;
  902. expectOperation( transOp[ 2 ], expected );
  903. } );
  904. } );
  905. describe( 'by NoOperation', () => {
  906. it( 'no operation update', () => {
  907. const transformBy = new NoOperation( baseVersion );
  908. const transOp = transform( op, transformBy );
  909. expect( transOp.length ).to.equal( 1 );
  910. expectOperation( transOp[ 0 ], expected );
  911. } );
  912. } );
  913. describe( 'by RenameOperation', () => {
  914. it( 'no position update', () => {
  915. const transformBy = new RenameOperation( new Position( root, [ 1, 0 ] ), 'oldName', 'newName', baseVersion );
  916. const transOp = transform( op, transformBy );
  917. expect( transOp.length ).to.equal( 1 );
  918. expectOperation( transOp[ 0 ], expected );
  919. } );
  920. } );
  921. describe( 'by MarkerOperation', () => {
  922. it( 'no operation update', () => {
  923. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  924. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  925. const transOp = transform( op, transformBy );
  926. expect( transOp.length ).to.equal( 1 );
  927. expectOperation( transOp[ 0 ], expected );
  928. } );
  929. } );
  930. } );
  931. // Some extra cases for a AttributeOperation that operates on single tree level range.
  932. // This means that the change range start and end differs only on offset value.
  933. // This test suite also have some modifications to the original operation
  934. // to get more test cases covered and better code coverage.
  935. describe( 'with single-level range', () => {
  936. beforeEach( () => {
  937. start = new Position( root, [ 0, 2, 1 ] );
  938. end = new Position( root, [ 0, 2, 4 ] );
  939. range = new Range( start, end );
  940. op = new AttributeOperation( range, 'foo', 'abc', 'bar', baseVersion );
  941. expected.range = new Range( start, end );
  942. } );
  943. describe( 'by InsertOperation', () => {
  944. it( 'target at offset before: increment offset', () => {
  945. const transformBy = new InsertOperation(
  946. new Position( root, [ 0, 2, 0 ] ),
  947. [ nodeA, nodeB ],
  948. baseVersion
  949. );
  950. const transOp = transform( op, transformBy );
  951. expected.range.start.offset += 2;
  952. expected.range.end.offset += 2;
  953. expect( transOp.length ).to.equal( 1 );
  954. expectOperation( transOp[ 0 ], expected );
  955. } );
  956. it( 'target at same offset: increment offset', () => {
  957. const transformBy = new InsertOperation(
  958. new Position( root, [ 0, 2, 1 ] ),
  959. [ nodeA, nodeB ],
  960. baseVersion
  961. );
  962. const transOp = transform( op, transformBy );
  963. expected.range.start.offset += 2;
  964. expected.range.end.offset += 2;
  965. expect( transOp.length ).to.equal( 1 );
  966. expectOperation( transOp[ 0 ], expected );
  967. } );
  968. } );
  969. describe( 'by MoveOperation', () => {
  970. it( 'range offset is before change range start offset: decrement offset', () => {
  971. const transformBy = new MoveOperation(
  972. new Position( root, [ 0, 2, 0 ] ),
  973. 1,
  974. new Position( root, [ 2, 4, 1 ] ),
  975. baseVersion
  976. );
  977. const transOp = transform( op, transformBy );
  978. expected.range.start.offset--;
  979. expected.range.end.offset--;
  980. expect( transOp.length ).to.equal( 1 );
  981. expectOperation( transOp[ 0 ], expected );
  982. } );
  983. it( 'target offset is before change range start offset: increment offset', () => {
  984. const transformBy = new MoveOperation(
  985. new Position( root, [ 2, 4, 1 ] ),
  986. 2,
  987. new Position( root, [ 0, 2, 0 ] ),
  988. baseVersion
  989. );
  990. const transOp = transform( op, transformBy );
  991. expected.range.start.offset += 2;
  992. expected.range.end.offset += 2;
  993. expect( transOp.length ).to.equal( 1 );
  994. expectOperation( transOp[ 0 ], expected );
  995. } );
  996. it( 'range intersects on left with change range: split into two operations', () => {
  997. const transformBy = new MoveOperation(
  998. new Position( root, [ 0, 2, 2 ] ),
  999. 4,
  1000. new Position( root, [ 2, 4, 1 ] ),
  1001. baseVersion
  1002. );
  1003. const transOp = transform( op, transformBy );
  1004. expect( transOp.length ).to.equal( 2 );
  1005. expected.range.end.offset -= 2;
  1006. expectOperation( transOp[ 0 ], expected );
  1007. expected.range.start.path = [ 2, 4, 1 ];
  1008. expected.range.end.path = [ 2, 4, 3 ];
  1009. expected.baseVersion++;
  1010. expectOperation( transOp[ 1 ], expected );
  1011. } );
  1012. it( 'range intersects on right with change range: split into two operation', () => {
  1013. const transformBy = new MoveOperation(
  1014. new Position( root, [ 0, 2, 0 ] ),
  1015. 2,
  1016. new Position( root, [ 2, 4, 1 ] ),
  1017. baseVersion
  1018. );
  1019. const transOp = transform( op, transformBy );
  1020. expect( transOp.length ).to.equal( 2 );
  1021. expected.range.start.offset -= 1;
  1022. expected.range.end.offset -= 2;
  1023. expectOperation( transOp[ 0 ], expected );
  1024. expected.range.start.path = [ 2, 4, 2 ];
  1025. expected.range.end.path = [ 2, 4, 3 ];
  1026. expected.baseVersion++;
  1027. expectOperation( transOp[ 1 ], expected );
  1028. } );
  1029. it( 'range contains change range: update change range', () => {
  1030. const transformBy = new MoveOperation(
  1031. new Position( root, [ 0, 1 ] ),
  1032. 3,
  1033. new Position( root, [ 2, 4, 1 ] ),
  1034. baseVersion
  1035. );
  1036. const transOp = transform( op, transformBy );
  1037. expected.range.start.path = [ 2, 4, 2, 1 ];
  1038. expected.range.end.path = [ 2, 4, 2, 4 ];
  1039. expect( transOp.length ).to.equal( 1 );
  1040. expectOperation( transOp[ 0 ], expected );
  1041. } );
  1042. it( 'range is inside change range: split into two operations', () => {
  1043. const transformBy = new MoveOperation(
  1044. new Position( root, [ 0, 2, 2 ] ),
  1045. 1,
  1046. new Position( root, [ 2, 4, 1 ] ),
  1047. baseVersion
  1048. );
  1049. const transOp = transform( op, transformBy );
  1050. expect( transOp.length ).to.equal( 2 );
  1051. expected.range.end.offset--;
  1052. expectOperation( transOp[ 0 ], expected );
  1053. expected.range.start.path = [ 2, 4, 1 ];
  1054. expected.range.end.path = [ 2, 4, 2 ];
  1055. expected.baseVersion++;
  1056. expectOperation( transOp[ 1 ], expected );
  1057. } );
  1058. it( 'range is same as change range: update change range', () => {
  1059. const transformBy = new MoveOperation(
  1060. new Position( root, [ 0, 2, 1 ] ),
  1061. 3,
  1062. new Position( root, [ 2, 4, 1 ] ),
  1063. baseVersion
  1064. );
  1065. const transOp = transform( op, transformBy );
  1066. expected.range.start.path = [ 2, 4, 1 ];
  1067. expected.range.end.path = [ 2, 4, 4 ];
  1068. expect( transOp.length ).to.equal( 1 );
  1069. expectOperation( transOp[ 0 ], expected );
  1070. } );
  1071. it( 'target inside change range: split into two operations', () => {
  1072. const transformBy = new MoveOperation(
  1073. new Position( root, [ 2, 4, 1 ] ),
  1074. 2,
  1075. new Position( root, [ 0, 2, 2 ] ),
  1076. baseVersion
  1077. );
  1078. const transOp = transform( op, transformBy );
  1079. expect( transOp.length ).to.equal( 2 );
  1080. expected.range.start.offset = 4;
  1081. expected.range.end.offset = 6;
  1082. expectOperation( transOp[ 0 ], expected );
  1083. expected.range.start.offset = op.range.start.offset;
  1084. expected.range.end.offset = 2;
  1085. expected.baseVersion++;
  1086. expectOperation( transOp[ 1 ], expected );
  1087. } );
  1088. it( 'range intersects change range and target inside change range: split into three operations', () => {
  1089. const transformBy = new MoveOperation(
  1090. new Position( root, [ 0, 2, 0 ] ),
  1091. 2,
  1092. new Position( root, [ 0, 2, 3 ] ),
  1093. baseVersion
  1094. );
  1095. const transOp = transform( op, transformBy );
  1096. expect( transOp.length ).to.equal( 3 );
  1097. expected.range.start.offset = 3;
  1098. expected.range.end.offset = 4;
  1099. expectOperation( transOp[ 0 ], expected );
  1100. expected.range.start.offset = 0;
  1101. expected.range.end.offset = 1;
  1102. expected.baseVersion++;
  1103. expectOperation( transOp[ 1 ], expected );
  1104. expected.range.start.offset = 2;
  1105. expected.range.end.offset = 3;
  1106. expected.baseVersion++;
  1107. expectOperation( transOp[ 2 ], expected );
  1108. } );
  1109. } );
  1110. } );
  1111. describe( 'by RemoveOperation', () => {
  1112. beforeEach( () => {
  1113. start = new Position( doc.graveyard, [ 2, 0 ] );
  1114. end = new Position( doc.graveyard, [ 2, 4 ] );
  1115. range = new Range( start, end );
  1116. op = new AttributeOperation( range, 'foo', 'abc', 'bar', baseVersion );
  1117. expected.range = new Range( start, end );
  1118. } );
  1119. it( 'remove operation inserted elements before attribute operation range: increment path', () => {
  1120. const transformBy = new RemoveOperation(
  1121. new Position( root, [ 0 ] ),
  1122. 2,
  1123. new Position( doc.graveyard, [ 0 ] ),
  1124. baseVersion
  1125. );
  1126. transformBy.targetPosition.path = [ 0 ];
  1127. const transOp = transform( op, transformBy );
  1128. expect( transOp.length ).to.equal( 1 );
  1129. expected.range.start.path = [ 4, 0 ];
  1130. expected.range.end.path = [ 4, 4 ];
  1131. expectOperation( transOp[ 0 ], expected );
  1132. } );
  1133. it( 'remove operation inserted elements after attribute operation range: do nothing', () => {
  1134. const transformBy = new RemoveOperation(
  1135. new Position( root, [ 0 ] ),
  1136. 2,
  1137. new Position( doc.graveyard, [ 0 ] ),
  1138. baseVersion
  1139. );
  1140. transformBy.targetPosition.path = [ 4 ];
  1141. const transOp = transform( op, transformBy );
  1142. expect( transOp.length ).to.equal( 1 );
  1143. expectOperation( transOp[ 0 ], expected );
  1144. } );
  1145. } );
  1146. } );
  1147. describe( 'RootAttributeOperation', () => {
  1148. const diffRoot = new RootElement( null );
  1149. beforeEach( () => {
  1150. expected = {
  1151. type: RootAttributeOperation,
  1152. key: 'foo',
  1153. oldValue: 'abc',
  1154. newValue: 'bar',
  1155. baseVersion: baseVersion + 1
  1156. };
  1157. op = new RootAttributeOperation( root, 'foo', 'abc', 'bar', baseVersion );
  1158. } );
  1159. describe( 'by InsertOperation', () => {
  1160. it( 'no operation update', () => {
  1161. const transformBy = new InsertOperation(
  1162. new Position( root, [ 0 ] ),
  1163. 'a',
  1164. baseVersion
  1165. );
  1166. const transOp = transform( op, transformBy );
  1167. expect( transOp.length ).to.equal( 1 );
  1168. expectOperation( transOp[ 0 ], expected );
  1169. } );
  1170. } );
  1171. describe( 'by AttributeOperation', () => {
  1172. it( 'no operation update', () => {
  1173. const transformBy = new AttributeOperation(
  1174. new Range(
  1175. new Position( root, [ 0 ] ),
  1176. new Position( root, [ 1 ] )
  1177. ),
  1178. 'foo',
  1179. 'bar',
  1180. 'xyz',
  1181. baseVersion
  1182. );
  1183. const transOp = transform( op, transformBy );
  1184. expect( transOp.length ).to.equal( 1 );
  1185. expectOperation( transOp[ 0 ], expected );
  1186. } );
  1187. } );
  1188. describe( 'by RootAttributeOperation', () => {
  1189. it( 'changes different root: no operation update', () => {
  1190. const transformBy = new RootAttributeOperation(
  1191. diffRoot,
  1192. 'foo',
  1193. 'abc',
  1194. 'xyz',
  1195. baseVersion
  1196. );
  1197. const transOp = transform( op, transformBy );
  1198. expect( transOp.length ).to.equal( 1 );
  1199. expectOperation( transOp[ 0 ], expected );
  1200. } );
  1201. it( 'changes different key: no operation update', () => {
  1202. const transformBy = new RootAttributeOperation(
  1203. root,
  1204. 'abc',
  1205. 'abc',
  1206. 'xyz',
  1207. baseVersion
  1208. );
  1209. const transOp = transform( op, transformBy );
  1210. expect( transOp.length ).to.equal( 1 );
  1211. expectOperation( transOp[ 0 ], expected );
  1212. } );
  1213. it( 'sets same value for same key: convert to NoOperation', () => {
  1214. const transformBy = new RootAttributeOperation(
  1215. root,
  1216. 'foo',
  1217. 'abc',
  1218. 'bar',
  1219. baseVersion
  1220. );
  1221. const transOp = transform( op, transformBy );
  1222. expect( transOp.length ).to.equal( 1 );
  1223. expectOperation( transOp[ 0 ], {
  1224. type: NoOperation,
  1225. baseVersion: baseVersion + 1
  1226. } );
  1227. } );
  1228. it( 'sets different value for same key on same root and is important: no operation update', () => {
  1229. const transformBy = new RootAttributeOperation(
  1230. root,
  1231. 'foo',
  1232. 'abc',
  1233. 'xyz',
  1234. baseVersion
  1235. );
  1236. const transOp = transform( op, transformBy );
  1237. expect( transOp.length ).to.equal( 1 );
  1238. expectOperation( transOp[ 0 ], {
  1239. type: NoOperation,
  1240. baseVersion: baseVersion + 1
  1241. } );
  1242. } );
  1243. it( 'sets different value for same key on same root and is less important: convert to NoOperation', () => {
  1244. const transformBy = new RootAttributeOperation(
  1245. root,
  1246. 'foo',
  1247. 'abc',
  1248. 'xyz',
  1249. baseVersion
  1250. );
  1251. const transOp = transform( op, transformBy, { isStrong: true } );
  1252. expect( transOp.length ).to.equal( 1 );
  1253. expectOperation( transOp[ 0 ], expected );
  1254. } );
  1255. } );
  1256. describe( 'by MoveOperation', () => {
  1257. it( 'no operation update', () => {
  1258. const transformBy = new MoveOperation(
  1259. new Position( root, [ 0 ] ),
  1260. 2,
  1261. new Position( root, [ 1 ] ),
  1262. baseVersion
  1263. );
  1264. const transOp = transform( op, transformBy );
  1265. expect( transOp.length ).to.equal( 1 );
  1266. expectOperation( transOp[ 0 ], expected );
  1267. } );
  1268. } );
  1269. describe( 'by NoOperation', () => {
  1270. it( 'no operation update', () => {
  1271. const transformBy = new NoOperation( baseVersion );
  1272. const transOp = transform( op, transformBy );
  1273. expect( transOp.length ).to.equal( 1 );
  1274. expectOperation( transOp[ 0 ], expected );
  1275. } );
  1276. } );
  1277. describe( 'by RenameOperation', () => {
  1278. it( 'no position update', () => {
  1279. const transformBy = new RenameOperation( new Position( root, [ 0 ] ), 'oldName', 'newName', baseVersion );
  1280. const transOp = transform( op, transformBy );
  1281. expect( transOp.length ).to.equal( 1 );
  1282. expectOperation( transOp[ 0 ], expected );
  1283. } );
  1284. } );
  1285. describe( 'by MarkerOperation', () => {
  1286. it( 'no position update', () => {
  1287. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1288. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  1289. const transOp = transform( op, transformBy );
  1290. expect( transOp.length ).to.equal( 1 );
  1291. expectOperation( transOp[ 0 ], expected );
  1292. } );
  1293. } );
  1294. } );
  1295. describe( 'MoveOperation', () => {
  1296. let sourcePosition, targetPosition, rangeEnd, howMany;
  1297. beforeEach( () => {
  1298. sourcePosition = new Position( root, [ 2, 2, 4 ] );
  1299. targetPosition = new Position( root, [ 3, 3, 3 ] );
  1300. howMany = 2;
  1301. rangeEnd = Position.createFromPosition( sourcePosition );
  1302. rangeEnd.offset += howMany;
  1303. op = new MoveOperation( sourcePosition, howMany, targetPosition, baseVersion );
  1304. expected = {
  1305. type: MoveOperation,
  1306. sourcePosition: Position.createFromPosition( sourcePosition ),
  1307. targetPosition: Position.createFromPosition( targetPosition ),
  1308. howMany,
  1309. baseVersion: baseVersion + 1
  1310. };
  1311. } );
  1312. describe( 'by InsertOperation', () => {
  1313. it( 'target at different position than move range and target: no operation update', () => {
  1314. const transformBy = new InsertOperation(
  1315. new Position( root, [ 1, 3, 2 ] ),
  1316. [ nodeA, nodeB ],
  1317. baseVersion
  1318. );
  1319. const transOp = transform( op, transformBy );
  1320. expect( transOp.length ).to.equal( 1 );
  1321. expectOperation( transOp[ 0 ], expected );
  1322. } );
  1323. it( 'target inside node from move range: no operation update', () => {
  1324. const transformBy = new InsertOperation(
  1325. new Position( root, [ 2, 2, 4, 1 ] ),
  1326. [ nodeA, nodeB ],
  1327. baseVersion
  1328. );
  1329. const transOp = transform( op, transformBy );
  1330. expect( transOp.length ).to.equal( 1 );
  1331. expectOperation( transOp[ 0 ], expected );
  1332. } );
  1333. it( 'target at offset before range offset: increment range offset', () => {
  1334. const transformBy = new InsertOperation(
  1335. new Position( root, [ 2, 2, 0 ] ),
  1336. [ nodeA, nodeB ],
  1337. baseVersion
  1338. );
  1339. const transOp = transform( op, transformBy );
  1340. expected.sourcePosition.offset += 2;
  1341. expect( transOp.length ).to.equal( 1 );
  1342. expectOperation( transOp[ 0 ], expected );
  1343. } );
  1344. it( 'target at offset after range offset: no operation update', () => {
  1345. const transformBy = new InsertOperation(
  1346. new Position( root, [ 2, 2, 7 ] ),
  1347. [ nodeA, nodeB ],
  1348. baseVersion
  1349. );
  1350. const transOp = transform( op, transformBy );
  1351. expect( transOp.length ).to.equal( 1 );
  1352. expectOperation( transOp[ 0 ], expected );
  1353. } );
  1354. it( 'target before node from range start path: increment index on range start path', () => {
  1355. const transformBy = new InsertOperation(
  1356. new Position( root, [ 2, 0 ] ),
  1357. [ nodeA, nodeB ],
  1358. baseVersion
  1359. );
  1360. const transOp = transform( op, transformBy );
  1361. expected.sourcePosition.path[ 1 ] += 2;
  1362. expect( transOp.length ).to.equal( 1 );
  1363. expectOperation( transOp[ 0 ], expected );
  1364. } );
  1365. it( 'target after node from range start path: no operation update', () => {
  1366. const transformBy = new InsertOperation(
  1367. new Position( root, [ 2, 3 ] ),
  1368. [ nodeA, nodeB ],
  1369. baseVersion
  1370. );
  1371. const transOp = transform( op, transformBy );
  1372. expect( transOp.length ).to.equal( 1 );
  1373. expectOperation( transOp[ 0 ], expected );
  1374. } );
  1375. it( 'target offset before move target offset: increment target offset', () => {
  1376. const transformBy = new InsertOperation(
  1377. new Position( root, [ 3, 3, 2 ] ),
  1378. [ nodeA, nodeB ],
  1379. baseVersion
  1380. );
  1381. const transOp = transform( op, transformBy );
  1382. expected.targetPosition.offset += 2;
  1383. expect( transOp.length ).to.equal( 1 );
  1384. expectOperation( transOp[ 0 ], expected );
  1385. } );
  1386. it( 'target offset after move target offset: no operation update', () => {
  1387. const transformBy = new InsertOperation(
  1388. new Position( root, [ 3, 3, 4 ] ),
  1389. [ nodeA, nodeB ],
  1390. baseVersion
  1391. );
  1392. const transOp = transform( op, transformBy );
  1393. expect( transOp.length ).to.equal( 1 );
  1394. expectOperation( transOp[ 0 ], expected );
  1395. } );
  1396. it( 'target before node from move target position path: increment index on move target position path', () => {
  1397. const transformBy = new InsertOperation(
  1398. new Position( root, [ 3, 3 ] ),
  1399. [ nodeA, nodeB ],
  1400. baseVersion
  1401. );
  1402. const transOp = transform( op, transformBy );
  1403. expected.targetPosition.path[ 1 ] += 2;
  1404. expect( transOp.length ).to.equal( 1 );
  1405. expectOperation( transOp[ 0 ], expected );
  1406. } );
  1407. it( 'target after node from move target position path: no operation update', () => {
  1408. const transformBy = new InsertOperation(
  1409. new Position( root, [ 3, 6 ] ),
  1410. [ nodeA, nodeB ],
  1411. baseVersion
  1412. );
  1413. const transOp = transform( op, transformBy );
  1414. expect( transOp.length ).to.equal( 1 );
  1415. expectOperation( transOp[ 0 ], expected );
  1416. } );
  1417. it( 'target offset same as move target offset and is important: increment target offset', () => {
  1418. const transformBy = new InsertOperation(
  1419. new Position( root, [ 3, 3, 3 ] ),
  1420. [ nodeA, nodeB ],
  1421. baseVersion
  1422. );
  1423. const transOp = transform( op, transformBy );
  1424. expected.targetPosition.offset += 2;
  1425. expect( transOp.length ).to.equal( 1 );
  1426. expectOperation( transOp[ 0 ], expected );
  1427. } );
  1428. it( 'target offset same as move target offset and is less important: no operation update', () => {
  1429. const transformBy = new InsertOperation(
  1430. new Position( root, [ 3, 3, 3 ] ),
  1431. [ nodeA, nodeB ],
  1432. baseVersion
  1433. );
  1434. const transOp = transform( op, transformBy, { isStrong: true } );
  1435. expect( transOp.length ).to.equal( 1 );
  1436. expectOperation( transOp[ 0 ], expected );
  1437. } );
  1438. it( 'target offset same as move target offset and context.insertBefore = true: increment target offset', () => {
  1439. const transformBy = new InsertOperation(
  1440. new Position( root, [ 3, 3, 3 ] ),
  1441. [ nodeA, nodeB ],
  1442. baseVersion
  1443. );
  1444. const transOp = transform( op, transformBy, { isStrong: true, insertBefore: true } );
  1445. expected.targetPosition.offset += 2;
  1446. expect( transOp.length ).to.equal( 1 );
  1447. expectOperation( transOp[ 0 ], expected );
  1448. } );
  1449. it( 'target offset same as move target offset and context.insertBefore = false: no operation update', () => {
  1450. const transformBy = new InsertOperation(
  1451. new Position( root, [ 3, 3, 3 ] ),
  1452. [ nodeA, nodeB ],
  1453. baseVersion
  1454. );
  1455. const transOp = transform( op, transformBy, { isStrong: false, insertBefore: false } );
  1456. expect( transOp.length ).to.equal( 1 );
  1457. expectOperation( transOp[ 0 ], expected );
  1458. } );
  1459. it( 'target inside move range: expand range', () => {
  1460. const transformBy = new InsertOperation(
  1461. new Position( root, [ 2, 2, 5 ] ),
  1462. [ nodeA, nodeB ],
  1463. baseVersion
  1464. );
  1465. const transOp = transform( op, transformBy );
  1466. expect( transOp.length ).to.equal( 1 );
  1467. expected.howMany = 4;
  1468. expectOperation( transOp[ 0 ], expected );
  1469. } );
  1470. it( 'target at offset same as range end boundary: no operation update', () => {
  1471. const transformBy = new InsertOperation(
  1472. new Position( root, [ 2, 2, 6 ] ),
  1473. [ nodeA, nodeB ],
  1474. baseVersion
  1475. );
  1476. const transOp = transform( op, transformBy );
  1477. expect( transOp.length ).to.equal( 1 );
  1478. expectOperation( transOp[ 0 ], expected );
  1479. } );
  1480. it( 'target at offset same as range end boundary (sticky move): expand range', () => {
  1481. const transformBy = new InsertOperation(
  1482. new Position( root, [ 2, 2, 6 ] ),
  1483. [ nodeA, nodeB ],
  1484. baseVersion
  1485. );
  1486. op.isSticky = true;
  1487. const transOp = transform( op, transformBy );
  1488. expected.howMany = 4;
  1489. expect( transOp.length ).to.equal( 1 );
  1490. expectOperation( transOp[ 0 ], expected );
  1491. } );
  1492. } );
  1493. describe( 'by AttributeOperation', () => {
  1494. it( 'no operation update', () => {
  1495. const transformBy = new AttributeOperation(
  1496. new Range( sourcePosition, rangeEnd ),
  1497. 'abc',
  1498. true,
  1499. false,
  1500. baseVersion
  1501. );
  1502. const transOp = transform( op, transformBy );
  1503. expect( transOp.length ).to.equal( 1 );
  1504. expectOperation( transOp[ 0 ], expected );
  1505. } );
  1506. } );
  1507. describe( 'by RootAttributeOperation', () => {
  1508. it( 'no operation update', () => {
  1509. const transformBy = new RootAttributeOperation(
  1510. root,
  1511. 'foo',
  1512. null,
  1513. 'bar',
  1514. baseVersion
  1515. );
  1516. const transOp = transform( op, transformBy );
  1517. expect( transOp.length ).to.equal( 1 );
  1518. expectOperation( transOp[ 0 ], expected );
  1519. } );
  1520. } );
  1521. describe( 'by MoveOperation', () => {
  1522. it( 'range and target different than transforming range and target: no operation update', () => {
  1523. const transformBy = new MoveOperation(
  1524. new Position( root, [ 1, 2 ] ),
  1525. 3,
  1526. new Position( root, [ 4, 1, 0 ] ),
  1527. baseVersion
  1528. );
  1529. const transOp = transform( op, transformBy );
  1530. expect( transOp.length ).to.equal( 1 );
  1531. expectOperation( transOp[ 0 ], expected );
  1532. } );
  1533. it( 'target offset before transforming range start offset: increment range offset', () => {
  1534. const transformBy = new MoveOperation(
  1535. new Position( root, [ 4, 1, 0 ] ),
  1536. 2,
  1537. new Position( root, [ 2, 2, 0 ] ),
  1538. baseVersion
  1539. );
  1540. const transOp = transform( op, transformBy );
  1541. expected.sourcePosition.offset += 2;
  1542. expect( transOp.length ).to.equal( 1 );
  1543. expectOperation( transOp[ 0 ], expected );
  1544. } );
  1545. it( 'target offset after transforming range start offset: no operation update', () => {
  1546. const transformBy = new MoveOperation(
  1547. new Position( root, [ 4, 1, 0 ] ),
  1548. 2,
  1549. new Position( root, [ 2, 2, 7 ] ),
  1550. baseVersion
  1551. );
  1552. const transOp = transform( op, transformBy );
  1553. expect( transOp.length ).to.equal( 1 );
  1554. expectOperation( transOp[ 0 ], expected );
  1555. } );
  1556. it( 'range start offset before transforming range start offset: decrement range offset', () => {
  1557. const transformBy = new MoveOperation(
  1558. new Position( root, [ 2, 2, 0 ] ),
  1559. 2,
  1560. new Position( root, [ 4, 1, 0 ] ),
  1561. baseVersion
  1562. );
  1563. const transOp = transform( op, transformBy );
  1564. expected.sourcePosition.offset -= 2;
  1565. expect( transOp.length ).to.equal( 1 );
  1566. expectOperation( transOp[ 0 ], expected );
  1567. } );
  1568. it( 'range start offset after transforming range start offset: no operation update', () => {
  1569. const transformBy = new MoveOperation(
  1570. new Position( root, [ 2, 2, 9 ] ),
  1571. 2,
  1572. new Position( root, [ 4, 1, 0 ] ),
  1573. baseVersion
  1574. );
  1575. const transOp = transform( op, transformBy, { isStrong: true } );
  1576. expect( transOp.length ).to.equal( 1 );
  1577. expectOperation( transOp[ 0 ], expected );
  1578. } );
  1579. it( 'target before node from transforming range start path: increment index on range start path', () => {
  1580. const transformBy = new MoveOperation(
  1581. new Position( root, [ 4, 1, 0 ] ),
  1582. 2,
  1583. new Position( root, [ 2, 1 ] ),
  1584. baseVersion
  1585. );
  1586. const transOp = transform( op, transformBy );
  1587. expected.sourcePosition.path[ 1 ] += 2;
  1588. expect( transOp.length ).to.equal( 1 );
  1589. expectOperation( transOp[ 0 ], expected );
  1590. } );
  1591. it( 'target after node from transforming range start path: no operation update', () => {
  1592. const transformBy = new MoveOperation(
  1593. new Position( root, [ 4, 1, 0 ] ),
  1594. 2,
  1595. new Position( root, [ 2, 5 ] ),
  1596. baseVersion
  1597. );
  1598. const transOp = transform( op, transformBy );
  1599. expect( transOp.length ).to.equal( 1 );
  1600. expectOperation( transOp[ 0 ], expected );
  1601. } );
  1602. it( 'range before node from transforming range start path: decrement index on range start path', () => {
  1603. const transformBy = new MoveOperation(
  1604. new Position( root, [ 2, 0 ] ),
  1605. 1,
  1606. new Position( root, [ 4, 1, 0 ] ),
  1607. baseVersion
  1608. );
  1609. const transOp = transform( op, transformBy );
  1610. expected.sourcePosition.path[ 1 ] -= 1;
  1611. expect( transOp.length ).to.equal( 1 );
  1612. expectOperation( transOp[ 0 ], expected );
  1613. } );
  1614. it( 'range after node from transforming range start path: no operation update', () => {
  1615. const transformBy = new MoveOperation(
  1616. new Position( root, [ 2, 3 ] ),
  1617. 2,
  1618. new Position( root, [ 4, 1, 0 ] ),
  1619. baseVersion
  1620. );
  1621. const transOp = transform( op, transformBy );
  1622. expect( transOp.length ).to.equal( 1 );
  1623. expectOperation( transOp[ 0 ], expected );
  1624. } );
  1625. it( 'target offset before transforming target offset: increment target offset', () => {
  1626. const transformBy = new MoveOperation(
  1627. new Position( root, [ 4, 1, 0 ] ),
  1628. 2,
  1629. new Position( root, [ 3, 3, 0 ] ),
  1630. baseVersion
  1631. );
  1632. const transOp = transform( op, transformBy );
  1633. expected.targetPosition.offset += 2;
  1634. expect( transOp.length ).to.equal( 1 );
  1635. expectOperation( transOp[ 0 ], expected );
  1636. } );
  1637. it( 'target offset after transforming target offset: no operation update', () => {
  1638. const transformBy = new MoveOperation(
  1639. new Position( root, [ 4, 1, 0 ] ),
  1640. 2,
  1641. new Position( root, [ 3, 3, 5 ] ),
  1642. baseVersion
  1643. );
  1644. const transOp = transform( op, transformBy );
  1645. expect( transOp.length ).to.equal( 1 );
  1646. expectOperation( transOp[ 0 ], expected );
  1647. } );
  1648. it( 'range offset before transforming target offset: decrement target offset', () => {
  1649. const transformBy = new MoveOperation(
  1650. new Position( root, [ 3, 3, 0 ] ),
  1651. 2,
  1652. new Position( root, [ 4, 1, 0 ] ),
  1653. baseVersion
  1654. );
  1655. const transOp = transform( op, transformBy );
  1656. expected.targetPosition.offset -= 2;
  1657. expect( transOp.length ).to.equal( 1 );
  1658. expectOperation( transOp[ 0 ], expected );
  1659. } );
  1660. it( 'range offset after transforming target offset: no operation update', () => {
  1661. const transformBy = new MoveOperation(
  1662. new Position( root, [ 3, 3, 5 ] ),
  1663. 2,
  1664. new Position( root, [ 4, 1, 0 ] ),
  1665. baseVersion
  1666. );
  1667. const transOp = transform( op, transformBy );
  1668. expect( transOp.length ).to.equal( 1 );
  1669. expectOperation( transOp[ 0 ], expected );
  1670. } );
  1671. it( 'target before node from transforming target path: increment index on target path', () => {
  1672. const transformBy = new MoveOperation(
  1673. new Position( root, [ 4, 1, 0 ] ),
  1674. 2,
  1675. new Position( root, [ 3, 1 ] ),
  1676. baseVersion
  1677. );
  1678. const transOp = transform( op, transformBy );
  1679. expected.targetPosition.path[ 1 ] += 2;
  1680. expect( transOp.length ).to.equal( 1 );
  1681. expectOperation( transOp[ 0 ], expected );
  1682. } );
  1683. it( 'target after node from transforming target path: no operation update', () => {
  1684. const transformBy = new MoveOperation(
  1685. new Position( root, [ 4, 1, 0 ] ),
  1686. 2,
  1687. new Position( root, [ 3, 5 ] ),
  1688. baseVersion
  1689. );
  1690. const transOp = transform( op, transformBy );
  1691. expect( transOp.length ).to.equal( 1 );
  1692. expectOperation( transOp[ 0 ], expected );
  1693. } );
  1694. it( 'range before node from transforming target path: decrement index on target path', () => {
  1695. const transformBy = new MoveOperation(
  1696. new Position( root, [ 3, 0 ] ),
  1697. 2,
  1698. new Position( root, [ 4, 1, 0 ] ),
  1699. baseVersion
  1700. );
  1701. const transOp = transform( op, transformBy );
  1702. expected.targetPosition.path[ 1 ] -= 2;
  1703. expect( transOp.length ).to.equal( 1 );
  1704. expectOperation( transOp[ 0 ], expected );
  1705. } );
  1706. it( 'range after node from transforming target path: no operation update', () => {
  1707. const transformBy = new MoveOperation(
  1708. new Position( root, [ 3, 5 ] ),
  1709. 2,
  1710. new Position( root, [ 4, 1, 0 ] ),
  1711. baseVersion
  1712. );
  1713. const transOp = transform( op, transformBy );
  1714. expect( transOp.length ).to.equal( 1 );
  1715. expectOperation( transOp[ 0 ], expected );
  1716. } );
  1717. it( 'target inside transforming move range: split into two operations', () => {
  1718. const transformBy = new MoveOperation(
  1719. new Position( root, [ 4, 1, 0 ] ),
  1720. 2,
  1721. new Position( root, [ 2, 2, 5 ] ),
  1722. baseVersion
  1723. );
  1724. const transOp = transform( op, transformBy );
  1725. expect( transOp.length ).to.equal( 2 );
  1726. expected.howMany = 1;
  1727. expectOperation( transOp[ 0 ], expected );
  1728. expected.sourcePosition.path = [ 2, 2, 6 ];
  1729. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1730. expected.baseVersion++;
  1731. expectOperation( transOp[ 1 ], expected );
  1732. } );
  1733. it( 'target at start boundary of transforming move range: increment source offset', () => {
  1734. const transformBy = new MoveOperation(
  1735. new Position( root, [ 4, 1, 0 ] ),
  1736. 2,
  1737. new Position( root, [ 2, 2, 4 ] ),
  1738. baseVersion
  1739. );
  1740. const transOp = transform( op, transformBy );
  1741. expect( transOp.length ).to.equal( 1 );
  1742. expected.sourcePosition.offset = 6;
  1743. expectOperation( transOp[ 0 ], expected );
  1744. } );
  1745. it( 'target at start boundary of transforming move range (sticky move): expand move range', () => {
  1746. const transformBy = new MoveOperation(
  1747. new Position( root, [ 4, 1, 0 ] ),
  1748. 2,
  1749. new Position( root, [ 2, 2, 4 ] ),
  1750. baseVersion
  1751. );
  1752. op.isSticky = true;
  1753. const transOp = transform( op, transformBy );
  1754. expect( transOp.length ).to.equal( 1 );
  1755. expected.howMany = 4;
  1756. expectOperation( transOp[ 0 ], expected );
  1757. } );
  1758. it( 'target at end boundary of transforming move range: no operation update', () => {
  1759. const transformBy = new MoveOperation(
  1760. new Position( root, [ 4, 1, 0 ] ),
  1761. 2,
  1762. new Position( root, [ 2, 2, 6 ] ),
  1763. baseVersion
  1764. );
  1765. const transOp = transform( op, transformBy );
  1766. expect( transOp.length ).to.equal( 1 );
  1767. expectOperation( transOp[ 0 ], expected );
  1768. } );
  1769. it( 'target at end boundary of transforming move range (sticky move): expand move range', () => {
  1770. const transformBy = new MoveOperation(
  1771. new Position( root, [ 4, 1, 0 ] ),
  1772. 2,
  1773. new Position( root, [ 2, 2, 6 ] ),
  1774. baseVersion
  1775. );
  1776. op.isSticky = true;
  1777. const transOp = transform( op, transformBy );
  1778. expect( transOp.length ).to.equal( 1 );
  1779. expected.howMany = 4;
  1780. expectOperation( transOp[ 0 ], expected );
  1781. } );
  1782. it( 'target inside a node from transforming range: no operation update', () => {
  1783. const transformBy = new MoveOperation(
  1784. new Position( root, [ 4, 1, 0 ] ),
  1785. 2,
  1786. new Position( root, [ 2, 2, 5, 1 ] ),
  1787. baseVersion
  1788. );
  1789. const transOp = transform( op, transformBy );
  1790. expect( transOp.length ).to.equal( 1 );
  1791. expectOperation( transOp[ 0 ], expected );
  1792. } );
  1793. it( 'range has node that contains transforming range: update range path', () => {
  1794. const transformBy = new MoveOperation(
  1795. new Position( root, [ 2, 1 ] ),
  1796. 3,
  1797. new Position( root, [ 4, 2 ] ),
  1798. baseVersion
  1799. );
  1800. const transOp = transform( op, transformBy );
  1801. expected.sourcePosition.path = [ 4, 3, 4 ];
  1802. expect( transOp.length ).to.equal( 1 );
  1803. expectOperation( transOp[ 0 ], expected );
  1804. } );
  1805. it( 'range has node that contains transforming target: update target path', () => {
  1806. const transformBy = new MoveOperation(
  1807. new Position( root, [ 3, 2 ] ),
  1808. 3,
  1809. new Position( root, [ 0, 1 ] ),
  1810. baseVersion
  1811. );
  1812. const transOp = transform( op, transformBy );
  1813. expected.targetPosition.path = [ 0, 2, 3 ];
  1814. expect( transOp.length ).to.equal( 1 );
  1815. expectOperation( transOp[ 0 ], expected );
  1816. } );
  1817. it( 'target inside a node from transforming range and vice versa: reverse transform-by operation', () => {
  1818. const transformBy = new MoveOperation(
  1819. new Position( root, [ 3, 2 ] ),
  1820. 3,
  1821. new Position( root, [ 2, 2, 5, 0 ] ),
  1822. baseVersion
  1823. );
  1824. const transOp = transform( op, transformBy );
  1825. const reversed = transformBy.getReversed();
  1826. expected.sourcePosition = reversed.sourcePosition;
  1827. expected.targetPosition = reversed.targetPosition;
  1828. expected.howMany = reversed.howMany;
  1829. expect( transOp.length ).to.equal( 1 );
  1830. expectOperation( transOp[ 0 ], expected );
  1831. } );
  1832. it( 'range is same as transforming range and is important: convert to NoOperation', () => {
  1833. const transformBy = new MoveOperation(
  1834. op.sourcePosition,
  1835. op.howMany,
  1836. new Position( root, [ 4, 1, 0 ] ),
  1837. baseVersion
  1838. );
  1839. const transOp = transform( op, transformBy );
  1840. expect( transOp.length ).to.equal( 1 );
  1841. expectOperation( transOp[ 0 ], {
  1842. type: NoOperation,
  1843. baseVersion: baseVersion + 1
  1844. } );
  1845. } );
  1846. it( 'range is same as transforming range and is less important: update range path', () => {
  1847. const transformBy = new MoveOperation(
  1848. op.sourcePosition,
  1849. op.howMany,
  1850. new Position( root, [ 4, 1, 0 ] ),
  1851. baseVersion
  1852. );
  1853. const transOp = transform( op, transformBy, { isStrong: true } );
  1854. expected.sourcePosition.path = [ 4, 1, 0 ];
  1855. expect( transOp.length ).to.equal( 1 );
  1856. expectOperation( transOp[ 0 ], expected );
  1857. } );
  1858. it( 'range contains transforming range and is important: convert to NoOperation', () => {
  1859. const transformBy = new MoveOperation(
  1860. new Position( root, [ 2, 2, 3 ] ),
  1861. 4,
  1862. new Position( root, [ 4, 1, 0 ] ),
  1863. baseVersion
  1864. );
  1865. const transOp = transform( op, transformBy );
  1866. expect( transOp.length ).to.equal( 1 );
  1867. expectOperation( transOp[ 0 ], {
  1868. type: NoOperation,
  1869. baseVersion: baseVersion + 1
  1870. } );
  1871. } );
  1872. it( 'range contains transforming range and is less important: update range path', () => {
  1873. const transformBy = new MoveOperation(
  1874. new Position( root, [ 2, 2, 3 ] ),
  1875. 4,
  1876. new Position( root, [ 4, 1, 0 ] ),
  1877. baseVersion
  1878. );
  1879. const transOp = transform( op, transformBy, { isStrong: true } );
  1880. expected.sourcePosition.path = [ 4, 1, 1 ];
  1881. expect( transOp.length ).to.equal( 1 );
  1882. expectOperation( transOp[ 0 ], expected );
  1883. } );
  1884. it( 'range contains transforming range and target and is important: update range path and target', () => {
  1885. op.targetPosition.path = [ 2, 2, 7 ];
  1886. const transformBy = new MoveOperation(
  1887. new Position( root, [ 2, 2, 3 ] ),
  1888. 5,
  1889. new Position( root, [ 4, 1, 0 ] ),
  1890. baseVersion
  1891. );
  1892. const transOp = transform( op, transformBy );
  1893. expect( transOp.length ).to.equal( 1 );
  1894. expected.sourcePosition.path = [ 4, 1, 1 ];
  1895. expected.targetPosition.path = [ 4, 1, 4 ];
  1896. expectOperation( transOp[ 0 ], expected );
  1897. } );
  1898. it( 'range contains transforming range and target and is less important: update range path and target', () => {
  1899. op.targetPosition.path = [ 2, 2, 7 ];
  1900. const transformBy = new MoveOperation(
  1901. new Position( root, [ 2, 2, 3 ] ),
  1902. 5,
  1903. new Position( root, [ 4, 1, 0 ] ),
  1904. baseVersion
  1905. );
  1906. const transOp = transform( op, transformBy, { isStrong: true } );
  1907. expect( transOp.length ).to.equal( 1 );
  1908. expected.sourcePosition.path = [ 4, 1, 1 ];
  1909. expected.targetPosition.path = [ 4, 1, 4 ];
  1910. expectOperation( transOp[ 0 ], expected );
  1911. } );
  1912. it( 'range intersects on left side of transforming range and is important: shrink range', () => {
  1913. const transformBy = new MoveOperation(
  1914. new Position( root, [ 2, 2, 3 ] ),
  1915. 2,
  1916. new Position( root, [ 4, 1, 0 ] ),
  1917. baseVersion
  1918. );
  1919. const transOp = transform( op, transformBy );
  1920. expected.sourcePosition.path = [ 2, 2, 3 ];
  1921. expected.howMany = 1;
  1922. expect( transOp.length ).to.equal( 1 );
  1923. expectOperation( transOp[ 0 ], expected );
  1924. } );
  1925. it( 'range intersects on left side of transforming range and is less important: split into two operations', () => {
  1926. // Get more test cases and better code coverage
  1927. const otherRoot = new RootElement( null );
  1928. const transformBy = new MoveOperation(
  1929. new Position( root, [ 2, 2, 3 ] ),
  1930. 2,
  1931. new Position( otherRoot, [ 4, 1, 0 ] ),
  1932. baseVersion
  1933. );
  1934. const transOp = transform( op, transformBy, { isStrong: true } );
  1935. expect( transOp.length ).to.equal( 2 );
  1936. expected.sourcePosition = new Position( otherRoot, [ 4, 1, 1 ] );
  1937. expected.howMany = 1;
  1938. expectOperation( transOp[ 0 ], expected );
  1939. expected.sourcePosition = new Position( root, [ 2, 2, 3 ] );
  1940. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1941. expected.baseVersion++;
  1942. expectOperation( transOp[ 1 ], expected );
  1943. } );
  1944. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1945. const transformBy = new MoveOperation(
  1946. new Position( root, [ 2, 2, 5 ] ),
  1947. 2,
  1948. new Position( root, [ 4, 1, 0 ] ),
  1949. baseVersion
  1950. );
  1951. const transOp = transform( op, transformBy );
  1952. expected.howMany = 1;
  1953. expect( transOp.length ).to.equal( 1 );
  1954. expectOperation( transOp[ 0 ], expected );
  1955. } );
  1956. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1957. const transformBy = new MoveOperation(
  1958. new Position( root, [ 2, 2, 5 ] ),
  1959. 2,
  1960. new Position( root, [ 4, 1, 0 ] ),
  1961. baseVersion
  1962. );
  1963. const transOp = transform( op, transformBy, { isStrong: true } );
  1964. expect( transOp.length ).to.equal( 2 );
  1965. expected.sourcePosition = sourcePosition;
  1966. expected.howMany = 1;
  1967. expectOperation( transOp[ 0 ], expected );
  1968. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1969. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1970. expected.baseVersion++;
  1971. expectOperation( transOp[ 1 ], expected );
  1972. } );
  1973. it( 'range intersects on left side, target inside transforming range and is important: split into two operations', () => {
  1974. op.howMany = 4;
  1975. const transformBy = new MoveOperation(
  1976. new Position( root, [ 2, 2, 3 ] ),
  1977. 2,
  1978. new Position( root, [ 2, 2, 6 ] ),
  1979. baseVersion
  1980. );
  1981. const transOp = transform( op, transformBy );
  1982. expect( transOp.length ).to.equal( 2 );
  1983. expected.sourcePosition.path = [ 2, 2, 3 ];
  1984. expected.howMany = 1;
  1985. expectOperation( transOp[ 0 ], expected );
  1986. expected.sourcePosition.path = [ 2, 2, 5 ];
  1987. expected.howMany = 2;
  1988. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1989. expected.baseVersion++;
  1990. expectOperation( transOp[ 1 ], expected );
  1991. } );
  1992. it( 'range intersects on left side, target inside transforming range and is less important: split into two operations', () => {
  1993. op.howMany = 4;
  1994. const transformBy = new MoveOperation(
  1995. new Position( root, [ 2, 2, 3 ] ),
  1996. 2,
  1997. new Position( root, [ 2, 2, 6 ] ),
  1998. baseVersion
  1999. );
  2000. const transOp = transform( op, transformBy, { isStrong: true } );
  2001. expect( transOp.length ).to.equal( 2 );
  2002. expected.sourcePosition.path = [ 2, 2, 3 ];
  2003. expected.howMany = 1;
  2004. expectOperation( transOp[ 0 ], expected );
  2005. expected.sourcePosition.path = [ 2, 2, 5 ];
  2006. expected.howMany = 2;
  2007. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  2008. expected.baseVersion++;
  2009. expectOperation( transOp[ 1 ], expected );
  2010. } );
  2011. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  2012. const transformBy = new MoveOperation(
  2013. new Position( root, [ 2, 2, 5 ] ),
  2014. 2,
  2015. new Position( root, [ 4, 1, 0 ] ),
  2016. baseVersion
  2017. );
  2018. const transOp = transform( op, transformBy );
  2019. expected.howMany = 1;
  2020. expect( transOp.length ).to.equal( 1 );
  2021. expectOperation( transOp[ 0 ], expected );
  2022. } );
  2023. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  2024. const transformBy = new MoveOperation(
  2025. new Position( root, [ 2, 2, 5 ] ),
  2026. 2,
  2027. new Position( root, [ 4, 1, 0 ] ),
  2028. baseVersion
  2029. );
  2030. const transOp = transform( op, transformBy, { isStrong: true } );
  2031. expect( transOp.length ).to.equal( 2 );
  2032. expected.sourcePosition = sourcePosition;
  2033. expected.howMany = 1;
  2034. expectOperation( transOp[ 0 ], expected );
  2035. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  2036. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  2037. expected.baseVersion++;
  2038. expectOperation( transOp[ 1 ], expected );
  2039. } );
  2040. it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
  2041. op.targetPosition.path = [ 2, 2, 7 ];
  2042. const transformBy = new MoveOperation(
  2043. new Position( root, [ 2, 2, 5 ] ),
  2044. 4,
  2045. new Position( root, [ 4, 1, 0 ] ),
  2046. baseVersion
  2047. );
  2048. const transOp = transform( op, transformBy, { isStrong: true } );
  2049. expect( transOp.length ).to.equal( 2 );
  2050. expected.sourcePosition.path = [ 2, 2, 4 ];
  2051. expected.targetPosition.path = [ 4, 1, 2 ];
  2052. expected.howMany = 1;
  2053. expectOperation( transOp[ 0 ], expected );
  2054. expected.sourcePosition.path = [ 4, 1, 0 ];
  2055. expected.targetPosition.path = [ 4, 1, 3 ];
  2056. expected.baseVersion++;
  2057. expectOperation( transOp[ 1 ], expected );
  2058. } );
  2059. it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
  2060. op.targetPosition.path = [ 2, 2, 7 ];
  2061. const transformBy = new MoveOperation(
  2062. new Position( root, [ 2, 2, 5 ] ),
  2063. 4,
  2064. new Position( root, [ 4, 1, 0 ] ),
  2065. baseVersion
  2066. );
  2067. const transOp = transform( op, transformBy );
  2068. expect( transOp.length ).to.equal( 1 );
  2069. expected.sourcePosition.path = [ 2, 2, 4 ];
  2070. expected.targetPosition.path = [ 4, 1, 2 ];
  2071. expected.howMany = 1;
  2072. expectOperation( transOp[ 0 ], expected );
  2073. } );
  2074. it( 'range inside transforming range and is important: split into two operations', () => {
  2075. op.howMany = 4;
  2076. const transformBy = new MoveOperation(
  2077. new Position( root, [ 2, 2, 5 ] ),
  2078. 2,
  2079. new Position( root, [ 4, 1, 0 ] ),
  2080. baseVersion
  2081. );
  2082. const transOp = transform( op, transformBy );
  2083. expect( transOp.length ).to.equal( 2 );
  2084. expected.sourcePosition.path = [ 2, 2, 4 ];
  2085. expected.howMany = 1;
  2086. expectOperation( transOp[ 0 ], expected );
  2087. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  2088. expected.baseVersion++;
  2089. expectOperation( transOp[ 1 ], expected );
  2090. } );
  2091. it( 'range inside transforming range and is less important: split into three operations', () => {
  2092. op.howMany = 4;
  2093. const transformBy = new MoveOperation(
  2094. new Position( root, [ 2, 2, 5 ] ),
  2095. 2,
  2096. new Position( root, [ 4, 1, 0 ] ),
  2097. baseVersion
  2098. );
  2099. const transOp = transform( op, transformBy, { isStrong: true } );
  2100. expect( transOp.length ).to.equal( 3 );
  2101. expected.sourcePosition.path = [ 2, 2, 4 ];
  2102. expected.howMany = 1;
  2103. expectOperation( transOp[ 0 ], expected );
  2104. expected.sourcePosition.path = [ 4, 1, 0 ];
  2105. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  2106. expected.howMany = 2;
  2107. expected.baseVersion++;
  2108. expectOperation( transOp[ 1 ], expected );
  2109. expected.sourcePosition.path = [ 2, 2, 4 ];
  2110. expected.targetPosition = targetPosition.getShiftedBy( 3 );
  2111. expected.howMany = 1;
  2112. expected.baseVersion++;
  2113. expectOperation( transOp[ 2 ], expected );
  2114. } );
  2115. it( 'range and target inside transforming range and is important: no operation update', () => {
  2116. op.howMany = 6;
  2117. const transformBy = new MoveOperation(
  2118. new Position( root, [ 2, 2, 5 ] ),
  2119. 2,
  2120. new Position( root, [ 2, 2, 9 ] ),
  2121. baseVersion
  2122. );
  2123. const transOp = transform( op, transformBy );
  2124. expect( transOp.length ).to.equal( 1 );
  2125. expected.howMany = 6;
  2126. expectOperation( transOp[ 0 ], expected );
  2127. } );
  2128. it( 'range and target inside transforming range and is less important: no operation update', () => {
  2129. op.howMany = 6;
  2130. const transformBy = new MoveOperation(
  2131. new Position( root, [ 2, 2, 5 ] ),
  2132. 2,
  2133. new Position( root, [ 2, 2, 9 ] ),
  2134. baseVersion
  2135. );
  2136. const transOp = transform( op, transformBy, { isStrong: true } );
  2137. expect( transOp.length ).to.equal( 1 );
  2138. expected.howMany = 6;
  2139. expectOperation( transOp[ 0 ], expected );
  2140. } );
  2141. } );
  2142. describe( 'by RemoveOperation', () => {
  2143. let transformBy;
  2144. beforeEach( () => {
  2145. transformBy = new RemoveOperation(
  2146. Position.createFromPosition( op.sourcePosition ),
  2147. op.howMany,
  2148. new Position( doc.graveyard, [ 0 ] ),
  2149. baseVersion
  2150. );
  2151. } );
  2152. it( 'should skip context.isStrong and be less important than RemoveOperation', () => {
  2153. const transOp = transform( op, transformBy, { isStrong: true } );
  2154. expect( transOp.length ).to.equal( 1 );
  2155. expectOperation( transOp[ 0 ], {
  2156. type: NoOperation,
  2157. baseVersion: baseVersion + 1
  2158. } );
  2159. } );
  2160. it( 'should use context.isStrong if forceWeakRemove flag is used', () => {
  2161. const transOp = transform( op, transformBy, { isStrong: true, forceWeakRemove: true } );
  2162. expect( transOp.length ).to.equal( 1 );
  2163. expected.sourcePosition = transformBy.targetPosition;
  2164. expectOperation( transOp[ 0 ], expected );
  2165. } );
  2166. } );
  2167. describe( 'by NoOperation', () => {
  2168. it( 'no operation update', () => {
  2169. const transformBy = new NoOperation( baseVersion );
  2170. const transOp = transform( op, transformBy );
  2171. expect( transOp.length ).to.equal( 1 );
  2172. expectOperation( transOp[ 0 ], expected );
  2173. } );
  2174. } );
  2175. describe( 'by RenameOperation', () => {
  2176. it( 'no position update', () => {
  2177. const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', baseVersion );
  2178. const transOp = transform( op, transformBy );
  2179. expect( transOp.length ).to.equal( 1 );
  2180. expectOperation( transOp[ 0 ], expected );
  2181. } );
  2182. } );
  2183. describe( 'by MarkerOperation', () => {
  2184. it( 'no position update', () => {
  2185. const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
  2186. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  2187. const transOp = transform( op, transformBy );
  2188. expect( transOp.length ).to.equal( 1 );
  2189. expectOperation( transOp[ 0 ], expected );
  2190. } );
  2191. } );
  2192. } );
  2193. describe( 'RemoveOperation', () => {
  2194. describe( 'by MoveOperation', () => {
  2195. it( 'should force removing content even if was less important', () => {
  2196. const op = new RemoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), baseVersion );
  2197. const targetPosition = Position.createFromPosition( op.targetPosition );
  2198. const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), baseVersion );
  2199. const sourcePosition = Position.createFromPosition( transformBy.targetPosition );
  2200. const transOp = transform( op, transformBy );
  2201. expect( transOp.length ).to.equal( 1 );
  2202. expectOperation( transOp[ 0 ], {
  2203. type: RemoveOperation,
  2204. howMany: 2,
  2205. sourcePosition,
  2206. targetPosition,
  2207. baseVersion: baseVersion + 1
  2208. } );
  2209. } );
  2210. } );
  2211. } );
  2212. describe( 'NoOperation', () => {
  2213. beforeEach( () => {
  2214. op = new NoOperation( baseVersion );
  2215. expected = {
  2216. type: NoOperation,
  2217. baseVersion: baseVersion + 1
  2218. };
  2219. } );
  2220. describe( 'by InsertOperation', () => {
  2221. it( 'no operation update', () => {
  2222. const transformBy = new InsertOperation(
  2223. new Position( root, [ 0 ] ),
  2224. 'a',
  2225. baseVersion
  2226. );
  2227. const transOp = transform( op, transformBy );
  2228. expect( transOp.length ).to.equal( 1 );
  2229. expectOperation( transOp[ 0 ], expected );
  2230. } );
  2231. } );
  2232. describe( 'by AttributeOperation', () => {
  2233. it( 'no operation update', () => {
  2234. const transformBy = new AttributeOperation(
  2235. new Range(
  2236. new Position( root, [ 0 ] ),
  2237. new Position( root, [ 1 ] )
  2238. ),
  2239. 'foo',
  2240. 'bar',
  2241. 'xyz',
  2242. baseVersion
  2243. );
  2244. const transOp = transform( op, transformBy );
  2245. expect( transOp.length ).to.equal( 1 );
  2246. expectOperation( transOp[ 0 ], expected );
  2247. } );
  2248. } );
  2249. describe( 'by RootAttributeOperation', () => {
  2250. it( 'no operation update', () => {
  2251. const transformBy = new RootAttributeOperation(
  2252. root,
  2253. 'foo',
  2254. null,
  2255. 'bar',
  2256. baseVersion
  2257. );
  2258. const transOp = transform( op, transformBy );
  2259. expect( transOp.length ).to.equal( 1 );
  2260. expectOperation( transOp[ 0 ], expected );
  2261. } );
  2262. } );
  2263. describe( 'by MoveOperation', () => {
  2264. it( 'no operation update', () => {
  2265. const transformBy = new MoveOperation(
  2266. new Position( root, [ 0 ] ),
  2267. 2,
  2268. new Position( root, [ 1 ] ),
  2269. baseVersion
  2270. );
  2271. const transOp = transform( op, transformBy );
  2272. expect( transOp.length ).to.equal( 1 );
  2273. expectOperation( transOp[ 0 ], expected );
  2274. } );
  2275. } );
  2276. describe( 'by NoOperation', () => {
  2277. it( 'no operation update', () => {
  2278. const transformBy = new NoOperation( baseVersion );
  2279. const transOp = transform( op, transformBy );
  2280. expect( transOp.length ).to.equal( 1 );
  2281. expectOperation( transOp[ 0 ], expected );
  2282. } );
  2283. } );
  2284. describe( 'by RenameOperation', () => {
  2285. it( 'no position update', () => {
  2286. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', baseVersion );
  2287. const transOp = transform( op, transformBy );
  2288. expect( transOp.length ).to.equal( 1 );
  2289. expectOperation( transOp[ 0 ], expected );
  2290. } );
  2291. } );
  2292. describe( 'by MarkerOperation', () => {
  2293. it( 'no position update', () => {
  2294. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  2295. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  2296. const transOp = transform( op, transformBy );
  2297. expect( transOp.length ).to.equal( 1 );
  2298. expectOperation( transOp[ 0 ], expected );
  2299. } );
  2300. } );
  2301. } );
  2302. describe( 'RenameOperation', () => {
  2303. beforeEach( () => {
  2304. const position = new Position( root, [ 0, 2, 2 ] );
  2305. op = new RenameOperation( position, 'oldName', 'newName', baseVersion );
  2306. expected = {
  2307. position,
  2308. oldName: 'oldName',
  2309. newName: 'newName',
  2310. baseVersion: baseVersion + 1
  2311. };
  2312. } );
  2313. describe( 'by InsertOperation', () => {
  2314. it( 'target before renamed element: offset update', () => {
  2315. const transformBy = new InsertOperation(
  2316. new Position( root, [ 0, 2, 1 ] ),
  2317. [ nodeA, nodeB ],
  2318. baseVersion
  2319. );
  2320. const transOp = transform( op, transformBy );
  2321. expect( transOp.length ).to.equal( 1 );
  2322. expected.position.offset = 4;
  2323. expectOperation( transOp[ 0 ], expected );
  2324. } );
  2325. it( 'target after renamed element: no change', () => {
  2326. const transformBy = new InsertOperation(
  2327. new Position( root, [ 0, 2, 3 ] ),
  2328. [ nodeA, nodeB ],
  2329. baseVersion
  2330. );
  2331. const transOp = transform( op, transformBy );
  2332. expect( transOp.length ).to.equal( 1 );
  2333. expectOperation( transOp[ 0 ], expected );
  2334. } );
  2335. it( 'target before a node on path to renamed element: path update', () => {
  2336. const transformBy = new InsertOperation(
  2337. new Position( root, [ 0, 1 ] ),
  2338. [ nodeA, nodeB ],
  2339. baseVersion
  2340. );
  2341. const transOp = transform( op, transformBy );
  2342. expect( transOp.length ).to.equal( 1 );
  2343. expected.position.path = [ 0, 4, 2 ];
  2344. expectOperation( transOp[ 0 ], expected );
  2345. } );
  2346. it( 'target after a node on path to renamed element: no change', () => {
  2347. const transformBy = new InsertOperation(
  2348. new Position( root, [ 0, 3 ] ),
  2349. [ nodeA, nodeB ],
  2350. baseVersion
  2351. );
  2352. const transOp = transform( op, transformBy );
  2353. expect( transOp.length ).to.equal( 1 );
  2354. expectOperation( transOp[ 0 ], expected );
  2355. } );
  2356. } );
  2357. describe( 'by AttributeOperation', () => {
  2358. it( 'no operation update', () => {
  2359. const transformBy = new AttributeOperation(
  2360. new Range(
  2361. new Position( root, [ 0, 2, 1 ] ),
  2362. new Position( root, [ 1, 3 ] )
  2363. ),
  2364. 'foo',
  2365. 'bar',
  2366. 'xyz',
  2367. baseVersion
  2368. );
  2369. const transOp = transform( op, transformBy );
  2370. expect( transOp.length ).to.equal( 1 );
  2371. expectOperation( transOp[ 0 ], expected );
  2372. } );
  2373. } );
  2374. describe( 'by RootAttributeOperation', () => {
  2375. it( 'no operation update', () => {
  2376. const transformBy = new RootAttributeOperation(
  2377. root,
  2378. 'foo',
  2379. null,
  2380. 'bar',
  2381. baseVersion
  2382. );
  2383. const transOp = transform( op, transformBy );
  2384. expect( transOp.length ).to.equal( 1 );
  2385. expectOperation( transOp[ 0 ], expected );
  2386. } );
  2387. } );
  2388. describe( 'by MarkerOperation', () => {
  2389. it( 'no operation update', () => {
  2390. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  2391. const transformBy = new MarkerOperation( 'name', null, newRange, doc.markers, baseVersion );
  2392. const transOp = transform( op, transformBy );
  2393. expect( transOp.length ).to.equal( 1 );
  2394. expectOperation( transOp[ 0 ], expected );
  2395. } );
  2396. } );
  2397. describe( 'by RenameOperation', () => {
  2398. it( 'different element: no change', () => {
  2399. const transformBy = new RenameOperation(
  2400. new Position( root, [ 0, 2, 1 ] ),
  2401. 'foo',
  2402. 'bar',
  2403. baseVersion
  2404. );
  2405. const transOp = transform( op, transformBy );
  2406. expect( transOp.length ).to.equal( 1 );
  2407. expectOperation( transOp[ 0 ], expected );
  2408. } );
  2409. it( 'same element and is important: convert to NoOperation', () => {
  2410. const transformBy = new RenameOperation(
  2411. new Position( root, [ 0, 2, 2 ] ),
  2412. 'oldName',
  2413. 'otherName',
  2414. baseVersion
  2415. );
  2416. const transOp = transform( op, transformBy );
  2417. expect( transOp.length ).to.equal( 1 );
  2418. expectOperation( transOp[ 0 ], {
  2419. type: NoOperation,
  2420. baseVersion: baseVersion + 1
  2421. } );
  2422. } );
  2423. it( 'same element and is not important: change old name to new name', () => {
  2424. const transformBy = new RenameOperation(
  2425. new Position( root, [ 0, 2, 2 ] ),
  2426. 'oldName',
  2427. 'otherName',
  2428. baseVersion
  2429. );
  2430. const transOp = transform( op, transformBy, { isStrong: true } );
  2431. expect( transOp.length ).to.equal( 1 );
  2432. expected.oldName = 'otherName';
  2433. expectOperation( transOp[ 0 ], expected );
  2434. } );
  2435. } );
  2436. describe( 'by MoveOperation', () => {
  2437. it( 'moved range before renamed element: update offset', () => {
  2438. const transformBy = new MoveOperation(
  2439. new Position( root, [ 0, 2, 0 ] ),
  2440. 2,
  2441. new Position( root, [ 2, 5 ] ),
  2442. baseVersion
  2443. );
  2444. const transOp = transform( op, transformBy );
  2445. expect( transOp.length ).to.equal( 1 );
  2446. expected.position.offset = 0;
  2447. expectOperation( transOp[ 0 ], expected );
  2448. } );
  2449. it( 'moved range before an element on path to renamed element: update path', () => {
  2450. const transformBy = new MoveOperation(
  2451. new Position( root, [ 0, 0 ] ),
  2452. 2,
  2453. new Position( root, [ 2, 5 ] ),
  2454. baseVersion
  2455. );
  2456. const transOp = transform( op, transformBy );
  2457. expect( transOp.length ).to.equal( 1 );
  2458. expected.position.path = [ 0, 0, 2 ];
  2459. expectOperation( transOp[ 0 ], expected );
  2460. } );
  2461. it( 'moved range contains renamed element: update path', () => {
  2462. const transformBy = new MoveOperation(
  2463. new Position( root, [ 0, 2, 1 ] ),
  2464. 3,
  2465. new Position( root, [ 2, 5 ] ),
  2466. baseVersion
  2467. );
  2468. const transOp = transform( op, transformBy );
  2469. expect( transOp.length ).to.equal( 1 );
  2470. expected.position.path = [ 2, 6 ];
  2471. expectOperation( transOp[ 0 ], expected );
  2472. } );
  2473. it( 'moved range contains renamed element parent: updated path', () => {
  2474. const transformBy = new MoveOperation(
  2475. new Position( root, [ 0, 1 ] ),
  2476. 3,
  2477. new Position( root, [ 2, 5 ] ),
  2478. baseVersion
  2479. );
  2480. const transOp = transform( op, transformBy );
  2481. expect( transOp.length ).to.equal( 1 );
  2482. expected.position.path = [ 2, 6, 2 ];
  2483. expectOperation( transOp[ 0 ], expected );
  2484. } );
  2485. it( 'move target before renamed element: update offset', () => {
  2486. const transformBy = new MoveOperation(
  2487. new Position( root, [ 2, 5 ] ),
  2488. 2,
  2489. new Position( root, [ 0, 2, 1 ] ),
  2490. baseVersion
  2491. );
  2492. const transOp = transform( op, transformBy );
  2493. expect( transOp.length ).to.equal( 1 );
  2494. expected.position.offset = 4;
  2495. expectOperation( transOp[ 0 ], expected );
  2496. } );
  2497. it( 'move target before an element on path to renamed element: update path', () => {
  2498. const transformBy = new MoveOperation(
  2499. new Position( root, [ 0, 2, 0 ] ),
  2500. 2,
  2501. new Position( root, [ 2, 5 ] ),
  2502. baseVersion
  2503. );
  2504. const transOp = transform( op, transformBy );
  2505. expect( transOp.length ).to.equal( 1 );
  2506. expected.position.offset = 0;
  2507. expectOperation( transOp[ 0 ], expected );
  2508. } );
  2509. } );
  2510. } );
  2511. describe( 'MarkerOperation', () => {
  2512. let oldRange, newRange;
  2513. beforeEach( () => {
  2514. oldRange = Range.createFromParentsAndOffsets( root, 1, root, 4 );
  2515. newRange = Range.createFromParentsAndOffsets( root, 10, root, 12 );
  2516. op = new MarkerOperation( 'name', oldRange, newRange, doc.markers, baseVersion );
  2517. expected = {
  2518. name: 'name',
  2519. oldRange,
  2520. newRange,
  2521. baseVersion: baseVersion + 1
  2522. };
  2523. } );
  2524. describe( 'by InsertOperation', () => {
  2525. it( 'insert position affecting oldRange: update oldRange', () => {
  2526. // Just CC things.
  2527. op.newRange = null;
  2528. const transformBy = new InsertOperation( Position.createAt( root, 0 ), [ nodeA, nodeB ], baseVersion );
  2529. const transOp = transform( op, transformBy );
  2530. expected.newRange = null;
  2531. expected.oldRange.start.offset = 3;
  2532. expected.oldRange.end.offset = 6;
  2533. expect( transOp.length ).to.equal( 1 );
  2534. expectOperation( transOp[ 0 ], expected );
  2535. } );
  2536. it( 'insert position affecting newRange: update newRange', () => {
  2537. // Just CC things.
  2538. op.oldRange = null;
  2539. const transformBy = new InsertOperation( Position.createAt( root, 8 ), [ nodeA, nodeB ], baseVersion );
  2540. const transOp = transform( op, transformBy );
  2541. expected.oldRange = null;
  2542. expected.newRange.start.offset = 12;
  2543. expected.newRange.end.offset = 14;
  2544. expect( transOp.length ).to.equal( 1 );
  2545. expectOperation( transOp[ 0 ], expected );
  2546. } );
  2547. } );
  2548. describe( 'by AttributeOperation', () => {
  2549. it( 'no operation update', () => {
  2550. const transformBy = new AttributeOperation(
  2551. new Range(
  2552. new Position( root, [ 2 ] ),
  2553. new Position( root, [ 11 ] )
  2554. ),
  2555. 'foo',
  2556. 'bar',
  2557. 'xyz',
  2558. baseVersion
  2559. );
  2560. const transOp = transform( op, transformBy );
  2561. expect( transOp.length ).to.equal( 1 );
  2562. expectOperation( transOp[ 0 ], expected );
  2563. } );
  2564. } );
  2565. describe( 'by MoveOperation', () => {
  2566. it( 'moved range is before oldRange: update oldRange', () => {
  2567. // Just CC things.
  2568. op.newRange = null;
  2569. const transformBy = new MoveOperation( Position.createAt( root, 0 ), 1, Position.createAt( root, 20 ), baseVersion );
  2570. const transOp = transform( op, transformBy );
  2571. expected.newRange = null;
  2572. expected.oldRange.start.offset = 0;
  2573. expected.oldRange.end.offset = 3;
  2574. expect( transOp.length ).to.equal( 1 );
  2575. expectOperation( transOp[ 0 ], expected );
  2576. } );
  2577. it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
  2578. const transformBy = new MoveOperation( Position.createAt( root, 2 ), 2, Position.createAt( root, 20 ), baseVersion );
  2579. const transOp = transform( op, transformBy );
  2580. expected.oldRange.start.offset = 1;
  2581. expected.oldRange.end.offset = 2;
  2582. expected.newRange.start.offset = 8;
  2583. expected.newRange.end.offset = 10;
  2584. expect( transOp.length ).to.equal( 1 );
  2585. expectOperation( transOp[ 0 ], expected );
  2586. } );
  2587. it( 'target position is inside newRange: update newRange', () => {
  2588. // Just CC things.
  2589. op.oldRange = null;
  2590. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 2, Position.createAt( root, 11 ), baseVersion );
  2591. const transOp = transform( op, transformBy );
  2592. expected.oldRange = null;
  2593. expected.newRange.start.offset = 10;
  2594. expected.newRange.end.offset = 14;
  2595. expect( transOp.length ).to.equal( 1 );
  2596. expectOperation( transOp[ 0 ], expected );
  2597. } );
  2598. it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
  2599. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 4, Position.createAt( root, 2 ), baseVersion );
  2600. const transOp = transform( op, transformBy );
  2601. expected.oldRange.start.offset = 1;
  2602. expected.oldRange.end.offset = 8;
  2603. expected.newRange.start.offset = 14;
  2604. expected.newRange.end.offset = 16;
  2605. expect( transOp.length ).to.equal( 1 );
  2606. expectOperation( transOp[ 0 ], expected );
  2607. } );
  2608. } );
  2609. describe( 'by RootAttributeOperation', () => {
  2610. it( 'no operation update', () => {
  2611. const transformBy = new RootAttributeOperation(
  2612. root,
  2613. 'foo',
  2614. null,
  2615. 'bar',
  2616. baseVersion
  2617. );
  2618. const transOp = transform( op, transformBy );
  2619. expect( transOp.length ).to.equal( 1 );
  2620. expectOperation( transOp[ 0 ], expected );
  2621. } );
  2622. } );
  2623. describe( 'by RenameOperation', () => {
  2624. it( 'no operation update', () => {
  2625. const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', baseVersion );
  2626. const transOp = transform( op, transformBy );
  2627. expect( transOp.length ).to.equal( 1 );
  2628. expectOperation( transOp[ 0 ], expected );
  2629. } );
  2630. } );
  2631. describe( 'by MarkerOperation', () => {
  2632. it( 'different marker name: no operation update', () => {
  2633. const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, doc.markers, baseVersion );
  2634. const transOp = transform( op, transformBy );
  2635. expect( transOp.length ).to.equal( 1 );
  2636. expectOperation( transOp[ 0 ], expected );
  2637. } );
  2638. it( 'same marker name and is important: convert to NoOperation', () => {
  2639. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  2640. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, doc.markers, baseVersion );
  2641. const transOp = transform( op, transformBy );
  2642. expect( transOp.length ).to.equal( 1 );
  2643. expectOperation( transOp[ 0 ], {
  2644. type: NoOperation,
  2645. baseVersion: baseVersion + 1
  2646. } );
  2647. } );
  2648. it( 'same marker name and is less important: update oldRange parameter', () => {
  2649. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  2650. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, doc.markers, baseVersion );
  2651. const transOp = transform( op, transformBy, { isStrong: true } );
  2652. expected.oldRange = anotherRange;
  2653. expect( transOp.length ).to.equal( 1 );
  2654. expectOperation( transOp[ 0 ], expected );
  2655. } );
  2656. } );
  2657. } );
  2658. } );