transform.js 69 KB

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