8
0

transform.js 70 KB

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