transform.js 71 KB

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