transform.js 92 KB

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