8
0

transform.js 95 KB

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