8
0

transform.js 71 KB

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