transform.js 70 KB

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