8
0

transform.js 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console */
  6. import { transform, transformSets } from '../../../src/model/operation/transform';
  7. import Model from '../../../src/model/model';
  8. import RootElement from '../../../src/model/rootelement';
  9. import Node from '../../../src/model/node';
  10. import Position from '../../../src/model/position';
  11. import Range from '../../../src/model/range';
  12. import InsertOperation from '../../../src/model/operation/insertoperation';
  13. import AttributeOperation from '../../../src/model/operation/attributeoperation';
  14. import RootAttributeOperation from '../../../src/model/operation/rootattributeoperation';
  15. import MarkerOperation from '../../../src/model/operation/markeroperation';
  16. import MoveOperation from '../../../src/model/operation/moveoperation';
  17. import RenameOperation from '../../../src/model/operation/renameoperation';
  18. import NoOperation from '../../../src/model/operation/nooperation';
  19. import SplitOperation from '../../../src/model/operation/splitoperation';
  20. describe( 'transform', () => {
  21. let model, doc, root, op, nodeA, nodeB, expected;
  22. beforeEach( () => {
  23. model = new Model();
  24. doc = model.document;
  25. root = doc.createRoot();
  26. nodeA = new Node();
  27. nodeB = new Node();
  28. } );
  29. afterEach( () => {
  30. sinon.restore();
  31. } );
  32. function expectOperation( op, params ) {
  33. for ( const i in params ) {
  34. if ( params.hasOwnProperty( i ) ) {
  35. if ( i == 'type' ) {
  36. expect( op, 'type' ).to.be.instanceof( params[ i ] );
  37. }
  38. else if ( params[ i ] instanceof Array ) {
  39. expect( op[ i ].length, i ).to.equal( params[ i ].length );
  40. for ( let j = 0; j < params[ i ].length; j++ ) {
  41. expect( op[ i ][ j ] ).to.equal( params[ i ][ j ] );
  42. }
  43. } else if ( params[ i ] instanceof Position || params[ i ] instanceof Range ) {
  44. expect( op[ i ].isEqual( params[ i ] ), i ).to.be.true;
  45. } else {
  46. expect( op[ i ], i ).to.equal( params[ i ] );
  47. }
  48. }
  49. }
  50. }
  51. const strongContext = {
  52. aIsStrong: true
  53. };
  54. it( 'should throw an error when one of operations is invalid', () => {
  55. // Catches the 'Error during operation transformation!' warning in the CK_DEBUG mode.
  56. sinon.stub( console, 'warn' );
  57. const nodeA = new Node();
  58. const nodeB = new Node();
  59. const position = new Position( root, [ 0 ] );
  60. const a = new InsertOperation( position, [ nodeA ], 0 );
  61. const b = new InsertOperation( position, [ nodeB ], 0 );
  62. // Modify an operation so it will throw an error.
  63. a.position = null;
  64. expect( () => {
  65. transform( a, b, {
  66. aIsStrong: true,
  67. aWasUndone: false,
  68. bWasUndone: false,
  69. abRelation: null,
  70. baRelation: null
  71. } );
  72. } ).to.throw( TypeError );
  73. } );
  74. describe( 'InsertOperation', () => {
  75. let nodeC, nodeD, position;
  76. beforeEach( () => {
  77. nodeC = new Node();
  78. nodeD = new Node();
  79. position = new Position( root, [ 0, 2, 2 ] );
  80. op = new InsertOperation( position, [ nodeA, nodeB ], 0 );
  81. expected = {
  82. type: InsertOperation,
  83. position: position.clone()
  84. };
  85. } );
  86. describe( 'by InsertOperation', () => {
  87. it( 'target at different position: no position update', () => {
  88. const transformBy = new InsertOperation(
  89. new Position( root, [ 1, 3, 2 ] ),
  90. [ nodeC, nodeD ],
  91. 0
  92. );
  93. const transOp = transform( op, transformBy );
  94. expect( transOp.length ).to.equal( 1 );
  95. expectOperation( transOp[ 0 ], expected );
  96. } );
  97. it( 'target at offset before: increment offset', () => {
  98. const transformBy = new InsertOperation(
  99. new Position( root, [ 0, 2, 0 ] ),
  100. [ nodeC, nodeD ],
  101. 0
  102. );
  103. const transOp = transform( op, transformBy );
  104. expected.position.offset += 2;
  105. expect( transOp.length ).to.equal( 1 );
  106. expectOperation( transOp[ 0 ], expected );
  107. } );
  108. it( 'target at same offset and is important: increment offset', () => {
  109. const transformBy = new InsertOperation(
  110. new Position( root, [ 0, 2, 2 ] ),
  111. [ nodeC, nodeD ],
  112. 0
  113. );
  114. const transOp = transform( op, transformBy );
  115. expected.position.offset += 2;
  116. expect( transOp.length ).to.equal( 1 );
  117. expectOperation( transOp[ 0 ], expected );
  118. } );
  119. it( 'target at same offset and is less important: no position update', () => {
  120. const transformBy = new InsertOperation(
  121. new Position( root, [ 0, 2, 2 ] ),
  122. [ nodeC, nodeD ],
  123. 0
  124. );
  125. const transOp = transform( op, transformBy, strongContext );
  126. expect( transOp.length ).to.equal( 1 );
  127. expectOperation( transOp[ 0 ], expected );
  128. } );
  129. it( 'target at same offset and context.insertBefore = false: increment offset', () => {
  130. const transformBy = new InsertOperation(
  131. new Position( root, [ 0, 2, 2 ] ),
  132. [ nodeC, nodeD ],
  133. 0
  134. );
  135. const transOp = transform( op, transformBy );
  136. expected.position.offset += 2;
  137. expect( transOp.length ).to.equal( 1 );
  138. expectOperation( transOp[ 0 ], expected );
  139. } );
  140. it( 'target at offset after: no position update', () => {
  141. const transformBy = new InsertOperation(
  142. new Position( root, [ 0, 2, 3 ] ),
  143. [ nodeC, nodeD ],
  144. 0
  145. );
  146. const transOp = transform( op, transformBy );
  147. expect( transOp.length ).to.equal( 1 );
  148. expectOperation( transOp[ 0 ], expected );
  149. } );
  150. it( 'target before node from path: increment index on path', () => {
  151. const transformBy = new InsertOperation(
  152. new Position( root, [ 0, 1 ] ),
  153. [ nodeC, nodeD ],
  154. 0
  155. );
  156. const transOp = transform( op, transformBy );
  157. expected.position.path[ 1 ] += 2;
  158. expect( transOp.length ).to.equal( 1 );
  159. expectOperation( transOp[ 0 ], expected );
  160. } );
  161. it( 'target after node from path: no position update', () => {
  162. const transformBy = new InsertOperation(
  163. new Position( root, [ 0, 6 ] ),
  164. [ nodeC, nodeD ],
  165. 0
  166. );
  167. const transOp = transform( op, transformBy );
  168. expect( transOp.length ).to.equal( 1 );
  169. expectOperation( transOp[ 0 ], expected );
  170. } );
  171. } );
  172. describe( 'by AttributeOperation', () => {
  173. it( 'no position update', () => {
  174. const transformBy = new AttributeOperation(
  175. Range._createFromPositionAndShift( position, 2 ),
  176. 'foo',
  177. null,
  178. 'bar',
  179. 0
  180. );
  181. const transOp = transform( op, transformBy );
  182. expect( transOp.length ).to.equal( 1 );
  183. expectOperation( transOp[ 0 ], expected );
  184. } );
  185. } );
  186. describe( 'by RootAttributeOperation', () => {
  187. it( 'no position update', () => {
  188. const transformBy = new RootAttributeOperation(
  189. root,
  190. 'foo',
  191. null,
  192. 'bar',
  193. 0
  194. );
  195. const transOp = transform( op, transformBy );
  196. expect( transOp.length ).to.equal( 1 );
  197. expectOperation( transOp[ 0 ], expected );
  198. } );
  199. } );
  200. describe( 'by MoveOperation', () => {
  201. it( 'range and target are different than insert position: no position update', () => {
  202. const transformBy = new MoveOperation(
  203. new Position( root, [ 1, 3, 2 ] ),
  204. 2,
  205. new Position( root, [ 2, 1 ] ),
  206. 0
  207. );
  208. const transOp = transform( op, transformBy );
  209. expect( transOp.length ).to.equal( 1 );
  210. expectOperation( transOp[ 0 ], expected );
  211. } );
  212. it( 'range is before insert position offset: decrement offset', () => {
  213. const transformBy = new MoveOperation(
  214. new Position( root, [ 0, 2, 0 ] ),
  215. 1,
  216. new Position( root, [ 1, 1 ] ),
  217. 0
  218. );
  219. const transOp = transform( op, transformBy );
  220. expected.position.offset--;
  221. expect( transOp.length ).to.equal( 1 );
  222. expectOperation( transOp[ 0 ], expected );
  223. } );
  224. it( 'range offset is after insert position offset: no position update', () => {
  225. const transformBy = new MoveOperation(
  226. new Position( root, [ 0, 2, 5 ] ),
  227. 1,
  228. new Position( root, [ 1, 1 ] ),
  229. 0
  230. );
  231. const transOp = transform( op, transformBy );
  232. expect( transOp.length ).to.equal( 1 );
  233. expectOperation( transOp[ 0 ], expected );
  234. } );
  235. it( 'target offset before insert position offset: increment offset', () => {
  236. const transformBy = new MoveOperation(
  237. new Position( root, [ 1, 1 ] ),
  238. 2,
  239. new Position( root, [ 0, 2, 0 ] ),
  240. 0
  241. );
  242. const transOp = transform( op, transformBy );
  243. expected.position.offset += 2;
  244. expect( transOp.length ).to.equal( 1 );
  245. expectOperation( transOp[ 0 ], expected );
  246. } );
  247. it( 'target offset after insert position offset: no position update', () => {
  248. const transformBy = new MoveOperation(
  249. new Position( root, [ 1, 1 ] ),
  250. 2,
  251. new Position( root, [ 0, 2, 5 ] ),
  252. 0
  253. );
  254. const transOp = transform( op, transformBy );
  255. expect( transOp.length ).to.equal( 1 );
  256. expectOperation( transOp[ 0 ], expected );
  257. } );
  258. it( 'target offset same as insert position offset and is important: increment offset', () => {
  259. const transformBy = new MoveOperation(
  260. new Position( root, [ 1, 1 ] ),
  261. 2,
  262. new Position( root, [ 0, 2, 2 ] ),
  263. 0
  264. );
  265. const transOp = transform( op, transformBy );
  266. expected.position.offset += 2;
  267. expect( transOp.length ).to.equal( 1 );
  268. expectOperation( transOp[ 0 ], expected );
  269. } );
  270. it( 'target offset same as insert position offset and is less important: increment offset', () => {
  271. const transformBy = new MoveOperation(
  272. new Position( root, [ 1, 1 ] ),
  273. 2,
  274. new Position( root, [ 0, 2, 2 ] ),
  275. 0
  276. );
  277. const transOp = transform( op, transformBy, strongContext );
  278. expected.position.offset += 2;
  279. expect( transOp.length ).to.equal( 1 );
  280. expectOperation( transOp[ 0 ], expected );
  281. } );
  282. it( 'range is before node from insert position path: decrement index on path', () => {
  283. const transformBy = new MoveOperation(
  284. new Position( root, [ 0, 0 ] ),
  285. 1,
  286. new Position( root, [ 1, 0 ] ),
  287. 0
  288. );
  289. const transOp = transform( op, transformBy );
  290. expected.position.path[ 1 ] -= 1;
  291. expect( transOp.length ).to.equal( 1 );
  292. expectOperation( transOp[ 0 ], expected );
  293. } );
  294. it( 'range is after node from insert position path: no position update', () => {
  295. const transformBy = new MoveOperation(
  296. new Position( root, [ 0, 4 ] ),
  297. 2,
  298. new Position( root, [ 1, 0 ] ),
  299. 0
  300. );
  301. const transOp = transform( op, transformBy );
  302. expect( transOp.length ).to.equal( 1 );
  303. expectOperation( transOp[ 0 ], expected );
  304. } );
  305. it( 'target before node from insert position path: increment index on path', () => {
  306. const transformBy = new MoveOperation(
  307. new Position( root, [ 1, 0 ] ),
  308. 2,
  309. new Position( root, [ 0, 0 ] ),
  310. 0
  311. );
  312. const transOp = transform( op, transformBy );
  313. expected.position.path[ 1 ] += 2;
  314. expect( transOp.length ).to.equal( 1 );
  315. expectOperation( transOp[ 0 ], expected );
  316. } );
  317. it( 'target after node from insert position path: no position update', () => {
  318. const transformBy = new MoveOperation(
  319. new Position( root, [ 1, 0 ] ),
  320. 2,
  321. new Position( root, [ 0, 4 ] ),
  322. 0
  323. );
  324. const transOp = transform( op, transformBy );
  325. expect( transOp.length ).to.equal( 1 );
  326. expectOperation( transOp[ 0 ], expected );
  327. } );
  328. it( 'range has node that contains insert position: update position', () => {
  329. const transformBy = new MoveOperation(
  330. new Position( root, [ 0, 1 ] ),
  331. 2,
  332. new Position( root, [ 1, 1 ] ),
  333. 0
  334. );
  335. const transOp = transform( op, transformBy );
  336. expected.position.path = [ 1, 2, 2 ];
  337. expect( transOp.length ).to.equal( 1 );
  338. expectOperation( transOp[ 0 ], expected );
  339. } );
  340. it( 'range contains insert position (on same level): update position', () => {
  341. const transformBy = new MoveOperation(
  342. new Position( root, [ 0, 2, 0 ] ),
  343. 4,
  344. new Position( root, [ 1, 0 ] ),
  345. 0
  346. );
  347. const transOp = transform( op, transformBy );
  348. expected.position.path = [ 1, 2 ];
  349. expect( transOp.length ).to.equal( 1 );
  350. expectOperation( transOp[ 0 ], expected );
  351. } );
  352. } );
  353. describe( 'by NoOperation', () => {
  354. it( 'no operation update', () => {
  355. const transformBy = new NoOperation( 0 );
  356. const transOp = transform( op, transformBy );
  357. expect( transOp.length ).to.equal( 1 );
  358. expectOperation( transOp[ 0 ], expected );
  359. } );
  360. } );
  361. describe( 'by RenameOperation', () => {
  362. it( 'no position update', () => {
  363. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  364. const transOp = transform( op, transformBy );
  365. expect( transOp.length ).to.equal( 1 );
  366. expectOperation( transOp[ 0 ], expected );
  367. } );
  368. } );
  369. describe( 'by MarkerOperation', () => {
  370. it( 'no position update', () => {
  371. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 4 ] ) );
  372. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  373. const transOp = transform( op, transformBy );
  374. expect( transOp.length ).to.equal( 1 );
  375. expectOperation( transOp[ 0 ], expected );
  376. } );
  377. } );
  378. } );
  379. describe( 'AttributeOperation', () => {
  380. let start, end, range;
  381. beforeEach( () => {
  382. start = new Position( root, [ 0, 2, 1 ] );
  383. end = new Position( root, [ 0, 2, 4 ] );
  384. range = new Range( start, end );
  385. op = new AttributeOperation( range, 'foo', 'abc', 'bar', 0 );
  386. expected = {
  387. type: AttributeOperation,
  388. range,
  389. key: 'foo',
  390. oldValue: 'abc',
  391. newValue: 'bar'
  392. };
  393. } );
  394. describe( 'by InsertOperation', () => {
  395. it( 'target at offset before: increment offset', () => {
  396. const transformBy = new InsertOperation(
  397. new Position( root, [ 0, 2, 0 ] ),
  398. [ nodeA, nodeB ],
  399. 0
  400. );
  401. const transOp = transform( op, transformBy );
  402. expected.range.start.offset += 2;
  403. expected.range.end.offset += 2;
  404. expect( transOp.length ).to.equal( 1 );
  405. expectOperation( transOp[ 0 ], expected );
  406. } );
  407. it( 'target at same offset: increment offset', () => {
  408. const transformBy = new InsertOperation(
  409. new Position( root, [ 0, 2, 1 ] ),
  410. [ nodeA, nodeB ],
  411. 0
  412. );
  413. const transOp = transform( op, transformBy );
  414. expected.range.start.offset += 2;
  415. expected.range.end.offset += 2;
  416. expect( transOp.length ).to.equal( 1 );
  417. expectOperation( transOp[ 0 ], expected );
  418. } );
  419. } );
  420. describe( 'by MoveOperation', () => {
  421. it( 'range offset is before change range start offset: decrement offset', () => {
  422. const transformBy = new MoveOperation(
  423. new Position( root, [ 0, 2, 0 ] ),
  424. 1,
  425. new Position( root, [ 2, 4, 1 ] ),
  426. 0
  427. );
  428. const transOp = transform( op, transformBy );
  429. expected.range.start.offset--;
  430. expected.range.end.offset--;
  431. expect( transOp.length ).to.equal( 1 );
  432. expectOperation( transOp[ 0 ], expected );
  433. } );
  434. it( 'target offset is before change range start offset: increment offset', () => {
  435. const transformBy = new MoveOperation(
  436. new Position( root, [ 2, 4, 1 ] ),
  437. 2,
  438. new Position( root, [ 0, 2, 0 ] ),
  439. 0
  440. );
  441. const transOp = transform( op, transformBy );
  442. expected.range.start.offset += 2;
  443. expected.range.end.offset += 2;
  444. expect( transOp.length ).to.equal( 1 );
  445. expectOperation( transOp[ 0 ], expected );
  446. } );
  447. it( 'range intersects on left with change range: split into two operations', () => {
  448. const transformBy = new MoveOperation(
  449. new Position( root, [ 0, 2, 2 ] ),
  450. 4,
  451. new Position( root, [ 2, 4, 1 ] ),
  452. 0
  453. );
  454. const transOp = transform( op, transformBy );
  455. expect( transOp.length ).to.equal( 2 );
  456. expected.range.end.offset -= 2;
  457. expectOperation( transOp[ 0 ], expected );
  458. expected.range.start.path = [ 2, 4, 1 ];
  459. expected.range.end.path = [ 2, 4, 3 ];
  460. expectOperation( transOp[ 1 ], expected );
  461. } );
  462. it( 'range intersects on right with change range: split into two operation', () => {
  463. const transformBy = new MoveOperation(
  464. new Position( root, [ 0, 2, 0 ] ),
  465. 2,
  466. new Position( root, [ 2, 4, 1 ] ),
  467. 0
  468. );
  469. const transOp = transform( op, transformBy );
  470. expect( transOp.length ).to.equal( 2 );
  471. expected.range.start.offset -= 1;
  472. expected.range.end.offset -= 2;
  473. expectOperation( transOp[ 0 ], expected );
  474. expected.range.start.path = [ 2, 4, 2 ];
  475. expected.range.end.path = [ 2, 4, 3 ];
  476. expectOperation( transOp[ 1 ], expected );
  477. } );
  478. it( 'range contains change range: update change range', () => {
  479. const transformBy = new MoveOperation(
  480. new Position( root, [ 0, 1 ] ),
  481. 3,
  482. new Position( root, [ 2, 4, 1 ] ),
  483. 0
  484. );
  485. const transOp = transform( op, transformBy );
  486. expected.range.start.path = [ 2, 4, 2, 1 ];
  487. expected.range.end.path = [ 2, 4, 2, 4 ];
  488. expect( transOp.length ).to.equal( 1 );
  489. expectOperation( transOp[ 0 ], expected );
  490. } );
  491. it( 'range is inside change range: split into two operations', () => {
  492. const transformBy = new MoveOperation(
  493. new Position( root, [ 0, 2, 2 ] ),
  494. 1,
  495. new Position( root, [ 2, 4, 1 ] ),
  496. 0
  497. );
  498. const transOp = transform( op, transformBy );
  499. expect( transOp.length ).to.equal( 3 );
  500. expected.range.start.path = [ 0, 2, 1 ];
  501. expected.range.end.path = [ 0, 2, 2 ];
  502. expectOperation( transOp[ 0 ], expected );
  503. expected.range.start.path = [ 0, 2, 2 ];
  504. expected.range.end.path = [ 0, 2, 3 ];
  505. expectOperation( transOp[ 1 ], expected );
  506. expected.range.start.path = [ 2, 4, 1 ];
  507. expected.range.end.path = [ 2, 4, 2 ];
  508. expectOperation( transOp[ 2 ], expected );
  509. } );
  510. it( 'range is same as change range: update change range', () => {
  511. const transformBy = new MoveOperation(
  512. new Position( root, [ 0, 2, 1 ] ),
  513. 3,
  514. new Position( root, [ 2, 4, 1 ] ),
  515. 0
  516. );
  517. const transOp = transform( op, transformBy );
  518. expected.range.start.path = [ 2, 4, 1 ];
  519. expected.range.end.path = [ 2, 4, 4 ];
  520. expect( transOp.length ).to.equal( 1 );
  521. expectOperation( transOp[ 0 ], expected );
  522. } );
  523. it( 'target inside change range: split into two operations', () => {
  524. const transformBy = new MoveOperation(
  525. new Position( root, [ 2, 4, 1 ] ),
  526. 2,
  527. new Position( root, [ 0, 2, 2 ] ),
  528. 0
  529. );
  530. const transOp = transform( op, transformBy );
  531. expect( transOp.length ).to.equal( 2 );
  532. expected.range.end.offset = 2;
  533. expectOperation( transOp[ 0 ], expected );
  534. expected.range.start.offset = 4;
  535. expected.range.end.offset = 6;
  536. expectOperation( transOp[ 1 ], expected );
  537. } );
  538. it( 'range intersects change range and target inside change range: split into three operations', () => {
  539. const transformBy = new MoveOperation(
  540. new Position( root, [ 0, 2, 0 ] ),
  541. 2,
  542. new Position( root, [ 0, 2, 3 ] ),
  543. 0
  544. );
  545. const transOp = transform( op, transformBy );
  546. expect( transOp.length ).to.equal( 3 );
  547. expected.range.start.path = [ 0, 2, 0 ];
  548. expected.range.end.path = [ 0, 2, 1 ];
  549. expectOperation( transOp[ 0 ], expected );
  550. expected.range.start.path = [ 0, 2, 3 ];
  551. expected.range.end.path = [ 0, 2, 4 ];
  552. expectOperation( transOp[ 1 ], expected );
  553. expected.range.start.path = [ 0, 2, 2 ];
  554. expected.range.end.path = [ 0, 2, 3 ];
  555. expectOperation( transOp[ 2 ], expected );
  556. } );
  557. } );
  558. describe( 'by SplitOperation', () => {
  559. it( 'stays the same if split of end node and has graveyardPosition', () => {
  560. const transformBy = new SplitOperation(
  561. new Position( root, [ 0, 2, 3, 1 ] ),
  562. 1,
  563. new Position( doc.graveyard, [ 0 ] ),
  564. 0
  565. );
  566. const transOp = 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( 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( 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( 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( 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( 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( 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( op, transformBy, strongContext );
  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( 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( 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( 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, false, 0 );
  712. const transOp = 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 = sourcePosition.clone();
  725. rangeEnd.offset += howMany;
  726. op = new MoveOperation( sourcePosition, howMany, targetPosition, 0 );
  727. expected = {
  728. type: MoveOperation,
  729. sourcePosition: sourcePosition.clone(),
  730. targetPosition: targetPosition.clone(),
  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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( op, transformBy, strongContext );
  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( 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( 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( 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( 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( 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( 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( 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( 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( op, transformBy, strongContext );
  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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( 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( op, transformBy, strongContext );
  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( 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( op, transformBy, strongContext );
  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( 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( op, transformBy, strongContext );
  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( 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( op, transformBy, strongContext );
  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( 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( op, transformBy, strongContext );
  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( 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( op, transformBy, strongContext );
  1357. expect( transOp.length ).to.equal( 3 );
  1358. expected.sourcePosition.path = [ 2, 2, 3 ];
  1359. expected.howMany = 1;
  1360. expectOperation( transOp[ 0 ], expected );
  1361. expected.sourcePosition.path = [ 2, 2, 4 ];
  1362. expected.howMany = 1;
  1363. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1364. expectOperation( transOp[ 1 ], expected );
  1365. expected.sourcePosition.path = [ 2, 2, 4 ];
  1366. expected.howMany = 2;
  1367. expected.targetPosition = targetPosition.getShiftedBy( 2 );
  1368. expectOperation( transOp[ 2 ], expected );
  1369. } );
  1370. it( 'range intersects on right side of transforming range and is important: shrink range', () => {
  1371. const transformBy = new MoveOperation(
  1372. new Position( root, [ 2, 2, 5 ] ),
  1373. 2,
  1374. new Position( root, [ 4, 1, 0 ] ),
  1375. 0
  1376. );
  1377. const transOp = transform( op, transformBy );
  1378. expected.howMany = 1;
  1379. expect( transOp.length ).to.equal( 1 );
  1380. expectOperation( transOp[ 0 ], expected );
  1381. } );
  1382. it( 'range intersects on right side of transforming range and is less important: split into two operations', () => {
  1383. const transformBy = new MoveOperation(
  1384. new Position( root, [ 2, 2, 5 ] ),
  1385. 2,
  1386. new Position( root, [ 4, 1, 0 ] ),
  1387. 0
  1388. );
  1389. const transOp = transform( op, transformBy, strongContext );
  1390. expect( transOp.length ).to.equal( 2 );
  1391. expected.sourcePosition = sourcePosition;
  1392. expected.howMany = 1;
  1393. expectOperation( transOp[ 0 ], expected );
  1394. expected.sourcePosition = new Position( root, [ 4, 1, 0 ] );
  1395. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1396. expectOperation( transOp[ 1 ], expected );
  1397. } );
  1398. it( 'range intersects, target inside transforming range and is important: split into two operations', () => {
  1399. op.targetPosition.path = [ 2, 2, 7 ];
  1400. const transformBy = new MoveOperation(
  1401. new Position( root, [ 2, 2, 5 ] ),
  1402. 4,
  1403. new Position( root, [ 4, 1, 0 ] ),
  1404. 0
  1405. );
  1406. const transOp = transform( op, transformBy, strongContext );
  1407. expect( transOp.length ).to.equal( 2 );
  1408. expected.sourcePosition.path = [ 2, 2, 4 ];
  1409. expected.targetPosition.path = [ 4, 1, 2 ];
  1410. expected.howMany = 1;
  1411. expectOperation( transOp[ 0 ], expected );
  1412. expected.sourcePosition.path = [ 4, 1, 0 ];
  1413. expected.targetPosition.path = [ 4, 1, 3 ];
  1414. expectOperation( transOp[ 1 ], expected );
  1415. } );
  1416. it( 'range intersects, target inside transforming range and is less important: shrink range', () => {
  1417. op.targetPosition.path = [ 2, 2, 7 ];
  1418. const transformBy = new MoveOperation(
  1419. new Position( root, [ 2, 2, 5 ] ),
  1420. 4,
  1421. new Position( root, [ 4, 1, 0 ] ),
  1422. 0
  1423. );
  1424. const transOp = transform( op, transformBy );
  1425. expect( transOp.length ).to.equal( 1 );
  1426. expected.sourcePosition.path = [ 2, 2, 4 ];
  1427. expected.targetPosition.path = [ 4, 1, 2 ];
  1428. expected.howMany = 1;
  1429. expectOperation( transOp[ 0 ], expected );
  1430. } );
  1431. it( 'range inside transforming range and is important: split into two operations', () => {
  1432. op.howMany = 4;
  1433. const transformBy = new MoveOperation(
  1434. new Position( root, [ 2, 2, 5 ] ),
  1435. 2,
  1436. new Position( root, [ 4, 1, 0 ] ),
  1437. 0
  1438. );
  1439. const transOp = transform( op, transformBy );
  1440. expect( transOp.length ).to.equal( 2 );
  1441. expected.sourcePosition.path = [ 2, 2, 4 ];
  1442. expected.howMany = 1;
  1443. expectOperation( transOp[ 0 ], expected );
  1444. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1445. expectOperation( transOp[ 1 ], expected );
  1446. } );
  1447. it( 'range inside transforming range and is less important: split into three operations', () => {
  1448. op.howMany = 4;
  1449. const transformBy = new MoveOperation(
  1450. new Position( root, [ 2, 2, 5 ] ),
  1451. 2,
  1452. new Position( root, [ 4, 1, 0 ] ),
  1453. 0
  1454. );
  1455. const transOp = transform( op, transformBy, strongContext );
  1456. expect( transOp.length ).to.equal( 3 );
  1457. expected.sourcePosition.path = [ 2, 2, 4 ];
  1458. expected.howMany = 1;
  1459. expectOperation( transOp[ 0 ], expected );
  1460. expected.sourcePosition.path = [ 4, 1, 0 ];
  1461. expected.targetPosition = targetPosition.getShiftedBy( 1 );
  1462. expected.howMany = 2;
  1463. expectOperation( transOp[ 1 ], expected );
  1464. expected.sourcePosition.path = [ 2, 2, 4 ];
  1465. expected.targetPosition = targetPosition.getShiftedBy( 3 );
  1466. expected.howMany = 1;
  1467. expectOperation( transOp[ 2 ], expected );
  1468. } );
  1469. it( 'range and target inside transforming range and is important: no operation update', () => {
  1470. op.howMany = 6;
  1471. const transformBy = new MoveOperation(
  1472. new Position( root, [ 2, 2, 5 ] ),
  1473. 2,
  1474. new Position( root, [ 2, 2, 9 ] ),
  1475. 0
  1476. );
  1477. const transOp = transform( op, transformBy );
  1478. expect( transOp.length ).to.equal( 1 );
  1479. expected.howMany = 6;
  1480. expectOperation( transOp[ 0 ], expected );
  1481. } );
  1482. it( 'range and target inside transforming range and is less important: no operation update', () => {
  1483. op.howMany = 6;
  1484. const transformBy = new MoveOperation(
  1485. new Position( root, [ 2, 2, 5 ] ),
  1486. 2,
  1487. new Position( root, [ 2, 2, 9 ] ),
  1488. 0
  1489. );
  1490. const transOp = transform( op, transformBy, strongContext );
  1491. expect( transOp.length ).to.equal( 1 );
  1492. expected.howMany = 6;
  1493. expectOperation( transOp[ 0 ], expected );
  1494. } );
  1495. } );
  1496. describe( 'by MoveOperation to graveyard', () => {
  1497. let transformBy;
  1498. beforeEach( () => {
  1499. transformBy = new MoveOperation(
  1500. op.sourcePosition.clone(),
  1501. op.howMany,
  1502. new Position( doc.graveyard, [ 0 ] ),
  1503. 0
  1504. );
  1505. } );
  1506. it( 'should skip context.aIsStrong and be less important than MoveOperation', () => {
  1507. const transOp = transform( op, transformBy, strongContext );
  1508. expect( transOp.length ).to.equal( 1 );
  1509. expectOperation( transOp[ 0 ], {
  1510. type: NoOperation
  1511. } );
  1512. } );
  1513. } );
  1514. describe( 'by NoOperation', () => {
  1515. it( 'no operation update', () => {
  1516. const transformBy = new NoOperation( 0 );
  1517. const transOp = transform( op, transformBy );
  1518. expect( transOp.length ).to.equal( 1 );
  1519. expectOperation( transOp[ 0 ], expected );
  1520. } );
  1521. } );
  1522. describe( 'by RenameOperation', () => {
  1523. it( 'no position update', () => {
  1524. const transformBy = new RenameOperation( new Position( root, [ 2, 2, 4 ] ), 'oldName', 'newName', 0 );
  1525. const transOp = transform( op, transformBy );
  1526. expect( transOp.length ).to.equal( 1 );
  1527. expectOperation( transOp[ 0 ], expected );
  1528. } );
  1529. } );
  1530. describe( 'by MarkerOperation', () => {
  1531. it( 'no position update', () => {
  1532. const newRange = new Range( new Position( root, [ 2, 2, 3 ] ), new Position( root, [ 2, 2, 8 ] ) );
  1533. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1534. const transOp = transform( op, transformBy );
  1535. expect( transOp.length ).to.equal( 1 );
  1536. expectOperation( transOp[ 0 ], expected );
  1537. } );
  1538. } );
  1539. } );
  1540. describe( 'MoveOperation to graveyard', () => {
  1541. describe( 'by MoveOperation', () => {
  1542. it( 'should force removing content even if was less important', () => {
  1543. const op = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  1544. const targetPosition = op.targetPosition.clone();
  1545. const transformBy = new MoveOperation( new Position( root, [ 8 ] ), 2, new Position( root, [ 1 ] ), 0 );
  1546. const sourcePosition = transformBy.targetPosition.clone();
  1547. const transOp = transform( op, transformBy );
  1548. expect( transOp.length ).to.equal( 1 );
  1549. expectOperation( transOp[ 0 ], {
  1550. type: MoveOperation,
  1551. howMany: 2,
  1552. sourcePosition,
  1553. targetPosition
  1554. } );
  1555. } );
  1556. } );
  1557. } );
  1558. describe( 'NoOperation', () => {
  1559. beforeEach( () => {
  1560. op = new NoOperation( 0 );
  1561. expected = {
  1562. type: NoOperation
  1563. };
  1564. } );
  1565. describe( 'by InsertOperation', () => {
  1566. it( 'no operation update', () => {
  1567. const transformBy = new InsertOperation(
  1568. new Position( root, [ 0 ] ),
  1569. 'a',
  1570. 0
  1571. );
  1572. const transOp = transform( op, transformBy );
  1573. expect( transOp.length ).to.equal( 1 );
  1574. expectOperation( transOp[ 0 ], expected );
  1575. } );
  1576. } );
  1577. describe( 'by AttributeOperation', () => {
  1578. it( 'no operation update', () => {
  1579. const transformBy = new AttributeOperation(
  1580. new Range(
  1581. new Position( root, [ 0 ] ),
  1582. new Position( root, [ 1 ] )
  1583. ),
  1584. 'foo',
  1585. 'bar',
  1586. 'xyz',
  1587. 0
  1588. );
  1589. const transOp = transform( op, transformBy );
  1590. expect( transOp.length ).to.equal( 1 );
  1591. expectOperation( transOp[ 0 ], expected );
  1592. } );
  1593. } );
  1594. describe( 'by RootAttributeOperation', () => {
  1595. it( 'no operation update', () => {
  1596. const transformBy = new RootAttributeOperation(
  1597. root,
  1598. 'foo',
  1599. null,
  1600. 'bar',
  1601. 0
  1602. );
  1603. const transOp = transform( op, transformBy );
  1604. expect( transOp.length ).to.equal( 1 );
  1605. expectOperation( transOp[ 0 ], expected );
  1606. } );
  1607. } );
  1608. describe( 'by MoveOperation', () => {
  1609. it( 'no operation update', () => {
  1610. const transformBy = new MoveOperation(
  1611. new Position( root, [ 0 ] ),
  1612. 2,
  1613. new Position( root, [ 1 ] ),
  1614. 0
  1615. );
  1616. const transOp = transform( op, transformBy );
  1617. expect( transOp.length ).to.equal( 1 );
  1618. expectOperation( transOp[ 0 ], expected );
  1619. } );
  1620. } );
  1621. describe( 'by NoOperation', () => {
  1622. it( 'no operation update', () => {
  1623. const transformBy = new NoOperation( 0 );
  1624. const transOp = transform( op, transformBy );
  1625. expect( transOp.length ).to.equal( 1 );
  1626. expectOperation( transOp[ 0 ], expected );
  1627. } );
  1628. } );
  1629. describe( 'by RenameOperation', () => {
  1630. it( 'no position update', () => {
  1631. const transformBy = new RenameOperation( new Position( root, [ 0, 2, 0 ] ), 'oldName', 'newName', 0 );
  1632. const transOp = transform( op, transformBy );
  1633. expect( transOp.length ).to.equal( 1 );
  1634. expectOperation( transOp[ 0 ], expected );
  1635. } );
  1636. } );
  1637. describe( 'by MarkerOperation', () => {
  1638. it( 'no position update', () => {
  1639. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1640. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1641. const transOp = transform( op, transformBy );
  1642. expect( transOp.length ).to.equal( 1 );
  1643. expectOperation( transOp[ 0 ], expected );
  1644. } );
  1645. } );
  1646. } );
  1647. describe( 'RenameOperation', () => {
  1648. beforeEach( () => {
  1649. const position = new Position( root, [ 0, 2, 2 ] );
  1650. op = new RenameOperation( position, 'oldName', 'newName', 0 );
  1651. expected = {
  1652. position,
  1653. oldName: 'oldName',
  1654. newName: 'newName'
  1655. };
  1656. } );
  1657. describe( 'by InsertOperation', () => {
  1658. it( 'target before renamed element: offset update', () => {
  1659. const transformBy = new InsertOperation(
  1660. new Position( root, [ 0, 2, 1 ] ),
  1661. [ nodeA, nodeB ],
  1662. 0
  1663. );
  1664. const transOp = transform( op, transformBy );
  1665. expect( transOp.length ).to.equal( 1 );
  1666. expected.position.offset = 4;
  1667. expectOperation( transOp[ 0 ], expected );
  1668. } );
  1669. it( 'target after renamed element: no change', () => {
  1670. const transformBy = new InsertOperation(
  1671. new Position( root, [ 0, 2, 3 ] ),
  1672. [ nodeA, nodeB ],
  1673. 0
  1674. );
  1675. const transOp = transform( op, transformBy );
  1676. expect( transOp.length ).to.equal( 1 );
  1677. expectOperation( transOp[ 0 ], expected );
  1678. } );
  1679. it( 'target before a node on path to renamed element: path update', () => {
  1680. const transformBy = new InsertOperation(
  1681. new Position( root, [ 0, 1 ] ),
  1682. [ nodeA, nodeB ],
  1683. 0
  1684. );
  1685. const transOp = transform( op, transformBy );
  1686. expect( transOp.length ).to.equal( 1 );
  1687. expected.position.path = [ 0, 4, 2 ];
  1688. expectOperation( transOp[ 0 ], expected );
  1689. } );
  1690. it( 'target after a node on path to renamed element: no change', () => {
  1691. const transformBy = new InsertOperation(
  1692. new Position( root, [ 0, 3 ] ),
  1693. [ nodeA, nodeB ],
  1694. 0
  1695. );
  1696. const transOp = transform( op, transformBy );
  1697. expect( transOp.length ).to.equal( 1 );
  1698. expectOperation( transOp[ 0 ], expected );
  1699. } );
  1700. } );
  1701. describe( 'by AttributeOperation', () => {
  1702. it( 'no operation update', () => {
  1703. const transformBy = new AttributeOperation(
  1704. new Range(
  1705. new Position( root, [ 0, 2, 1 ] ),
  1706. new Position( root, [ 1, 3 ] )
  1707. ),
  1708. 'foo',
  1709. 'bar',
  1710. 'xyz',
  1711. 0
  1712. );
  1713. const transOp = transform( op, transformBy );
  1714. expect( transOp.length ).to.equal( 1 );
  1715. expectOperation( transOp[ 0 ], expected );
  1716. } );
  1717. } );
  1718. describe( 'by RootAttributeOperation', () => {
  1719. it( 'no operation update', () => {
  1720. const transformBy = new RootAttributeOperation(
  1721. root,
  1722. 'foo',
  1723. null,
  1724. 'bar',
  1725. 0
  1726. );
  1727. const transOp = transform( op, transformBy );
  1728. expect( transOp.length ).to.equal( 1 );
  1729. expectOperation( transOp[ 0 ], expected );
  1730. } );
  1731. } );
  1732. describe( 'by MarkerOperation', () => {
  1733. it( 'no operation update', () => {
  1734. const newRange = new Range( new Position( root, [ 0, 2, 0 ] ), new Position( root, [ 0, 2, 8 ] ) );
  1735. const transformBy = new MarkerOperation( 'name', null, newRange, model.markers, false, 0 );
  1736. const transOp = transform( op, transformBy );
  1737. expect( transOp.length ).to.equal( 1 );
  1738. expectOperation( transOp[ 0 ], expected );
  1739. } );
  1740. } );
  1741. describe( 'by RenameOperation', () => {
  1742. it( 'different element: no change', () => {
  1743. const transformBy = new RenameOperation(
  1744. new Position( root, [ 0, 2, 1 ] ),
  1745. 'foo',
  1746. 'bar',
  1747. 0
  1748. );
  1749. const transOp = transform( op, transformBy );
  1750. expect( transOp.length ).to.equal( 1 );
  1751. expectOperation( transOp[ 0 ], expected );
  1752. } );
  1753. it( 'same element and is important: convert to NoOperation', () => {
  1754. const transformBy = new RenameOperation(
  1755. new Position( root, [ 0, 2, 2 ] ),
  1756. 'oldName',
  1757. 'otherName',
  1758. 0
  1759. );
  1760. const transOp = transform( op, transformBy );
  1761. expect( transOp.length ).to.equal( 1 );
  1762. expectOperation( transOp[ 0 ], {
  1763. type: NoOperation
  1764. } );
  1765. } );
  1766. it( 'same element and is not important: change old name to new name', () => {
  1767. const transformBy = new RenameOperation(
  1768. new Position( root, [ 0, 2, 2 ] ),
  1769. 'oldName',
  1770. 'otherName',
  1771. 0
  1772. );
  1773. const transOp = transform( op, transformBy, strongContext );
  1774. expect( transOp.length ).to.equal( 1 );
  1775. expected.oldName = 'otherName';
  1776. expectOperation( transOp[ 0 ], expected );
  1777. } );
  1778. } );
  1779. describe( 'by MoveOperation', () => {
  1780. it( 'moved range before renamed element: update offset', () => {
  1781. const transformBy = new MoveOperation(
  1782. new Position( root, [ 0, 2, 0 ] ),
  1783. 2,
  1784. new Position( root, [ 2, 5 ] ),
  1785. 0
  1786. );
  1787. const transOp = transform( op, transformBy );
  1788. expect( transOp.length ).to.equal( 1 );
  1789. expected.position.offset = 0;
  1790. expectOperation( transOp[ 0 ], expected );
  1791. } );
  1792. it( 'moved range before an element on path to renamed element: update path', () => {
  1793. const transformBy = new MoveOperation(
  1794. new Position( root, [ 0, 0 ] ),
  1795. 2,
  1796. new Position( root, [ 2, 5 ] ),
  1797. 0
  1798. );
  1799. const transOp = transform( op, transformBy );
  1800. expect( transOp.length ).to.equal( 1 );
  1801. expected.position.path = [ 0, 0, 2 ];
  1802. expectOperation( transOp[ 0 ], expected );
  1803. } );
  1804. it( 'moved range contains renamed element: update path', () => {
  1805. const transformBy = new MoveOperation(
  1806. new Position( root, [ 0, 2, 1 ] ),
  1807. 3,
  1808. new Position( root, [ 2, 5 ] ),
  1809. 0
  1810. );
  1811. const transOp = transform( op, transformBy );
  1812. expect( transOp.length ).to.equal( 1 );
  1813. expected.position.path = [ 2, 6 ];
  1814. expectOperation( transOp[ 0 ], expected );
  1815. } );
  1816. it( 'moved range contains renamed element parent: updated path', () => {
  1817. const transformBy = new MoveOperation(
  1818. new Position( root, [ 0, 1 ] ),
  1819. 3,
  1820. new Position( root, [ 2, 5 ] ),
  1821. 0
  1822. );
  1823. const transOp = transform( op, transformBy );
  1824. expect( transOp.length ).to.equal( 1 );
  1825. expected.position.path = [ 2, 6, 2 ];
  1826. expectOperation( transOp[ 0 ], expected );
  1827. } );
  1828. it( 'move target before renamed element: update offset', () => {
  1829. const transformBy = new MoveOperation(
  1830. new Position( root, [ 2, 5 ] ),
  1831. 2,
  1832. new Position( root, [ 0, 2, 1 ] ),
  1833. 0
  1834. );
  1835. const transOp = transform( op, transformBy );
  1836. expect( transOp.length ).to.equal( 1 );
  1837. expected.position.offset = 4;
  1838. expectOperation( transOp[ 0 ], expected );
  1839. } );
  1840. it( 'move target before an element on path to renamed element: update path', () => {
  1841. const transformBy = new MoveOperation(
  1842. new Position( root, [ 0, 2, 0 ] ),
  1843. 2,
  1844. new Position( root, [ 2, 5 ] ),
  1845. 0
  1846. );
  1847. const transOp = transform( op, transformBy );
  1848. expect( transOp.length ).to.equal( 1 );
  1849. expected.position.offset = 0;
  1850. expectOperation( transOp[ 0 ], expected );
  1851. } );
  1852. } );
  1853. } );
  1854. describe( 'MarkerOperation', () => {
  1855. let oldRange, newRange;
  1856. beforeEach( () => {
  1857. oldRange = new Range( Position._createAt( root, 1 ), Position._createAt( root, 4 ) );
  1858. newRange = new Range( Position._createAt( root, 10 ), Position._createAt( root, 12 ) );
  1859. op = new MarkerOperation( 'name', oldRange, newRange, model.markers, false, 0 );
  1860. expected = {
  1861. name: 'name',
  1862. oldRange,
  1863. newRange
  1864. };
  1865. } );
  1866. describe( 'by InsertOperation', () => {
  1867. it( 'insert position affecting oldRange: update oldRange', () => {
  1868. // Just CC things.
  1869. op.newRange = null;
  1870. const transformBy = new InsertOperation( Position._createAt( root, 0 ), [ nodeA, nodeB ], 0 );
  1871. const transOp = transform( op, transformBy );
  1872. expected.newRange = null;
  1873. expected.oldRange.start.offset = 3;
  1874. expected.oldRange.end.offset = 6;
  1875. expect( transOp.length ).to.equal( 1 );
  1876. expectOperation( transOp[ 0 ], expected );
  1877. } );
  1878. it( 'insert position affecting newRange: update newRange', () => {
  1879. // Just CC things.
  1880. op.oldRange = null;
  1881. const transformBy = new InsertOperation( Position._createAt( root, 8 ), [ nodeA, nodeB ], 0 );
  1882. const transOp = transform( op, transformBy );
  1883. expected.oldRange = null;
  1884. expected.newRange.start.offset = 12;
  1885. expected.newRange.end.offset = 14;
  1886. expect( transOp.length ).to.equal( 1 );
  1887. expectOperation( transOp[ 0 ], expected );
  1888. } );
  1889. } );
  1890. describe( 'by AttributeOperation', () => {
  1891. it( 'no operation update', () => {
  1892. const transformBy = new AttributeOperation(
  1893. new Range(
  1894. new Position( root, [ 2 ] ),
  1895. new Position( root, [ 11 ] )
  1896. ),
  1897. 'foo',
  1898. 'bar',
  1899. 'xyz',
  1900. 0
  1901. );
  1902. const transOp = transform( op, transformBy );
  1903. expect( transOp.length ).to.equal( 1 );
  1904. expectOperation( transOp[ 0 ], expected );
  1905. } );
  1906. } );
  1907. describe( 'by MoveOperation', () => {
  1908. it( 'moved range is before oldRange: update oldRange', () => {
  1909. // Just CC things.
  1910. op.newRange = null;
  1911. const transformBy = new MoveOperation( Position._createAt( root, 0 ), 1, Position._createAt( root, 20 ), 0 );
  1912. const transOp = transform( op, transformBy );
  1913. expected.newRange = null;
  1914. expected.oldRange.start.offset = 0;
  1915. expected.oldRange.end.offset = 3;
  1916. expect( transOp.length ).to.equal( 1 );
  1917. expectOperation( transOp[ 0 ], expected );
  1918. } );
  1919. it( 'moved range contains oldRange and is before newRange: update oldRange and newRange', () => {
  1920. const transformBy = new MoveOperation( Position._createAt( root, 2 ), 2, Position._createAt( root, 20 ), 0 );
  1921. const transOp = transform( op, transformBy );
  1922. expected.oldRange.start.offset = 1;
  1923. expected.oldRange.end.offset = 2;
  1924. expected.newRange.start.offset = 8;
  1925. expected.newRange.end.offset = 10;
  1926. expect( transOp.length ).to.equal( 1 );
  1927. expectOperation( transOp[ 0 ], expected );
  1928. } );
  1929. it( 'target position is inside newRange: update newRange', () => {
  1930. // Just CC things.
  1931. op.oldRange = null;
  1932. const transformBy = new MoveOperation( Position._createAt( root, 20 ), 2, Position._createAt( root, 11 ), 0 );
  1933. const transOp = transform( op, transformBy );
  1934. expected.oldRange = null;
  1935. expected.newRange.start.offset = 10;
  1936. expected.newRange.end.offset = 14;
  1937. expect( transOp.length ).to.equal( 1 );
  1938. expectOperation( transOp[ 0 ], expected );
  1939. } );
  1940. it( 'target position is inside oldRange and before newRange: update oldRange and newRange', () => {
  1941. const transformBy = new MoveOperation( Position._createAt( root, 20 ), 4, Position._createAt( root, 2 ), 0 );
  1942. const transOp = transform( op, transformBy );
  1943. expected.oldRange.start.offset = 1;
  1944. expected.oldRange.end.offset = 8;
  1945. expected.newRange.start.offset = 14;
  1946. expected.newRange.end.offset = 16;
  1947. expect( transOp.length ).to.equal( 1 );
  1948. expectOperation( transOp[ 0 ], expected );
  1949. } );
  1950. } );
  1951. describe( 'by RootAttributeOperation', () => {
  1952. it( 'no operation update', () => {
  1953. const transformBy = new RootAttributeOperation(
  1954. root,
  1955. 'foo',
  1956. null,
  1957. 'bar',
  1958. 0
  1959. );
  1960. const transOp = transform( op, transformBy );
  1961. expect( transOp.length ).to.equal( 1 );
  1962. expectOperation( transOp[ 0 ], expected );
  1963. } );
  1964. } );
  1965. describe( 'by RenameOperation', () => {
  1966. it( 'no operation update', () => {
  1967. const transformBy = new RenameOperation( new Position( root, [ 1 ] ), 'oldName', 'newName', 0 );
  1968. const transOp = transform( op, transformBy );
  1969. expect( transOp.length ).to.equal( 1 );
  1970. expectOperation( transOp[ 0 ], expected );
  1971. } );
  1972. } );
  1973. describe( 'by MarkerOperation', () => {
  1974. it( 'different marker name: no operation update', () => {
  1975. const transformBy = new MarkerOperation( 'otherName', oldRange, newRange, model.markers, false, 0 );
  1976. const transOp = transform( op, transformBy );
  1977. expect( transOp.length ).to.equal( 1 );
  1978. expectOperation( transOp[ 0 ], expected );
  1979. } );
  1980. it( 'same marker name and is important: convert to NoOperation', () => {
  1981. const anotherRange = new Range( Position._createAt( root, 2 ), Position._createAt( root, 2 ) );
  1982. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, false, 0 );
  1983. const transOp = transform( op, transformBy );
  1984. expect( transOp.length ).to.equal( 1 );
  1985. expectOperation( transOp[ 0 ], {
  1986. type: NoOperation
  1987. } );
  1988. } );
  1989. it( 'same marker name and is less important: update oldRange parameter', () => {
  1990. const anotherRange = new Range( Position._createAt( root, 2 ), Position._createAt( root, 2 ) );
  1991. const transformBy = new MarkerOperation( 'name', oldRange, anotherRange, model.markers, false, 0 );
  1992. const transOp = transform( op, transformBy, strongContext );
  1993. expected.oldRange = anotherRange;
  1994. expect( transOp.length ).to.equal( 1 );
  1995. expectOperation( transOp[ 0 ], expected );
  1996. } );
  1997. } );
  1998. } );
  1999. } );
  2000. describe( 'transformSets', () => {
  2001. let model, doc, root, node;
  2002. beforeEach( () => {
  2003. model = new Model();
  2004. doc = model.document;
  2005. root = doc.createRoot();
  2006. node = new Node();
  2007. } );
  2008. it( 'originalOperations should correctly link transformed operations with original operations #1', () => {
  2009. const position = new Position( root, [ 0 ] );
  2010. const a = new InsertOperation( position, [ node ], 0 );
  2011. const { operationsA, originalOperations } = transformSets( [ a ], [], {
  2012. document: doc,
  2013. useRelations: false,
  2014. padWithNoOps: false
  2015. } );
  2016. expect( originalOperations.get( operationsA[ 0 ] ) ).to.equal( a );
  2017. } );
  2018. it( 'originalOperations should correctly link transformed operations with original operations #2', () => {
  2019. const position = new Position( root, [ 0 ] );
  2020. const b = new InsertOperation( position, [ node ], 0 );
  2021. const { operationsB, originalOperations } = transformSets( [], [ b ], {
  2022. document: doc,
  2023. useRelations: false,
  2024. padWithNoOps: false
  2025. } );
  2026. expect( originalOperations.get( operationsB[ 0 ] ) ).to.equal( b );
  2027. } );
  2028. it( 'originalOperations should correctly link transformed operations with original operations #3', () => {
  2029. const position = new Position( root, [ 4 ] );
  2030. const a = new InsertOperation( position, [ node ], 0 );
  2031. const b = new AttributeOperation(
  2032. new Range(
  2033. new Position( root, [ 2 ] ),
  2034. new Position( root, [ 11 ] )
  2035. ),
  2036. 'foo',
  2037. 'bar',
  2038. 'xyz',
  2039. 0
  2040. );
  2041. const { operationsA, operationsB, originalOperations } = transformSets( [ a ], [ b ], {
  2042. document: doc,
  2043. useRelations: false,
  2044. padWithNoOps: false
  2045. } );
  2046. expect( originalOperations.get( operationsA[ 0 ] ) ).to.equal( a );
  2047. expect( originalOperations.get( operationsB[ 0 ] ) ).to.equal( b );
  2048. expect( originalOperations.get( operationsB[ 1 ] ) ).to.equal( b );
  2049. } );
  2050. } );