8
0

transform.js 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transform from '../../../src/model/operation/transform';
  6. import Model from '../../../src/model/model';
  7. import RootElement from '../../../src/model/rootelement';
  8. import Node from '../../../src/model/node';
  9. import Position from '../../../src/model/position';
  10. import Range from '../../../src/model/range';
  11. import InsertOperation from '../../../src/model/operation/insertoperation';
  12. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  13. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  14. import MarkerOperation from '../../../src/model/operation/markeroperation';
  15. import MoveOperation from '../../../src/model/operation/moveoperation';
  16. import RemoveOperation from '../../../src/model/operation/removeoperation';
  17. import RenameOperation from '../../../src/model/operation/renameoperation';
  18. import NoOperation from '../../../src/model/operation/nooperation';
  19. describe( 'transform', () => {
  20. let model, doc, root, op, nodeA, nodeB, expected;
  21. beforeEach( () => {
  22. model = new Model();
  23. doc = model.document;
  24. root = doc.createRoot();
  25. nodeA = new Node();
  26. nodeB = new Node();
  27. } );
  28. function expectOperation( op, params ) {
  29. for ( const i in params ) {
  30. if ( params.hasOwnProperty( i ) ) {
  31. if ( i == 'type' ) {
  32. expect( op, 'type' ).to.be.instanceof( params[ i ] );
  33. }
  34. else if ( params[ i ] instanceof Array ) {
  35. expect( op[ i ].length, i ).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 ] ), i ).to.be.true;
  41. } else {
  42. expect( op[ i ], 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 ], 0 );
  54. expected = {
  55. type: InsertOperation,
  56. position: Position.createFromPosition( position )
  57. };
  58. } );
  59. describe( 'by InsertOperation', () => {
  60. it( 'target at different position: no position update', () => {
  61. const transformBy = new InsertOperation(
  62. new Position( root, [ 1, 3, 2 ] ),
  63. [ nodeC, nodeD ],
  64. 0
  65. );
  66. const transOp = transform.transform( op, transformBy );
  67. expect( transOp.length ).to.equal( 1 );
  68. expectOperation( transOp[ 0 ], expected );
  69. } );
  70. it( 'target at offset before: increment offset', () => {
  71. const transformBy = new InsertOperation(
  72. new Position( root, [ 0, 2, 0 ] ),
  73. [ nodeC, nodeD ],
  74. 0
  75. );
  76. const transOp = transform.transform( op, transformBy );
  77. expected.position.offset += 2;
  78. expect( transOp.length ).to.equal( 1 );
  79. expectOperation( transOp[ 0 ], expected );
  80. } );
  81. it( 'target at same offset and is important: increment offset', () => {
  82. const transformBy = new InsertOperation(
  83. new Position( root, [ 0, 2, 2 ] ),
  84. [ nodeC, nodeD ],
  85. 0
  86. );
  87. const transOp = transform.transform( op, transformBy );
  88. expected.position.offset += 2;
  89. expect( transOp.length ).to.equal( 1 );
  90. expectOperation( transOp[ 0 ], expected );
  91. } );
  92. it( 'target at same offset and is less important: no position update', () => {
  93. const transformBy = new InsertOperation(
  94. new Position( root, [ 0, 2, 2 ] ),
  95. [ nodeC, nodeD ],
  96. 0
  97. );
  98. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  99. expect( transOp.length ).to.equal( 1 );
  100. expectOperation( transOp[ 0 ], expected );
  101. } );
  102. it( 'target at same offset and context.insertBefore = false: increment offset', () => {
  103. const transformBy = new InsertOperation(
  104. new Position( root, [ 0, 2, 2 ] ),
  105. [ nodeC, nodeD ],
  106. 0
  107. );
  108. const transOp = transform.transform( op, transformBy );
  109. expected.position.offset += 2;
  110. expect( transOp.length ).to.equal( 1 );
  111. expectOperation( transOp[ 0 ], expected );
  112. } );
  113. it( 'target at offset after: no position update', () => {
  114. const transformBy = new InsertOperation(
  115. new Position( root, [ 0, 2, 3 ] ),
  116. [ nodeC, nodeD ],
  117. 0
  118. );
  119. const transOp = transform.transform( op, transformBy );
  120. expect( transOp.length ).to.equal( 1 );
  121. expectOperation( transOp[ 0 ], expected );
  122. } );
  123. it( 'target before node from path: increment index on path', () => {
  124. const transformBy = new InsertOperation(
  125. new Position( root, [ 0, 1 ] ),
  126. [ nodeC, nodeD ],
  127. 0
  128. );
  129. const transOp = transform.transform( op, transformBy );
  130. expected.position.path[ 1 ] += 2;
  131. expect( transOp.length ).to.equal( 1 );
  132. expectOperation( transOp[ 0 ], expected );
  133. } );
  134. it( 'target after node from path: no position update', () => {
  135. const transformBy = new InsertOperation(
  136. new Position( root, [ 0, 6 ] ),
  137. [ nodeC, nodeD ],
  138. 0
  139. );
  140. const transOp = transform.transform( op, transformBy );
  141. expect( transOp.length ).to.equal( 1 );
  142. expectOperation( transOp[ 0 ], expected );
  143. } );
  144. } );
  145. describe( 'by AttributeOperation', () => {
  146. it( 'no position update', () => {
  147. const transformBy = new AttributeOperation(
  148. Range.createFromPositionAndShift( position, 2 ),
  149. 'foo',
  150. null,
  151. 'bar',
  152. 0
  153. );
  154. const transOp = transform.transform( op, transformBy );
  155. expect( transOp.length ).to.equal( 1 );
  156. expectOperation( transOp[ 0 ], expected );
  157. } );
  158. } );
  159. describe( 'by RootAttributeOperation', () => {
  160. it( 'no position update', () => {
  161. const transformBy = new RootAttributeOperation(
  162. root,
  163. 'foo',
  164. null,
  165. 'bar',
  166. 0
  167. );
  168. const transOp = transform.transform( op, transformBy );
  169. expect( transOp.length ).to.equal( 1 );
  170. expectOperation( transOp[ 0 ], expected );
  171. } );
  172. } );
  173. describe( 'by MoveOperation', () => {
  174. it( 'range and target are different than insert position: no position update', () => {
  175. const transformBy = new MoveOperation(
  176. new Position( root, [ 1, 3, 2 ] ),
  177. 2,
  178. new Position( root, [ 2, 1 ] ),
  179. 0
  180. );
  181. const transOp = transform.transform( op, transformBy );
  182. expect( transOp.length ).to.equal( 1 );
  183. expectOperation( transOp[ 0 ], expected );
  184. } );
  185. it( 'range is before insert position offset: decrement offset', () => {
  186. const transformBy = new MoveOperation(
  187. new Position( root, [ 0, 2, 0 ] ),
  188. 1,
  189. new Position( root, [ 1, 1 ] ),
  190. 0
  191. );
  192. const transOp = transform.transform( op, transformBy );
  193. expected.position.offset--;
  194. expect( transOp.length ).to.equal( 1 );
  195. expectOperation( transOp[ 0 ], expected );
  196. } );
  197. it( 'range offset is after insert position offset: no position update', () => {
  198. const transformBy = new MoveOperation(
  199. new Position( root, [ 0, 2, 5 ] ),
  200. 1,
  201. new Position( root, [ 1, 1 ] ),
  202. 0
  203. );
  204. const transOp = transform.transform( op, transformBy );
  205. expect( transOp.length ).to.equal( 1 );
  206. expectOperation( transOp[ 0 ], expected );
  207. } );
  208. it( 'target offset before insert position offset: increment offset', () => {
  209. const transformBy = new MoveOperation(
  210. new Position( root, [ 1, 1 ] ),
  211. 2,
  212. new Position( root, [ 0, 2, 0 ] ),
  213. 0
  214. );
  215. const transOp = transform.transform( op, transformBy );
  216. expected.position.offset += 2;
  217. expect( transOp.length ).to.equal( 1 );
  218. expectOperation( transOp[ 0 ], expected );
  219. } );
  220. it( 'target offset after insert position offset: no position update', () => {
  221. const transformBy = new MoveOperation(
  222. new Position( root, [ 1, 1 ] ),
  223. 2,
  224. new Position( root, [ 0, 2, 5 ] ),
  225. 0
  226. );
  227. const transOp = transform.transform( op, transformBy );
  228. expect( transOp.length ).to.equal( 1 );
  229. expectOperation( transOp[ 0 ], expected );
  230. } );
  231. it( 'target offset same as insert position offset and is important: increment offset', () => {
  232. const transformBy = new MoveOperation(
  233. new Position( root, [ 1, 1 ] ),
  234. 2,
  235. new Position( root, [ 0, 2, 2 ] ),
  236. 0
  237. );
  238. const transOp = transform.transform( op, transformBy );
  239. expected.position.offset += 2;
  240. expect( transOp.length ).to.equal( 1 );
  241. expectOperation( transOp[ 0 ], expected );
  242. } );
  243. it( 'target offset same as insert position offset and is less important: increment offset', () => {
  244. const transformBy = new MoveOperation(
  245. new Position( root, [ 1, 1 ] ),
  246. 2,
  247. new Position( root, [ 0, 2, 2 ] ),
  248. 0
  249. );
  250. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  251. expected.position.offset += 2;
  252. expect( transOp.length ).to.equal( 1 );
  253. expectOperation( transOp[ 0 ], expected );
  254. } );
  255. it( 'range is before node from insert position path: decrement index on path', () => {
  256. const transformBy = new MoveOperation(
  257. new Position( root, [ 0, 0 ] ),
  258. 1,
  259. new Position( root, [ 1, 0 ] ),
  260. 0
  261. );
  262. const transOp = transform.transform( op, transformBy );
  263. expected.position.path[ 1 ] -= 1;
  264. expect( transOp.length ).to.equal( 1 );
  265. expectOperation( transOp[ 0 ], expected );
  266. } );
  267. it( 'range is after node from insert position path: no position update', () => {
  268. const transformBy = new MoveOperation(
  269. new Position( root, [ 0, 4 ] ),
  270. 2,
  271. new Position( root, [ 1, 0 ] ),
  272. 0
  273. );
  274. const transOp = transform.transform( op, transformBy );
  275. expect( transOp.length ).to.equal( 1 );
  276. expectOperation( transOp[ 0 ], expected );
  277. } );
  278. it( 'target before node from insert position path: increment index on path', () => {
  279. const transformBy = new MoveOperation(
  280. new Position( root, [ 1, 0 ] ),
  281. 2,
  282. new Position( root, [ 0, 0 ] ),
  283. 0
  284. );
  285. const transOp = transform.transform( op, transformBy );
  286. expected.position.path[ 1 ] += 2;
  287. expect( transOp.length ).to.equal( 1 );
  288. expectOperation( transOp[ 0 ], expected );
  289. } );
  290. it( 'target after node from insert position path: no position update', () => {
  291. const transformBy = new MoveOperation(
  292. new Position( root, [ 1, 0 ] ),
  293. 2,
  294. new Position( root, [ 0, 4 ] ),
  295. 0
  296. );
  297. const transOp = transform.transform( op, transformBy );
  298. expect( transOp.length ).to.equal( 1 );
  299. expectOperation( transOp[ 0 ], expected );
  300. } );
  301. it( 'range has node that contains insert position: update position', () => {
  302. const transformBy = new MoveOperation(
  303. new Position( root, [ 0, 1 ] ),
  304. 2,
  305. new Position( root, [ 1, 1 ] ),
  306. 0
  307. );
  308. const transOp = transform.transform( op, transformBy );
  309. expected.position.path = [ 1, 2, 2 ];
  310. expect( transOp.length ).to.equal( 1 );
  311. expectOperation( transOp[ 0 ], expected );
  312. } );
  313. it( 'range contains insert position (on same level): update position', () => {
  314. const transformBy = new MoveOperation(
  315. new Position( root, [ 0, 2, 0 ] ),
  316. 4,
  317. new Position( root, [ 1, 0 ] ),
  318. 0
  319. );
  320. const transOp = transform.transform( op, transformBy );
  321. expected.position.path = [ 1, 2 ];
  322. expect( transOp.length ).to.equal( 1 );
  323. expectOperation( transOp[ 0 ], expected );
  324. } );
  325. } );
  326. describe( 'by NoOperation', () => {
  327. it( 'no operation update', () => {
  328. const transformBy = new NoOperation( 0 );
  329. const transOp = transform.transform( op, transformBy );
  330. expect( transOp.length ).to.equal( 1 );
  331. expectOperation( transOp[ 0 ], expected );
  332. } );
  333. } );
  334. describe( 'by RenameOperation', () => {
  335. it( 'no position update', () => {
  336. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  337. const transOp = transform.transform( op, transformBy );
  338. expect( transOp.length ).to.equal( 1 );
  339. expectOperation( transOp[ 0 ], expected );
  340. } );
  341. } );
  342. describe( 'by MarkerOperation', () => {
  343. it( 'no position update', () => {
  344. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  345. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  346. const transOp = transform.transform( op, transformBy );
  347. expect( transOp.length ).to.equal( 1 );
  348. expectOperation( transOp[ 0 ], expected );
  349. } );
  350. } );
  351. } );
  352. describe( 'AttributeOperation', () => {
  353. let start, end, range;
  354. beforeEach( () => {
  355. start = new Position( root, [ 0, 2, 1 ] );
  356. end = new Position( root, [ 0, 2, 4 ] );
  357. range = new Range( start, end );
  358. op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
  359. expected = {
  360. type: AttributeOperation,
  361. range,
  362. key: 'foo',
  363. oldValue: 'abc',
  364. newValue: 'bar'
  365. };
  366. } );
  367. describe( 'by InsertOperation', () => {
  368. it( 'target at offset before: increment offset', () => {
  369. const transformBy = new InsertOperation(
  370. new Position( root, [ 0, 2, 0 ] ),
  371. [ nodeA, nodeB ],
  372. 0
  373. );
  374. const transOp = transform.transform( op, transformBy );
  375. expected.range.start.offset += 2;
  376. expected.range.end.offset += 2;
  377. expect( transOp.length ).to.equal( 1 );
  378. expectOperation( transOp[ 0 ], expected );
  379. } );
  380. it( 'target at same offset: increment offset', () => {
  381. const transformBy = new InsertOperation(
  382. new Position( root, [ 0, 2, 1 ] ),
  383. [ nodeA, nodeB ],
  384. 0
  385. );
  386. const transOp = transform.transform( op, transformBy );
  387. expected.range.start.offset += 2;
  388. expected.range.end.offset += 2;
  389. expect( transOp.length ).to.equal( 1 );
  390. expectOperation( transOp[ 0 ], expected );
  391. } );
  392. } );
  393. describe( 'by MoveOperation', () => {
  394. it( 'range offset is before change range start offset: decrement offset', () => {
  395. const transformBy = new MoveOperation(
  396. new Position( root, [ 0, 2, 0 ] ),
  397. 1,
  398. new Position( root, [ 2, 4, 1 ] ),
  399. 0
  400. );
  401. const transOp = transform.transform( op, transformBy );
  402. expected.range.start.offset--;
  403. expected.range.end.offset--;
  404. expect( transOp.length ).to.equal( 1 );
  405. expectOperation( transOp[ 0 ], expected );
  406. } );
  407. it( 'target offset is before change range start offset: increment offset', () => {
  408. const transformBy = new MoveOperation(
  409. new Position( root, [ 2, 4, 1 ] ),
  410. 2,
  411. new Position( root, [ 0, 2, 0 ] ),
  412. 0
  413. );
  414. const transOp = transform.transform( op, transformBy );
  415. expected.range.start.offset += 2;
  416. expected.range.end.offset += 2;
  417. expect( transOp.length ).to.equal( 1 );
  418. expectOperation( transOp[ 0 ], expected );
  419. } );
  420. it( 'range intersects on left with change range: split into two operations', () => {
  421. const transformBy = new MoveOperation(
  422. new Position( root, [ 0, 2, 2 ] ),
  423. 4,
  424. new Position( root, [ 2, 4, 1 ] ),
  425. 0
  426. );
  427. const transOp = transform.transform( op, transformBy );
  428. expect( transOp.length ).to.equal( 2 );
  429. expected.range.end.offset -= 2;
  430. expectOperation( transOp[ 0 ], expected );
  431. expected.range.start.path = [ 2, 4, 1 ];
  432. expected.range.end.path = [ 2, 4, 3 ];
  433. expectOperation( transOp[ 1 ], expected );
  434. } );
  435. it( 'range intersects on right with change range: split into two operation', () => {
  436. const transformBy = new MoveOperation(
  437. new Position( root, [ 0, 2, 0 ] ),
  438. 2,
  439. new Position( root, [ 2, 4, 1 ] ),
  440. 0
  441. );
  442. const transOp = transform.transform( op, transformBy );
  443. expect( transOp.length ).to.equal( 2 );
  444. expected.range.start.offset -= 1;
  445. expected.range.end.offset -= 2;
  446. expectOperation( transOp[ 0 ], expected );
  447. expected.range.start.path = [ 2, 4, 2 ];
  448. expected.range.end.path = [ 2, 4, 3 ];
  449. expectOperation( transOp[ 1 ], expected );
  450. } );
  451. it( 'range contains change range: update change range', () => {
  452. const transformBy = new MoveOperation(
  453. new Position( root, [ 0, 1 ] ),
  454. 3,
  455. new Position( root, [ 2, 4, 1 ] ),
  456. 0
  457. );
  458. const transOp = transform.transform( op, transformBy );
  459. expected.range.start.path = [ 2, 4, 2, 1 ];
  460. expected.range.end.path = [ 2, 4, 2, 4 ];
  461. expect( transOp.length ).to.equal( 1 );
  462. expectOperation( transOp[ 0 ], expected );
  463. } );
  464. it( 'range is inside change range: split into two operations', () => {
  465. const transformBy = new MoveOperation(
  466. new Position( root, [ 0, 2, 2 ] ),
  467. 1,
  468. new Position( root, [ 2, 4, 1 ] ),
  469. 0
  470. );
  471. const transOp = transform.transform( op, transformBy );
  472. expect( transOp.length ).to.equal( 3 );
  473. expected.range.start.path = [ 0, 2, 1 ];
  474. expected.range.end.path = [ 0, 2, 2 ];
  475. expectOperation( transOp[ 0 ], expected );
  476. expected.range.start.path = [ 0, 2, 2 ];
  477. expected.range.end.path = [ 0, 2, 3 ];
  478. expectOperation( transOp[ 1 ], expected );
  479. expected.range.start.path = [ 2, 4, 1 ];
  480. expected.range.end.path = [ 2, 4, 2 ];
  481. expectOperation( transOp[ 2 ], expected );
  482. } );
  483. it( 'range is same as change range: update change range', () => {
  484. const transformBy = new MoveOperation(
  485. new Position( root, [ 0, 2, 1 ] ),
  486. 3,
  487. new Position( root, [ 2, 4, 1 ] ),
  488. 0
  489. );
  490. const transOp = transform.transform( op, transformBy );
  491. expected.range.start.path = [ 2, 4, 1 ];
  492. expected.range.end.path = [ 2, 4, 4 ];
  493. expect( transOp.length ).to.equal( 1 );
  494. expectOperation( transOp[ 0 ], expected );
  495. } );
  496. it( 'target inside change range: split into two operations', () => {
  497. const transformBy = new MoveOperation(
  498. new Position( root, [ 2, 4, 1 ] ),
  499. 2,
  500. new Position( root, [ 0, 2, 2 ] ),
  501. 0
  502. );
  503. const transOp = transform.transform( op, transformBy );
  504. expect( transOp.length ).to.equal( 2 );
  505. expected.range.end.offset = 2;
  506. expectOperation( transOp[ 0 ], expected );
  507. expected.range.start.offset = 4;
  508. expected.range.end.offset = 6;
  509. expectOperation( transOp[ 1 ], expected );
  510. } );
  511. it( 'range intersects change range and target inside change range: split into three operations', () => {
  512. const transformBy = new MoveOperation(
  513. new Position( root, [ 0, 2, 0 ] ),
  514. 2,
  515. new Position( root, [ 0, 2, 3 ] ),
  516. 0
  517. );
  518. const transOp = transform.transform( op, transformBy );
  519. expect( transOp.length ).to.equal( 3 );
  520. expected.range.start.path = [ 0, 2, 0 ];
  521. expected.range.end.path = [ 0, 2, 1 ];
  522. expectOperation( transOp[ 0 ], expected );
  523. expected.range.start.path = [ 0, 2, 1 ];
  524. expected.range.end.path = [ 0, 2, 2 ];
  525. expectOperation( transOp[ 1 ], expected );
  526. expected.range.start.path = [ 0, 2, 2 ];
  527. expected.range.end.path = [ 0, 2, 3 ];
  528. expectOperation( transOp[ 2 ], expected );
  529. } );
  530. } );
  531. describe( 'by RemoveOperation', () => {
  532. beforeEach( () => {
  533. start = new Position( doc.graveyard, [ 2, 0 ] );
  534. end = new Position( doc.graveyard, [ 2, 4 ] );
  535. range = new Range( start, end );
  536. op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
  537. expected.range = new Range( start, end );
  538. } );
  539. it( 'remove operation inserted elements before attribute operation range: increment path', () => {
  540. const transformBy = new RemoveOperation(
  541. new Position( root, [ 0 ] ),
  542. 2,
  543. new Position( doc.graveyard, [ 0 ] ),
  544. 0
  545. );
  546. transformBy.targetPosition.path = [ 0 ];
  547. const transOp = transform.transform( op, transformBy );
  548. expect( transOp.length ).to.equal( 1 );
  549. expected.range.start.path = [ 4, 0 ];
  550. expected.range.end.path = [ 4, 4 ];
  551. expectOperation( transOp[ 0 ], expected );
  552. } );
  553. it( 'remove operation inserted elements after attribute operation range: do nothing', () => {
  554. const transformBy = new RemoveOperation(
  555. new Position( root, [ 0 ] ),
  556. 2,
  557. new Position( doc.graveyard, [ 0 ] ),
  558. 0
  559. );
  560. transformBy.targetPosition.path = [ 4 ];
  561. const transOp = transform.transform( op, transformBy );
  562. expect( transOp.length ).to.equal( 1 );
  563. expectOperation( transOp[ 0 ], expected );
  564. } );
  565. } );
  566. } );
  567. describe( 'RootAttributeOperation', () => {
  568. const diffRoot = new RootElement( null );
  569. beforeEach( () => {
  570. expected = {
  571. type: RootAttributeOperation,
  572. key: 'foo',
  573. oldValue: 'abc',
  574. newValue: 'bar'
  575. };
  576. op = new RootAttributeOperation( root, 'foo', 'abc', 'bar', 0 );
  577. } );
  578. describe( 'by InsertOperation', () => {
  579. it( 'no operation update', () => {
  580. const transformBy = new InsertOperation(
  581. new Position( root, [ 0 ] ),
  582. 'a',
  583. 0
  584. );
  585. const transOp = transform.transform( op, transformBy );
  586. expect( transOp.length ).to.equal( 1 );
  587. expectOperation( transOp[ 0 ], expected );
  588. } );
  589. } );
  590. describe( 'by AttributeOperation', () => {
  591. it( 'no operation update', () => {
  592. const transformBy = new AttributeOperation(
  593. new Range(
  594. new Position( root, [ 0 ] ),
  595. new Position( root, [ 1 ] )
  596. ),
  597. 'foo',
  598. 'bar',
  599. 'xyz',
  600. 0
  601. );
  602. const transOp = transform.transform( op, transformBy );
  603. expect( transOp.length ).to.equal( 1 );
  604. expectOperation( transOp[ 0 ], expected );
  605. } );
  606. } );
  607. describe( 'by RootAttributeOperation', () => {
  608. it( 'changes different root: no operation update', () => {
  609. const transformBy = new RootAttributeOperation(
  610. diffRoot,
  611. 'foo',
  612. 'abc',
  613. 'xyz',
  614. 0
  615. );
  616. const transOp = transform.transform( op, transformBy );
  617. expect( transOp.length ).to.equal( 1 );
  618. expectOperation( transOp[ 0 ], expected );
  619. } );
  620. it( 'changes different key: no operation update', () => {
  621. const transformBy = new RootAttributeOperation(
  622. root,
  623. 'abc',
  624. 'abc',
  625. 'xyz',
  626. 0
  627. );
  628. const transOp = transform.transform( op, transformBy );
  629. expect( transOp.length ).to.equal( 1 );
  630. expectOperation( transOp[ 0 ], expected );
  631. } );
  632. it( 'sets same value for same key: convert to NoOperation', () => {
  633. const transformBy = new RootAttributeOperation(
  634. root,
  635. 'foo',
  636. 'abc',
  637. 'bar',
  638. 0
  639. );
  640. const transOp = transform.transform( op, transformBy );
  641. expect( transOp.length ).to.equal( 1 );
  642. expectOperation( transOp[ 0 ], {
  643. type: NoOperation
  644. } );
  645. } );
  646. it( 'sets different value for same key on same root and is important: convert to NoOperation', () => {
  647. const transformBy = new RootAttributeOperation(
  648. root,
  649. 'foo',
  650. 'abc',
  651. 'xyz',
  652. 0
  653. );
  654. const transOp = transform.transform( op, transformBy );
  655. expect( transOp.length ).to.equal( 1 );
  656. expectOperation( transOp[ 0 ], {
  657. type: NoOperation
  658. } );
  659. } );
  660. it( 'sets different value for same key on same root and is less important: change oldValue', () => {
  661. const transformBy = new RootAttributeOperation(
  662. root,
  663. 'foo',
  664. 'abc',
  665. 'xyz',
  666. 0
  667. );
  668. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  669. expected.oldValue = 'xyz';
  670. expect( transOp.length ).to.equal( 1 );
  671. expectOperation( transOp[ 0 ], expected );
  672. } );
  673. } );
  674. describe( 'by MoveOperation', () => {
  675. it( 'no operation update', () => {
  676. const transformBy = new MoveOperation(
  677. new Position( root, [ 0 ] ),
  678. 2,
  679. new Position( root, [ 1 ] ),
  680. 0
  681. );
  682. const transOp = transform.transform( op, transformBy );
  683. expect( transOp.length ).to.equal( 1 );
  684. expectOperation( transOp[ 0 ], expected );
  685. } );
  686. } );
  687. describe( 'by NoOperation', () => {
  688. it( 'no operation update', () => {
  689. const transformBy = new NoOperation( 0 );
  690. const transOp = transform.transform( op, transformBy );
  691. expect( transOp.length ).to.equal( 1 );
  692. expectOperation( transOp[ 0 ], expected );
  693. } );
  694. } );
  695. describe( 'by RenameOperation', () => {
  696. it( 'no position update', () => {
  697. const transformBy = new RenameOperation( new Position( root, [ 0 ] ), 'oldName', 'newName', 0 );
  698. const transOp = transform.transform( op, transformBy );
  699. expect( transOp.length ).to.equal( 1 );
  700. expectOperation( transOp[ 0 ], expected );
  701. } );
  702. } );
  703. describe( 'by MarkerOperation', () => {
  704. it( 'no position update', () => {
  705. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  706. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  707. const transOp = transform.transform( op, transformBy );
  708. expect( transOp.length ).to.equal( 1 );
  709. expectOperation( transOp[ 0 ], expected );
  710. } );
  711. } );
  712. } );
  713. describe( 'MoveOperation', () => {
  714. let sourcePosition, targetPosition, rangeEnd, howMany;
  715. beforeEach( () => {
  716. sourcePosition = new Position( root, [ 2, 2, 4 ] );
  717. targetPosition = new Position( root, [ 3, 3, 3 ] );
  718. howMany = 2;
  719. rangeEnd = Position.createFromPosition( sourcePosition );
  720. rangeEnd.offset += howMany;
  721. op = new MoveOperation( sourcePosition, howMany, targetPosition, 0 );
  722. expected = {
  723. type: MoveOperation,
  724. sourcePosition: Position.createFromPosition( sourcePosition ),
  725. targetPosition: Position.createFromPosition( targetPosition ),
  726. howMany
  727. };
  728. } );
  729. describe( 'by InsertOperation', () => {
  730. it( 'target at different position than move range and target: no operation update', () => {
  731. const transformBy = new InsertOperation(
  732. new Position( root, [ 1, 3, 2 ] ),
  733. [ nodeA, nodeB ],
  734. 0
  735. );
  736. const transOp = transform.transform( op, transformBy );
  737. expect( transOp.length ).to.equal( 1 );
  738. expectOperation( transOp[ 0 ], expected );
  739. } );
  740. it( 'target inside node from move range: no operation update', () => {
  741. const transformBy = new InsertOperation(
  742. new Position( root, [ 2, 2, 4, 1 ] ),
  743. [ nodeA, nodeB ],
  744. 0
  745. );
  746. const transOp = transform.transform( op, transformBy );
  747. expect( transOp.length ).to.equal( 1 );
  748. expectOperation( transOp[ 0 ], expected );
  749. } );
  750. it( 'target at offset before range offset: increment range offset', () => {
  751. const transformBy = new InsertOperation(
  752. new Position( root, [ 2, 2, 0 ] ),
  753. [ nodeA, nodeB ],
  754. 0
  755. );
  756. const transOp = transform.transform( op, transformBy );
  757. expected.sourcePosition.offset += 2;
  758. expect( transOp.length ).to.equal( 1 );
  759. expectOperation( transOp[ 0 ], expected );
  760. } );
  761. it( 'target at offset after range offset: no operation update', () => {
  762. const transformBy = new InsertOperation(
  763. new Position( root, [ 2, 2, 7 ] ),
  764. [ nodeA, nodeB ],
  765. 0
  766. );
  767. const transOp = transform.transform( op, transformBy );
  768. expect( transOp.length ).to.equal( 1 );
  769. expectOperation( transOp[ 0 ], expected );
  770. } );
  771. it( 'target before node from range start path: increment index on range start path', () => {
  772. const transformBy = new InsertOperation(
  773. new Position( root, [ 2, 0 ] ),
  774. [ nodeA, nodeB ],
  775. 0
  776. );
  777. const transOp = transform.transform( op, transformBy );
  778. expected.sourcePosition.path[ 1 ] += 2;
  779. expect( transOp.length ).to.equal( 1 );
  780. expectOperation( transOp[ 0 ], expected );
  781. } );
  782. it( 'target after node from range start path: no operation update', () => {
  783. const transformBy = new InsertOperation(
  784. new Position( root, [ 2, 3 ] ),
  785. [ nodeA, nodeB ],
  786. 0
  787. );
  788. const transOp = transform.transform( op, transformBy );
  789. expect( transOp.length ).to.equal( 1 );
  790. expectOperation( transOp[ 0 ], expected );
  791. } );
  792. it( 'target offset before move target offset: increment target offset', () => {
  793. const transformBy = new InsertOperation(
  794. new Position( root, [ 3, 3, 2 ] ),
  795. [ nodeA, nodeB ],
  796. 0
  797. );
  798. const transOp = transform.transform( op, transformBy );
  799. expected.targetPosition.offset += 2;
  800. expect( transOp.length ).to.equal( 1 );
  801. expectOperation( transOp[ 0 ], expected );
  802. } );
  803. it( 'target offset after move target offset: no operation update', () => {
  804. const transformBy = new InsertOperation(
  805. new Position( root, [ 3, 3, 4 ] ),
  806. [ nodeA, nodeB ],
  807. 0
  808. );
  809. const transOp = transform.transform( op, transformBy );
  810. expect( transOp.length ).to.equal( 1 );
  811. expectOperation( transOp[ 0 ], expected );
  812. } );
  813. it( 'target before node from move target position path: increment index on move target position path', () => {
  814. const transformBy = new InsertOperation(
  815. new Position( root, [ 3, 3 ] ),
  816. [ nodeA, nodeB ],
  817. 0
  818. );
  819. const transOp = transform.transform( op, transformBy );
  820. expected.targetPosition.path[ 1 ] += 2;
  821. expect( transOp.length ).to.equal( 1 );
  822. expectOperation( transOp[ 0 ], expected );
  823. } );
  824. it( 'target after node from move target position path: no operation update', () => {
  825. const transformBy = new InsertOperation(
  826. new Position( root, [ 3, 6 ] ),
  827. [ nodeA, nodeB ],
  828. 0
  829. );
  830. const transOp = transform.transform( op, transformBy );
  831. expect( transOp.length ).to.equal( 1 );
  832. expectOperation( transOp[ 0 ], expected );
  833. } );
  834. it( 'target offset same as move target offset and is important: no operation update', () => {
  835. const transformBy = new InsertOperation(
  836. new Position( root, [ 3, 3, 3 ] ),
  837. [ nodeA, nodeB ],
  838. 0
  839. );
  840. const transOp = transform.transform( op, transformBy );
  841. expect( transOp.length ).to.equal( 1 );
  842. expectOperation( transOp[ 0 ], expected );
  843. } );
  844. it( 'target offset same as move target offset and is less important: no operation update', () => {
  845. const transformBy = new InsertOperation(
  846. new Position( root, [ 3, 3, 3 ] ),
  847. [ nodeA, nodeB ],
  848. 0
  849. );
  850. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  851. expect( transOp.length ).to.equal( 1 );
  852. expectOperation( transOp[ 0 ], expected );
  853. } );
  854. it( 'target inside move range: expand range', () => {
  855. const transformBy = new InsertOperation(
  856. new Position( root, [ 2, 2, 5 ] ),
  857. [ nodeA, nodeB ],
  858. 0
  859. );
  860. const transOp = transform.transform( op, transformBy );
  861. expect( transOp.length ).to.equal( 1 );
  862. expected.howMany = 4;
  863. expectOperation( transOp[ 0 ], expected );
  864. } );
  865. it( 'target at offset same as range end boundary: no operation update', () => {
  866. const transformBy = new InsertOperation(
  867. new Position( root, [ 2, 2, 6 ] ),
  868. [ nodeA, nodeB ],
  869. 0
  870. );
  871. const transOp = transform.transform( op, transformBy );
  872. expect( transOp.length ).to.equal( 1 );
  873. expectOperation( transOp[ 0 ], expected );
  874. } );
  875. } );
  876. describe( 'by AttributeOperation', () => {
  877. it( 'no operation update', () => {
  878. const transformBy = new AttributeOperation(
  879. new Range( sourcePosition, rangeEnd ),
  880. 'abc',
  881. true,
  882. false,
  883. 0
  884. );
  885. const transOp = transform.transform( op, transformBy );
  886. expect( transOp.length ).to.equal( 1 );
  887. expectOperation( transOp[ 0 ], expected );
  888. } );
  889. } );
  890. describe( 'by RootAttributeOperation', () => {
  891. it( 'no operation update', () => {
  892. const transformBy = new RootAttributeOperation(
  893. root,
  894. 'foo',
  895. null,
  896. 'bar',
  897. 0
  898. );
  899. const transOp = transform.transform( op, transformBy );
  900. expect( transOp.length ).to.equal( 1 );
  901. expectOperation( transOp[ 0 ], expected );
  902. } );
  903. } );
  904. describe( 'by MoveOperation', () => {
  905. it( 'range and target different than transforming range and target: no operation update', () => {
  906. const transformBy = new MoveOperation(
  907. new Position( root, [ 1, 2 ] ),
  908. 3,
  909. new Position( root, [ 4, 1, 0 ] ),
  910. 0
  911. );
  912. const transOp = transform.transform( op, transformBy );
  913. expect( transOp.length ).to.equal( 1 );
  914. expectOperation( transOp[ 0 ], expected );
  915. } );
  916. it( 'target offset before transforming range start offset: increment range offset', () => {
  917. const transformBy = new MoveOperation(
  918. new Position( root, [ 4, 1, 0 ] ),
  919. 2,
  920. new Position( root, [ 2, 2, 0 ] ),
  921. 0
  922. );
  923. const transOp = transform.transform( op, transformBy );
  924. expected.sourcePosition.offset += 2;
  925. expect( transOp.length ).to.equal( 1 );
  926. expectOperation( transOp[ 0 ], expected );
  927. } );
  928. it( 'target offset after transforming range start offset: no operation update', () => {
  929. const transformBy = new MoveOperation(
  930. new Position( root, [ 4, 1, 0 ] ),
  931. 2,
  932. new Position( root, [ 2, 2, 7 ] ),
  933. 0
  934. );
  935. const transOp = transform.transform( op, transformBy );
  936. expect( transOp.length ).to.equal( 1 );
  937. expectOperation( transOp[ 0 ], expected );
  938. } );
  939. it( 'range start offset before transforming range start offset: decrement range offset', () => {
  940. const transformBy = new MoveOperation(
  941. new Position( root, [ 2, 2, 0 ] ),
  942. 2,
  943. new Position( root, [ 4, 1, 0 ] ),
  944. 0
  945. );
  946. const transOp = transform.transform( op, transformBy );
  947. expected.sourcePosition.offset -= 2;
  948. expect( transOp.length ).to.equal( 1 );
  949. expectOperation( transOp[ 0 ], expected );
  950. } );
  951. it( 'range start offset after transforming range start offset: no operation update', () => {
  952. const transformBy = new MoveOperation(
  953. new Position( root, [ 2, 2, 9 ] ),
  954. 2,
  955. new Position( root, [ 4, 1, 0 ] ),
  956. 0
  957. );
  958. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  959. expect( transOp.length ).to.equal( 1 );
  960. expectOperation( transOp[ 0 ], expected );
  961. } );
  962. it( 'target before node from transforming range start path: increment index on range start path', () => {
  963. const transformBy = new MoveOperation(
  964. new Position( root, [ 4, 1, 0 ] ),
  965. 2,
  966. new Position( root, [ 2, 1 ] ),
  967. 0
  968. );
  969. const transOp = transform.transform( op, transformBy );
  970. expected.sourcePosition.path[ 1 ] += 2;
  971. expect( transOp.length ).to.equal( 1 );
  972. expectOperation( transOp[ 0 ], expected );
  973. } );
  974. it( 'target after node from transforming range start path: no operation update', () => {
  975. const transformBy = new MoveOperation(
  976. new Position( root, [ 4, 1, 0 ] ),
  977. 2,
  978. new Position( root, [ 2, 5 ] ),
  979. 0
  980. );
  981. const transOp = transform.transform( op, transformBy );
  982. expect( transOp.length ).to.equal( 1 );
  983. expectOperation( transOp[ 0 ], expected );
  984. } );
  985. it( 'range before node from transforming range start path: decrement index on range start path', () => {
  986. const transformBy = new MoveOperation(
  987. new Position( root, [ 2, 0 ] ),
  988. 1,
  989. new Position( root, [ 4, 1, 0 ] ),
  990. 0
  991. );
  992. const transOp = transform.transform( op, transformBy );
  993. expected.sourcePosition.path[ 1 ] -= 1;
  994. expect( transOp.length ).to.equal( 1 );
  995. expectOperation( transOp[ 0 ], expected );
  996. } );
  997. it( 'range after node from transforming range start path: no operation update', () => {
  998. const transformBy = new MoveOperation(
  999. new Position( root, [ 2, 3 ] ),
  1000. 2,
  1001. new Position( root, [ 4, 1, 0 ] ),
  1002. 0
  1003. );
  1004. const transOp = transform.transform( op, transformBy );
  1005. expect( transOp.length ).to.equal( 1 );
  1006. expectOperation( transOp[ 0 ], expected );
  1007. } );
  1008. it( 'target offset before transforming target offset: increment target offset', () => {
  1009. const transformBy = new MoveOperation(
  1010. new Position( root, [ 4, 1, 0 ] ),
  1011. 2,
  1012. new Position( root, [ 3, 3, 0 ] ),
  1013. 0
  1014. );
  1015. const transOp = transform.transform( op, transformBy );
  1016. expected.targetPosition.offset += 2;
  1017. expect( transOp.length ).to.equal( 1 );
  1018. expectOperation( transOp[ 0 ], expected );
  1019. } );
  1020. it( 'target offset after transforming target offset: no operation update', () => {
  1021. const transformBy = new MoveOperation(
  1022. new Position( root, [ 4, 1, 0 ] ),
  1023. 2,
  1024. new Position( root, [ 3, 3, 5 ] ),
  1025. 0
  1026. );
  1027. const transOp = transform.transform( op, transformBy );
  1028. expect( transOp.length ).to.equal( 1 );
  1029. expectOperation( transOp[ 0 ], expected );
  1030. } );
  1031. it( 'range offset before transforming target offset: decrement target offset', () => {
  1032. const transformBy = new MoveOperation(
  1033. new Position( root, [ 3, 3, 0 ] ),
  1034. 2,
  1035. new Position( root, [ 4, 1, 0 ] ),
  1036. 0
  1037. );
  1038. const transOp = transform.transform( op, transformBy );
  1039. expected.targetPosition.offset -= 2;
  1040. expect( transOp.length ).to.equal( 1 );
  1041. expectOperation( transOp[ 0 ], expected );
  1042. } );
  1043. it( 'range offset after transforming target offset: no operation update', () => {
  1044. const transformBy = new MoveOperation(
  1045. new Position( root, [ 3, 3, 5 ] ),
  1046. 2,
  1047. new Position( root, [ 4, 1, 0 ] ),
  1048. 0
  1049. );
  1050. const transOp = transform.transform( op, transformBy );
  1051. expect( transOp.length ).to.equal( 1 );
  1052. expectOperation( transOp[ 0 ], expected );
  1053. } );
  1054. it( 'target before node from transforming target path: increment index on target path', () => {
  1055. const transformBy = new MoveOperation(
  1056. new Position( root, [ 4, 1, 0 ] ),
  1057. 2,
  1058. new Position( root, [ 3, 1 ] ),
  1059. 0
  1060. );
  1061. const transOp = transform.transform( op, transformBy );
  1062. expected.targetPosition.path[ 1 ] += 2;
  1063. expect( transOp.length ).to.equal( 1 );
  1064. expectOperation( transOp[ 0 ], expected );
  1065. } );
  1066. it( 'target after node from transforming target path: no operation update', () => {
  1067. const transformBy = new MoveOperation(
  1068. new Position( root, [ 4, 1, 0 ] ),
  1069. 2,
  1070. new Position( root, [ 3, 5 ] ),
  1071. 0
  1072. );
  1073. const transOp = transform.transform( op, transformBy );
  1074. expect( transOp.length ).to.equal( 1 );
  1075. expectOperation( transOp[ 0 ], expected );
  1076. } );
  1077. it( 'range before node from transforming target path: decrement index on target path', () => {
  1078. const transformBy = new MoveOperation(
  1079. new Position( root, [ 3, 0 ] ),
  1080. 2,
  1081. new Position( root, [ 4, 1, 0 ] ),
  1082. 0
  1083. );
  1084. const transOp = transform.transform( op, transformBy );
  1085. expected.targetPosition.path[ 1 ] -= 2;
  1086. expect( transOp.length ).to.equal( 1 );
  1087. expectOperation( transOp[ 0 ], expected );
  1088. } );
  1089. it( 'range after node from transforming target path: no operation update', () => {
  1090. const transformBy = new MoveOperation(
  1091. new Position( root, [ 3, 5 ] ),
  1092. 2,
  1093. new Position( root, [ 4, 1, 0 ] ),
  1094. 0
  1095. );
  1096. const transOp = transform.transform( op, transformBy );
  1097. expect( transOp.length ).to.equal( 1 );
  1098. expectOperation( transOp[ 0 ], expected );
  1099. } );
  1100. it( 'target inside transforming move range: split into two operations', () => {
  1101. const transformBy = new MoveOperation(
  1102. new Position( root, [ 4, 1, 0 ] ),
  1103. 2,
  1104. new Position( root, [ 2, 2, 5 ] ),
  1105. 0
  1106. );
  1107. const transOp = transform.transform( op, transformBy );
  1108. expect( transOp.length ).to.equal( 2 );
  1109. expected.howMany = 1;
  1110. expectOperation( transOp[ 0 ], expected );
  1111. expected.sourcePosition.path = [ 2, 2, 6 ];
  1112. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1113. expectOperation( transOp[ 1 ], expected );
  1114. } );
  1115. it( 'target at start boundary of transforming move range: increment source offset', () => {
  1116. const transformBy = new MoveOperation(
  1117. new Position( root, [ 4, 1, 0 ] ),
  1118. 2,
  1119. new Position( root, [ 2, 2, 4 ] ),
  1120. 0
  1121. );
  1122. const transOp = transform.transform( op, transformBy );
  1123. expect( transOp.length ).to.equal( 1 );
  1124. expected.sourcePosition.offset = 6;
  1125. expectOperation( transOp[ 0 ], expected );
  1126. } );
  1127. it( 'target at end boundary of transforming move range: no operation update', () => {
  1128. const transformBy = new MoveOperation(
  1129. new Position( root, [ 4, 1, 0 ] ),
  1130. 2,
  1131. new Position( root, [ 2, 2, 6 ] ),
  1132. 0
  1133. );
  1134. const transOp = transform.transform( op, transformBy );
  1135. expect( transOp.length ).to.equal( 1 );
  1136. expectOperation( transOp[ 0 ], expected );
  1137. } );
  1138. it( 'target inside a node from transforming range: no operation update', () => {
  1139. const transformBy = new MoveOperation(
  1140. new Position( root, [ 4, 1, 0 ] ),
  1141. 2,
  1142. new Position( root, [ 2, 2, 5, 1 ] ),
  1143. 0
  1144. );
  1145. const transOp = transform.transform( op, transformBy );
  1146. expect( transOp.length ).to.equal( 1 );
  1147. expectOperation( transOp[ 0 ], expected );
  1148. } );
  1149. it( 'range has node that contains transforming range: update range path', () => {
  1150. const transformBy = new MoveOperation(
  1151. new Position( root, [ 2, 1 ] ),
  1152. 3,
  1153. new Position( root, [ 4, 2 ] ),
  1154. 0
  1155. );
  1156. const transOp = transform.transform( op, transformBy );
  1157. expected.sourcePosition.path = [ 4, 3, 4 ];
  1158. expect( transOp.length ).to.equal( 1 );
  1159. expectOperation( transOp[ 0 ], expected );
  1160. } );
  1161. it( 'range has node that contains transforming target: update target path', () => {
  1162. const transformBy = new MoveOperation(
  1163. new Position( root, [ 3, 2 ] ),
  1164. 3,
  1165. new Position( root, [ 0, 1 ] ),
  1166. 0
  1167. );
  1168. const transOp = transform.transform( op, transformBy );
  1169. expected.targetPosition.path = [ 0, 2, 3 ];
  1170. expect( transOp.length ).to.equal( 1 );
  1171. expectOperation( transOp[ 0 ], expected );
  1172. } );
  1173. it( 'target inside a node from transforming range and vice versa: reverse transform-by operation', () => {
  1174. const transformBy = new MoveOperation(
  1175. new Position( root, [ 3, 2 ] ),
  1176. 3,
  1177. new Position( root, [ 2, 2, 5, 0 ] ),
  1178. 0
  1179. );
  1180. const transOp = transform.transform( op, transformBy );
  1181. const reversed = transformBy.getReversed();
  1182. expected.sourcePosition = reversed.sourcePosition;
  1183. expected.targetPosition = reversed.targetPosition;
  1184. expected.howMany = reversed.howMany;
  1185. expect( transOp.length ).to.equal( 1 );
  1186. expectOperation( transOp[ 0 ], expected );
  1187. } );
  1188. it( 'range is same as transforming range and is important: convert to NoOperation', () => {
  1189. const transformBy = new MoveOperation(
  1190. op.sourcePosition,
  1191. op.howMany,
  1192. new Position( root, [ 4, 1, 0 ] ),
  1193. 0
  1194. );
  1195. const transOp = transform.transform( op, transformBy );
  1196. expect( transOp.length ).to.equal( 1 );
  1197. expectOperation( transOp[ 0 ], {
  1198. type: NoOperation
  1199. } );
  1200. } );
  1201. it( 'range is same as transforming range and is less important: update range path', () => {
  1202. const transformBy = new MoveOperation(
  1203. op.sourcePosition,
  1204. op.howMany,
  1205. new Position( root, [ 4, 1, 0 ] ),
  1206. 0
  1207. );
  1208. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1209. expected.sourcePosition.path = [ 4, 1, 0 ];
  1210. expect( transOp.length ).to.equal( 1 );
  1211. expectOperation( transOp[ 0 ], expected );
  1212. } );
  1213. it( 'range contains transforming range and is important: convert to NoOperation', () => {
  1214. const transformBy = new MoveOperation(
  1215. new Position( root, [ 2, 2, 3 ] ),
  1216. 4,
  1217. new Position( root, [ 4, 1, 0 ] ),
  1218. 0
  1219. );
  1220. const transOp = transform.transform( op, transformBy );
  1221. expect( transOp.length ).to.equal( 1 );
  1222. expectOperation( transOp[ 0 ], {
  1223. type: NoOperation
  1224. } );
  1225. } );
  1226. it( 'range contains transforming range and is less important: update range path', () => {
  1227. const transformBy = new MoveOperation(
  1228. new Position( root, [ 2, 2, 3 ] ),
  1229. 4,
  1230. new Position( root, [ 4, 1, 0 ] ),
  1231. 0
  1232. );
  1233. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1234. expected.sourcePosition.path = [ 4, 1, 1 ];
  1235. expect( transOp.length ).to.equal( 1 );
  1236. expectOperation( transOp[ 0 ], expected );
  1237. } );
  1238. it( 'range contains transforming range and target and is important: update range path and target', () => {
  1239. op.targetPosition.path = [ 2, 2, 7 ];
  1240. const transformBy = new MoveOperation(
  1241. new Position( root, [ 2, 2, 3 ] ),
  1242. 5,
  1243. new Position( root, [ 4, 1, 0 ] ),
  1244. 0
  1245. );
  1246. const transOp = transform.transform( op, transformBy );
  1247. expect( transOp.length ).to.equal( 1 );
  1248. expected.sourcePosition.path = [ 4, 1, 1 ];
  1249. expected.targetPosition.path = [ 4, 1, 4 ];
  1250. expectOperation( transOp[ 0 ], expected );
  1251. } );
  1252. it( 'range contains transforming range and target and is less important: update range path and target', () => {
  1253. op.targetPosition.path = [ 2, 2, 7 ];
  1254. const transformBy = new MoveOperation(
  1255. new Position( root, [ 2, 2, 3 ] ),
  1256. 5,
  1257. new Position( root, [ 4, 1, 0 ] ),
  1258. 0
  1259. );
  1260. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1261. expect( transOp.length ).to.equal( 1 );
  1262. expected.sourcePosition.path = [ 4, 1, 1 ];
  1263. expected.targetPosition.path = [ 4, 1, 4 ];
  1264. expectOperation( transOp[ 0 ], expected );
  1265. } );
  1266. it( 'range intersects on left side of transforming range and is important: shrink range', () => {
  1267. const transformBy = new MoveOperation(
  1268. new Position( root, [ 2, 2, 3 ] ),
  1269. 2,
  1270. new Position( root, [ 4, 1, 0 ] ),
  1271. 0
  1272. );
  1273. const transOp = transform.transform( op, transformBy );
  1274. expected.sourcePosition.path = [ 2, 2, 3 ];
  1275. expected.howMany = 1;
  1276. expect( transOp.length ).to.equal( 1 );
  1277. expectOperation( transOp[ 0 ], expected );
  1278. } );
  1279. it( 'range intersects on left side of transforming range and is less important: split into two operations', () => {
  1280. // Get more test cases and better code coverage
  1281. const otherRoot = new RootElement( null );
  1282. const transformBy = new MoveOperation(
  1283. new Position( root, [ 2, 2, 3 ] ),
  1284. 2,
  1285. new Position( otherRoot, [ 4, 1, 0 ] ),
  1286. 0
  1287. );
  1288. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1289. expect( transOp.length ).to.equal( 2 );
  1290. expected.sourcePosition = new Position( otherRoot, [ 4, 1, 1 ] );
  1291. expected.howMany = 1;
  1292. expectOperation( transOp[ 0 ], expected );
  1293. expected.sourcePosition = new Position( root, [ 2, 2, 3 ] );
  1294. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1295. expectOperation( transOp[ 1 ], expected );
  1296. } );
  1297. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1298. const transformBy = new MoveOperation(
  1299. new Position( root, [ 2, 2, 5 ] ),
  1300. 2,
  1301. new Position( root, [ 4, 1, 0 ] ),
  1302. 0
  1303. );
  1304. const transOp = transform.transform( op, transformBy );
  1305. expected.howMany = 1;
  1306. expect( transOp.length ).to.equal( 1 );
  1307. expectOperation( transOp[ 0 ], expected );
  1308. } );
  1309. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1310. const transformBy = new MoveOperation(
  1311. new Position( root, [ 2, 2, 5 ] ),
  1312. 2,
  1313. new Position( root, [ 4, 1, 0 ] ),
  1314. 0
  1315. );
  1316. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1317. expect( transOp.length ).to.equal( 2 );
  1318. expected.sourcePosition = sourcePosition;
  1319. expected.howMany = 1;
  1320. expectOperation( transOp[ 0 ], expected );
  1321. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1322. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1323. expectOperation( transOp[ 1 ], expected );
  1324. } );
  1325. it( 'range intersects on left side, target inside transforming range and is important: split into two operations', () => {
  1326. op.howMany = 4;
  1327. const transformBy = new MoveOperation(
  1328. new Position( root, [ 2, 2, 3 ] ),
  1329. 2,
  1330. new Position( root, [ 2, 2, 6 ] ),
  1331. 0
  1332. );
  1333. const transOp = transform.transform( op, transformBy );
  1334. expect( transOp.length ).to.equal( 2 );
  1335. expected.sourcePosition.path = [ 2, 2, 3 ];
  1336. expected.howMany = 1;
  1337. expectOperation( transOp[ 0 ], expected );
  1338. expected.sourcePosition.path = [ 2, 2, 5 ];
  1339. expected.howMany = 2;
  1340. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1341. expectOperation( transOp[ 1 ], expected );
  1342. } );
  1343. it( 'range intersects on left side, target inside transforming range and is less important: split into two operations', () => {
  1344. op.howMany = 4;
  1345. const transformBy = new MoveOperation(
  1346. new Position( root, [ 2, 2, 3 ] ),
  1347. 2,
  1348. new Position( root, [ 2, 2, 6 ] ),
  1349. 0
  1350. );
  1351. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1352. expect( transOp.length ).to.equal( 2 );
  1353. expected.sourcePosition.path = [ 2, 2, 3 ];
  1354. expected.howMany = 1;
  1355. expectOperation( transOp[ 0 ], expected );
  1356. expected.sourcePosition.path = [ 2, 2, 5 ];
  1357. expected.howMany = 2;
  1358. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1359. expectOperation( transOp[ 1 ], expected );
  1360. } );
  1361. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1362. const transformBy = new MoveOperation(
  1363. new Position( root, [ 2, 2, 5 ] ),
  1364. 2,
  1365. new Position( root, [ 4, 1, 0 ] ),
  1366. 0
  1367. );
  1368. const transOp = transform.transform( op, transformBy );
  1369. expected.howMany = 1;
  1370. expect( transOp.length ).to.equal( 1 );
  1371. expectOperation( transOp[ 0 ], expected );
  1372. } );
  1373. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1374. const transformBy = new MoveOperation(
  1375. new Position( root, [ 2, 2, 5 ] ),
  1376. 2,
  1377. new Position( root, [ 4, 1, 0 ] ),
  1378. 0
  1379. );
  1380. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1381. expect( transOp.length ).to.equal( 2 );
  1382. expected.sourcePosition = sourcePosition;
  1383. expected.howMany = 1;
  1384. expectOperation( transOp[ 0 ], expected );
  1385. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1386. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1387. expectOperation( transOp[ 1 ], expected );
  1388. } );
  1389. it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
  1390. op.targetPosition.path = [ 2, 2, 7 ];
  1391. const transformBy = new MoveOperation(
  1392. new Position( root, [ 2, 2, 5 ] ),
  1393. 4,
  1394. new Position( root, [ 4, 1, 0 ] ),
  1395. 0
  1396. );
  1397. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1398. expect( transOp.length ).to.equal( 2 );
  1399. expected.sourcePosition.path = [ 2, 2, 4 ];
  1400. expected.targetPosition.path = [ 4, 1, 2 ];
  1401. expected.howMany = 1;
  1402. expectOperation( transOp[ 0 ], expected );
  1403. expected.sourcePosition.path = [ 4, 1, 0 ];
  1404. expected.targetPosition.path = [ 4, 1, 3 ];
  1405. expectOperation( transOp[ 1 ], expected );
  1406. } );
  1407. it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
  1408. op.targetPosition.path = [ 2, 2, 7 ];
  1409. const transformBy = new MoveOperation(
  1410. new Position( root, [ 2, 2, 5 ] ),
  1411. 4,
  1412. new Position( root, [ 4, 1, 0 ] ),
  1413. 0
  1414. );
  1415. const transOp = transform.transform( op, transformBy );
  1416. expect( transOp.length ).to.equal( 1 );
  1417. expected.sourcePosition.path = [ 2, 2, 4 ];
  1418. expected.targetPosition.path = [ 4, 1, 2 ];
  1419. expected.howMany = 1;
  1420. expectOperation( transOp[ 0 ], expected );
  1421. } );
  1422. it( 'range inside transforming range and is important: split into two operations', () => {
  1423. op.howMany = 4;
  1424. const transformBy = new MoveOperation(
  1425. new Position( root, [ 2, 2, 5 ] ),
  1426. 2,
  1427. new Position( root, [ 4, 1, 0 ] ),
  1428. 0
  1429. );
  1430. const transOp = transform.transform( op, transformBy );
  1431. expect( transOp.length ).to.equal( 2 );
  1432. expected.sourcePosition.path = [ 2, 2, 4 ];
  1433. expected.howMany = 1;
  1434. expectOperation( transOp[ 0 ], expected );
  1435. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1436. expectOperation( transOp[ 1 ], expected );
  1437. } );
  1438. it( 'range inside transforming range and is less important: split into three operations', () => {
  1439. op.howMany = 4;
  1440. const transformBy = new MoveOperation(
  1441. new Position( root, [ 2, 2, 5 ] ),
  1442. 2,
  1443. new Position( root, [ 4, 1, 0 ] ),
  1444. 0
  1445. );
  1446. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1447. expect( transOp.length ).to.equal( 3 );
  1448. expected.sourcePosition.path = [ 2, 2, 4 ];
  1449. expected.howMany = 1;
  1450. expectOperation( transOp[ 0 ], expected );
  1451. expected.sourcePosition.path = [ 4, 1, 0 ];
  1452. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1453. expected.howMany = 2;
  1454. expectOperation( transOp[ 1 ], expected );
  1455. expected.sourcePosition.path = [ 2, 2, 4 ];
  1456. expected.targetPosition = targetPosition.getShiftedBy( 3 );
  1457. expected.howMany = 1;
  1458. expectOperation( transOp[ 2 ], expected );
  1459. } );
  1460. it( 'range and target inside transforming range and is important: no operation update', () => {
  1461. op.howMany = 6;
  1462. const transformBy = new MoveOperation(
  1463. new Position( root, [ 2, 2, 5 ] ),
  1464. 2,
  1465. new Position( root, [ 2, 2, 9 ] ),
  1466. 0
  1467. );
  1468. const transOp = transform.transform( op, transformBy );
  1469. expect( transOp.length ).to.equal( 1 );
  1470. expected.howMany = 6;
  1471. expectOperation( transOp[ 0 ], expected );
  1472. } );
  1473. it( 'range and target inside transforming range and is less important: no operation update', () => {
  1474. op.howMany = 6;
  1475. const transformBy = new MoveOperation(
  1476. new Position( root, [ 2, 2, 5 ] ),
  1477. 2,
  1478. new Position( root, [ 2, 2, 9 ] ),
  1479. 0
  1480. );
  1481. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1482. expect( transOp.length ).to.equal( 1 );
  1483. expected.howMany = 6;
  1484. expectOperation( transOp[ 0 ], expected );
  1485. } );
  1486. } );
  1487. describe( 'by RemoveOperation', () => {
  1488. let transformBy;
  1489. beforeEach( () => {
  1490. transformBy = new RemoveOperation(
  1491. Position.createFromPosition( op.sourcePosition ),
  1492. op.howMany,
  1493. new Position( doc.graveyard, [ 0 ] ),
  1494. 0
  1495. );
  1496. } );
  1497. it( 'should skip context.aIsStrong and be less important than RemoveOperation', () => {
  1498. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1499. expect( transOp.length ).to.equal( 1 );
  1500. expectOperation( transOp[ 0 ], {
  1501. type: NoOperation
  1502. } );
  1503. } );
  1504. } );
  1505. describe( 'by NoOperation', () => {
  1506. it( 'no operation update', () => {
  1507. const transformBy = new NoOperation( 0 );
  1508. const transOp = transform.transform( op, transformBy );
  1509. expect( transOp.length ).to.equal( 1 );
  1510. expectOperation( transOp[ 0 ], expected );
  1511. } );
  1512. } );
  1513. describe( 'by RenameOperation', () => {
  1514. it( 'no position update', () => {
  1515. const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', 0 );
  1516. const transOp = transform.transform( op, transformBy );
  1517. expect( transOp.length ).to.equal( 1 );
  1518. expectOperation( transOp[ 0 ], expected );
  1519. } );
  1520. } );
  1521. describe( 'by MarkerOperation', () => {
  1522. it( 'no position update', () => {
  1523. const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
  1524. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1525. const transOp = transform.transform( op, transformBy );
  1526. expect( transOp.length ).to.equal( 1 );
  1527. expectOperation( transOp[ 0 ], expected );
  1528. } );
  1529. } );
  1530. } );
  1531. describe( 'RemoveOperation', () => {
  1532. describe( 'by MoveOperation', () => {
  1533. it( 'should force removing content even if was less important', () => {
  1534. const op = new RemoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  1535. const targetPosition = Position.createFromPosition( op.targetPosition );
  1536. const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), 0 );
  1537. const sourcePosition = Position.createFromPosition( transformBy.targetPosition );
  1538. const transOp = transform.transform( op, transformBy );
  1539. expect( transOp.length ).to.equal( 1 );
  1540. expectOperation( transOp[ 0 ], {
  1541. type: RemoveOperation,
  1542. howMany: 2,
  1543. sourcePosition,
  1544. targetPosition
  1545. } );
  1546. } );
  1547. } );
  1548. } );
  1549. describe( 'NoOperation', () => {
  1550. beforeEach( () => {
  1551. op = new NoOperation( 0 );
  1552. expected = {
  1553. type: NoOperation
  1554. };
  1555. } );
  1556. describe( 'by InsertOperation', () => {
  1557. it( 'no operation update', () => {
  1558. const transformBy = new InsertOperation(
  1559. new Position( root, [ 0 ] ),
  1560. 'a',
  1561. 0
  1562. );
  1563. const transOp = transform.transform( op, transformBy );
  1564. expect( transOp.length ).to.equal( 1 );
  1565. expectOperation( transOp[ 0 ], expected );
  1566. } );
  1567. } );
  1568. describe( 'by AttributeOperation', () => {
  1569. it( 'no operation update', () => {
  1570. const transformBy = new AttributeOperation(
  1571. new Range(
  1572. new Position( root, [ 0 ] ),
  1573. new Position( root, [ 1 ] )
  1574. ),
  1575. 'foo',
  1576. 'bar',
  1577. 'xyz',
  1578. 0
  1579. );
  1580. const transOp = transform.transform( op, transformBy );
  1581. expect( transOp.length ).to.equal( 1 );
  1582. expectOperation( transOp[ 0 ], expected );
  1583. } );
  1584. } );
  1585. describe( 'by RootAttributeOperation', () => {
  1586. it( 'no operation update', () => {
  1587. const transformBy = new RootAttributeOperation(
  1588. root,
  1589. 'foo',
  1590. null,
  1591. 'bar',
  1592. 0
  1593. );
  1594. const transOp = transform.transform( op, transformBy );
  1595. expect( transOp.length ).to.equal( 1 );
  1596. expectOperation( transOp[ 0 ], expected );
  1597. } );
  1598. } );
  1599. describe( 'by MoveOperation', () => {
  1600. it( 'no operation update', () => {
  1601. const transformBy = new MoveOperation(
  1602. new Position( root, [ 0 ] ),
  1603. 2,
  1604. new Position( root, [ 1 ] ),
  1605. 0
  1606. );
  1607. const transOp = transform.transform( op, transformBy );
  1608. expect( transOp.length ).to.equal( 1 );
  1609. expectOperation( transOp[ 0 ], expected );
  1610. } );
  1611. } );
  1612. describe( 'by NoOperation', () => {
  1613. it( 'no operation update', () => {
  1614. const transformBy = new NoOperation( 0 );
  1615. const transOp = transform.transform( op, transformBy );
  1616. expect( transOp.length ).to.equal( 1 );
  1617. expectOperation( transOp[ 0 ], expected );
  1618. } );
  1619. } );
  1620. describe( 'by RenameOperation', () => {
  1621. it( 'no position update', () => {
  1622. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  1623. const transOp = transform.transform( op, transformBy );
  1624. expect( transOp.length ).to.equal( 1 );
  1625. expectOperation( transOp[ 0 ], expected );
  1626. } );
  1627. } );
  1628. describe( 'by MarkerOperation', () => {
  1629. it( 'no position update', () => {
  1630. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1631. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1632. const transOp = transform.transform( op, transformBy );
  1633. expect( transOp.length ).to.equal( 1 );
  1634. expectOperation( transOp[ 0 ], expected );
  1635. } );
  1636. } );
  1637. } );
  1638. describe( 'RenameOperation', () => {
  1639. beforeEach( () => {
  1640. const position = new Position( root, [ 0, 2, 2 ] );
  1641. op = new RenameOperation( position, 'oldName', 'newName', 0 );
  1642. expected = {
  1643. position,
  1644. oldName: 'oldName',
  1645. newName: 'newName'
  1646. };
  1647. } );
  1648. describe( 'by InsertOperation', () => {
  1649. it( 'target before renamed element: offset update', () => {
  1650. const transformBy = new InsertOperation(
  1651. new Position( root, [ 0, 2, 1 ] ),
  1652. [ nodeA, nodeB ],
  1653. 0
  1654. );
  1655. const transOp = transform.transform( op, transformBy );
  1656. expect( transOp.length ).to.equal( 1 );
  1657. expected.position.offset = 4;
  1658. expectOperation( transOp[ 0 ], expected );
  1659. } );
  1660. it( 'target after renamed element: no change', () => {
  1661. const transformBy = new InsertOperation(
  1662. new Position( root, [ 0, 2, 3 ] ),
  1663. [ nodeA, nodeB ],
  1664. 0
  1665. );
  1666. const transOp = transform.transform( op, transformBy );
  1667. expect( transOp.length ).to.equal( 1 );
  1668. expectOperation( transOp[ 0 ], expected );
  1669. } );
  1670. it( 'target before a node on path to renamed element: path update', () => {
  1671. const transformBy = new InsertOperation(
  1672. new Position( root, [ 0, 1 ] ),
  1673. [ nodeA, nodeB ],
  1674. 0
  1675. );
  1676. const transOp = transform.transform( op, transformBy );
  1677. expect( transOp.length ).to.equal( 1 );
  1678. expected.position.path = [ 0, 4, 2 ];
  1679. expectOperation( transOp[ 0 ], expected );
  1680. } );
  1681. it( 'target after a node on path to renamed element: no change', () => {
  1682. const transformBy = new InsertOperation(
  1683. new Position( root, [ 0, 3 ] ),
  1684. [ nodeA, nodeB ],
  1685. 0
  1686. );
  1687. const transOp = transform.transform( op, transformBy );
  1688. expect( transOp.length ).to.equal( 1 );
  1689. expectOperation( transOp[ 0 ], expected );
  1690. } );
  1691. } );
  1692. describe( 'by AttributeOperation', () => {
  1693. it( 'no operation update', () => {
  1694. const transformBy = new AttributeOperation(
  1695. new Range(
  1696. new Position( root, [ 0, 2, 1 ] ),
  1697. new Position( root, [ 1, 3 ] )
  1698. ),
  1699. 'foo',
  1700. 'bar',
  1701. 'xyz',
  1702. 0
  1703. );
  1704. const transOp = transform.transform( op, transformBy );
  1705. expect( transOp.length ).to.equal( 1 );
  1706. expectOperation( transOp[ 0 ], expected );
  1707. } );
  1708. } );
  1709. describe( 'by RootAttributeOperation', () => {
  1710. it( 'no operation update', () => {
  1711. const transformBy = new RootAttributeOperation(
  1712. root,
  1713. 'foo',
  1714. null,
  1715. 'bar',
  1716. 0
  1717. );
  1718. const transOp = transform.transform( op, transformBy );
  1719. expect( transOp.length ).to.equal( 1 );
  1720. expectOperation( transOp[ 0 ], expected );
  1721. } );
  1722. } );
  1723. describe( 'by MarkerOperation', () => {
  1724. it( 'no operation update', () => {
  1725. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1726. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, 0 );
  1727. const transOp = transform.transform( op, transformBy );
  1728. expect( transOp.length ).to.equal( 1 );
  1729. expectOperation( transOp[ 0 ], expected );
  1730. } );
  1731. } );
  1732. describe( 'by RenameOperation', () => {
  1733. it( 'different element: no change', () => {
  1734. const transformBy = new RenameOperation(
  1735. new Position( root, [ 0, 2, 1 ] ),
  1736. 'foo',
  1737. 'bar',
  1738. 0
  1739. );
  1740. const transOp = transform.transform( op, transformBy );
  1741. expect( transOp.length ).to.equal( 1 );
  1742. expectOperation( transOp[ 0 ], expected );
  1743. } );
  1744. it( 'same element and is important: convert to NoOperation', () => {
  1745. const transformBy = new RenameOperation(
  1746. new Position( root, [ 0, 2, 2 ] ),
  1747. 'oldName',
  1748. 'otherName',
  1749. 0
  1750. );
  1751. const transOp = transform.transform( op, transformBy );
  1752. expect( transOp.length ).to.equal( 1 );
  1753. expectOperation( transOp[ 0 ], {
  1754. type: NoOperation
  1755. } );
  1756. } );
  1757. it( 'same element and is not important: change old name to new name', () => {
  1758. const transformBy = new RenameOperation(
  1759. new Position( root, [ 0, 2, 2 ] ),
  1760. 'oldName',
  1761. 'otherName',
  1762. 0
  1763. );
  1764. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1765. expect( transOp.length ).to.equal( 1 );
  1766. expected.oldName = 'otherName';
  1767. expectOperation( transOp[ 0 ], expected );
  1768. } );
  1769. } );
  1770. describe( 'by MoveOperation', () => {
  1771. it( 'moved range before renamed element: update offset', () => {
  1772. const transformBy = new MoveOperation(
  1773. new Position( root, [ 0, 2, 0 ] ),
  1774. 2,
  1775. new Position( root, [ 2, 5 ] ),
  1776. 0
  1777. );
  1778. const transOp = transform.transform( op, transformBy );
  1779. expect( transOp.length ).to.equal( 1 );
  1780. expected.position.offset = 0;
  1781. expectOperation( transOp[ 0 ], expected );
  1782. } );
  1783. it( 'moved range before an element on path to renamed element: update path', () => {
  1784. const transformBy = new MoveOperation(
  1785. new Position( root, [ 0, 0 ] ),
  1786. 2,
  1787. new Position( root, [ 2, 5 ] ),
  1788. 0
  1789. );
  1790. const transOp = transform.transform( op, transformBy );
  1791. expect( transOp.length ).to.equal( 1 );
  1792. expected.position.path = [ 0, 0, 2 ];
  1793. expectOperation( transOp[ 0 ], expected );
  1794. } );
  1795. it( 'moved range contains renamed element: update path', () => {
  1796. const transformBy = new MoveOperation(
  1797. new Position( root, [ 0, 2, 1 ] ),
  1798. 3,
  1799. new Position( root, [ 2, 5 ] ),
  1800. 0
  1801. );
  1802. const transOp = transform.transform( op, transformBy );
  1803. expect( transOp.length ).to.equal( 1 );
  1804. expected.position.path = [ 2, 6 ];
  1805. expectOperation( transOp[ 0 ], expected );
  1806. } );
  1807. it( 'moved range contains renamed element parent: updated path', () => {
  1808. const transformBy = new MoveOperation(
  1809. new Position( root, [ 0, 1 ] ),
  1810. 3,
  1811. new Position( root, [ 2, 5 ] ),
  1812. 0
  1813. );
  1814. const transOp = transform.transform( op, transformBy );
  1815. expect( transOp.length ).to.equal( 1 );
  1816. expected.position.path = [ 2, 6, 2 ];
  1817. expectOperation( transOp[ 0 ], expected );
  1818. } );
  1819. it( 'move target before renamed element: update offset', () => {
  1820. const transformBy = new MoveOperation(
  1821. new Position( root, [ 2, 5 ] ),
  1822. 2,
  1823. new Position( root, [ 0, 2, 1 ] ),
  1824. 0
  1825. );
  1826. const transOp = transform.transform( op, transformBy );
  1827. expect( transOp.length ).to.equal( 1 );
  1828. expected.position.offset = 4;
  1829. expectOperation( transOp[ 0 ], expected );
  1830. } );
  1831. it( 'move target before an element on path to renamed element: update path', () => {
  1832. const transformBy = new MoveOperation(
  1833. new Position( root, [ 0, 2, 0 ] ),
  1834. 2,
  1835. new Position( root, [ 2, 5 ] ),
  1836. 0
  1837. );
  1838. const transOp = transform.transform( op, transformBy );
  1839. expect( transOp.length ).to.equal( 1 );
  1840. expected.position.offset = 0;
  1841. expectOperation( transOp[ 0 ], expected );
  1842. } );
  1843. } );
  1844. } );
  1845. describe( 'MarkerOperation', () => {
  1846. let oldRange, newRange;
  1847. beforeEach( () => {
  1848. oldRange = Range.createFromParentsAndOffsets( root, 1, root, 4 );
  1849. newRange = Range.createFromParentsAndOffsets( root, 10, root, 12 );
  1850. op = new MarkerOperation( 'name', oldRange, newRange, model.markers, 0 );
  1851. expected = {
  1852. name: 'name',
  1853. oldRange,
  1854. newRange
  1855. };
  1856. } );
  1857. describe( 'by InsertOperation', () => {
  1858. it( 'insert position affecting oldRange: update oldRange', () => {
  1859. // Just CC things.
  1860. op.newRange = null;
  1861. const transformBy = new InsertOperation( Position.createAt( root, 0 ), [ nodeA, nodeB ], 0 );
  1862. const transOp = transform.transform( op, transformBy );
  1863. expected.newRange = null;
  1864. expected.oldRange.start.offset = 3;
  1865. expected.oldRange.end.offset = 6;
  1866. expect( transOp.length ).to.equal( 1 );
  1867. expectOperation( transOp[ 0 ], expected );
  1868. } );
  1869. it( 'insert position affecting newRange: update newRange', () => {
  1870. // Just CC things.
  1871. op.oldRange = null;
  1872. const transformBy = new InsertOperation( Position.createAt( root, 8 ), [ nodeA, nodeB ], 0 );
  1873. const transOp = transform.transform( op, transformBy );
  1874. expected.oldRange = null;
  1875. expected.newRange.start.offset = 12;
  1876. expected.newRange.end.offset = 14;
  1877. expect( transOp.length ).to.equal( 1 );
  1878. expectOperation( transOp[ 0 ], expected );
  1879. } );
  1880. } );
  1881. describe( 'by AttributeOperation', () => {
  1882. it( 'no operation update', () => {
  1883. const transformBy = new AttributeOperation(
  1884. new Range(
  1885. new Position( root, [ 2 ] ),
  1886. new Position( root, [ 11 ] )
  1887. ),
  1888. 'foo',
  1889. 'bar',
  1890. 'xyz',
  1891. 0
  1892. );
  1893. const transOp = transform.transform( op, transformBy );
  1894. expect( transOp.length ).to.equal( 1 );
  1895. expectOperation( transOp[ 0 ], expected );
  1896. } );
  1897. } );
  1898. describe( 'by MoveOperation', () => {
  1899. it( 'moved range is before oldRange: update oldRange', () => {
  1900. // Just CC things.
  1901. op.newRange = null;
  1902. const transformBy = new MoveOperation( Position.createAt( root, 0 ), 1, Position.createAt( root, 20 ), 0 );
  1903. const transOp = transform.transform( op, transformBy );
  1904. expected.newRange = null;
  1905. expected.oldRange.start.offset = 0;
  1906. expected.oldRange.end.offset = 3;
  1907. expect( transOp.length ).to.equal( 1 );
  1908. expectOperation( transOp[ 0 ], expected );
  1909. } );
  1910. it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
  1911. const transformBy = new MoveOperation( Position.createAt( root, 2 ), 2, Position.createAt( root, 20 ), 0 );
  1912. const transOp = transform.transform( op, transformBy );
  1913. expected.oldRange.start.offset = 1;
  1914. expected.oldRange.end.offset = 2;
  1915. expected.newRange.start.offset = 8;
  1916. expected.newRange.end.offset = 10;
  1917. expect( transOp.length ).to.equal( 1 );
  1918. expectOperation( transOp[ 0 ], expected );
  1919. } );
  1920. it( 'target position is inside newRange: update newRange', () => {
  1921. // Just CC things.
  1922. op.oldRange = null;
  1923. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 2, Position.createAt( root, 11 ), 0 );
  1924. const transOp = transform.transform( op, transformBy );
  1925. expected.oldRange = null;
  1926. expected.newRange.start.offset = 10;
  1927. expected.newRange.end.offset = 14;
  1928. expect( transOp.length ).to.equal( 1 );
  1929. expectOperation( transOp[ 0 ], expected );
  1930. } );
  1931. it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
  1932. const transformBy = new MoveOperation( Position.createAt( root, 20 ), 4, Position.createAt( root, 2 ), 0 );
  1933. const transOp = transform.transform( op, transformBy );
  1934. expected.oldRange.start.offset = 1;
  1935. expected.oldRange.end.offset = 8;
  1936. expected.newRange.start.offset = 14;
  1937. expected.newRange.end.offset = 16;
  1938. expect( transOp.length ).to.equal( 1 );
  1939. expectOperation( transOp[ 0 ], expected );
  1940. } );
  1941. } );
  1942. describe( 'by RootAttributeOperation', () => {
  1943. it( 'no operation update', () => {
  1944. const transformBy = new RootAttributeOperation(
  1945. root,
  1946. 'foo',
  1947. null,
  1948. 'bar',
  1949. 0
  1950. );
  1951. const transOp = transform.transform( op, transformBy );
  1952. expect( transOp.length ).to.equal( 1 );
  1953. expectOperation( transOp[ 0 ], expected );
  1954. } );
  1955. } );
  1956. describe( 'by RenameOperation', () => {
  1957. it( 'no operation update', () => {
  1958. const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', 0 );
  1959. const transOp = transform.transform( op, transformBy );
  1960. expect( transOp.length ).to.equal( 1 );
  1961. expectOperation( transOp[ 0 ], expected );
  1962. } );
  1963. } );
  1964. describe( 'by MarkerOperation', () => {
  1965. it( 'different marker name: no operation update', () => {
  1966. const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, model.markers, 0 );
  1967. const transOp = transform.transform( op, transformBy );
  1968. expect( transOp.length ).to.equal( 1 );
  1969. expectOperation( transOp[ 0 ], expected );
  1970. } );
  1971. it( 'same marker name and is important: convert to NoOperation', () => {
  1972. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  1973. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
  1974. const transOp = transform.transform( op, transformBy );
  1975. expect( transOp.length ).to.equal( 1 );
  1976. expectOperation( transOp[ 0 ], {
  1977. type: NoOperation
  1978. } );
  1979. } );
  1980. it( 'same marker name and is less important: update oldRange parameter', () => {
  1981. const anotherRange = Range.createFromParentsAndOffsets( root, 2, root, 2 );
  1982. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, 0 );
  1983. const transOp = transform.transform( op, transformBy, { aIsStrong: true } );
  1984. expected.oldRange = anotherRange;
  1985. expect( transOp.length ).to.equal( 1 );
  1986. expectOperation( transOp[ 0 ], expected );
  1987. } );
  1988. } );
  1989. } );
  1990. } );