transform.js 70 KB

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