8
0

transform.js 92 KB

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