8
0

transform.js 95 KB

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