8
0

transform.js 95 KB

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