splitdelta.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import transformations from '../../../../src/model/delta/basic-transformations'; // eslint-disable-line no-unused-vars
  6. import deltaTransform from '../../../../src/model/delta/transform';
  7. const transform = deltaTransform.transform;
  8. import Element from '../../../../src/model/element';
  9. import Position from '../../../../src/model/position';
  10. import Range from '../../../../src/model/range';
  11. import Delta from '../../../../src/model/delta/delta';
  12. import SplitDelta from '../../../../src/model/delta/splitdelta';
  13. import AttributeDelta from '../../../../src/model/delta/attributedelta';
  14. import RenameDelta from '../../../../src/model/delta/renamedelta';
  15. import InsertOperation from '../../../../src/model/operation/insertoperation';
  16. import AttributeOperation from '../../../../src/model/operation/attributeoperation';
  17. import ReinsertOperation from '../../../../src/model/operation/reinsertoperation';
  18. import MoveOperation from '../../../../src/model/operation/moveoperation';
  19. import NoOperation from '../../../../src/model/operation/nooperation';
  20. import RenameOperation from '../../../../src/model/operation/renameoperation';
  21. import { getNodesAndText } from '../../../../tests/model/_utils/utils';
  22. import {
  23. applyDelta,
  24. expectDelta,
  25. getFilledDocument,
  26. getSplitDelta,
  27. getWrapDelta,
  28. getUnwrapDelta,
  29. getRemoveDelta
  30. } from '../../../../tests/model/delta/transform/_utils/utils';
  31. describe( 'transform', () => {
  32. let doc, root, gy, baseVersion, context;
  33. beforeEach( () => {
  34. doc = getFilledDocument();
  35. root = doc.getRoot();
  36. gy = doc.graveyard;
  37. baseVersion = doc.version;
  38. context = { isStrong: false };
  39. } );
  40. describe( 'SplitDelta by', () => {
  41. let splitDelta, splitPosition;
  42. beforeEach( () => {
  43. splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  44. splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  45. } );
  46. describe( 'SplitDelta', () => {
  47. it( 'split in same parent and offset', () => {
  48. const splitDeltaB = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  49. const transformed = transform( splitDelta, splitDeltaB, context );
  50. baseVersion = splitDeltaB.operations.length;
  51. expect( transformed.length ).to.equal( 1 );
  52. expectDelta( transformed[ 0 ], {
  53. type: SplitDelta,
  54. operations: [
  55. {
  56. type: InsertOperation,
  57. position: new Position( root, [ 3, 3, 4 ] ),
  58. baseVersion
  59. },
  60. {
  61. type: NoOperation,
  62. baseVersion: baseVersion + 1
  63. }
  64. ]
  65. } );
  66. // Test if deltas do what they should after applying transformed delta.
  67. applyDelta( splitDeltaB, doc );
  68. applyDelta( transformed[ 0 ], doc );
  69. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  70. // Incoming split delta is discarded. Only one new element is created after applying both split deltas.
  71. // There are no empty P elements.
  72. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPPPfoobarxyzP' );
  73. } );
  74. it( 'should not change context.insertBefore if it was set', () => {
  75. // SplitDelta x SplitDelta transformation case sets `context.insertBefore` on its own if it is not set.
  76. // It is to achieve better transformation results from UX point of view.
  77. // However, if `context.insertBefore` was already set, it should not be changed. This might be important for undo.
  78. const splitDeltaB = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  79. const transformed = transform( splitDelta, splitDeltaB, {
  80. isStrong: false,
  81. insertBefore: true
  82. } );
  83. baseVersion = splitDeltaB.operations.length;
  84. expect( transformed.length ).to.equal( 1 );
  85. expectDelta( transformed[ 0 ], {
  86. type: SplitDelta,
  87. operations: [
  88. {
  89. type: InsertOperation,
  90. // If `context.insertBefore` would not be set, the offset would be 4. See an example above.
  91. position: new Position( root, [ 3, 3, 5 ] ),
  92. baseVersion
  93. },
  94. {
  95. type: NoOperation,
  96. baseVersion: baseVersion + 1
  97. }
  98. ]
  99. } );
  100. } );
  101. it( 'split in same parent, incoming delta splits closer', () => {
  102. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
  103. const transformed = transform( splitDelta, splitDeltaB, context );
  104. baseVersion = splitDeltaB.operations.length;
  105. expect( transformed.length ).to.equal( 1 );
  106. expectDelta( transformed[ 0 ], {
  107. type: SplitDelta,
  108. operations: [
  109. {
  110. type: InsertOperation,
  111. position: new Position( root, [ 3, 3, 4 ] ),
  112. baseVersion
  113. },
  114. {
  115. type: MoveOperation,
  116. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  117. howMany: 2,
  118. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  119. baseVersion: baseVersion + 1
  120. }
  121. ]
  122. } );
  123. // Test if deltas do what they should after applying transformed delta.
  124. applyDelta( splitDeltaB, doc );
  125. applyDelta( transformed[ 0 ], doc );
  126. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  127. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  128. expect( nodesAndText ).to.equal( 'XXXXXabcdXPabcPPfoPPobarxyzP' );
  129. } );
  130. it( 'split in same parent, incoming delta splits closer, split deltas have reinsert operations', () => {
  131. let reOp = new ReinsertOperation(
  132. new Position( gy, [ 1 ] ),
  133. 1,
  134. Position.createFromPosition( splitDelta.operations[ 0 ].position ),
  135. splitDelta.operations[ 0 ].baseVersion
  136. );
  137. splitDelta.operations[ 0 ] = reOp;
  138. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 5 ] ), new Element( 'p' ), 7, baseVersion );
  139. reOp = new ReinsertOperation(
  140. new Position( gy, [ 0 ] ),
  141. 1,
  142. Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
  143. splitDeltaB.operations[ 0 ].baseVersion
  144. );
  145. splitDeltaB.operations[ 0 ] = reOp;
  146. const transformed = transform( splitDelta, splitDeltaB, context );
  147. baseVersion = splitDeltaB.operations.length;
  148. expect( transformed.length ).to.equal( 1 );
  149. expectDelta( transformed[ 0 ], {
  150. type: SplitDelta,
  151. operations: [
  152. {
  153. type: ReinsertOperation,
  154. sourcePosition: new Position( gy, [ 0 ] ),
  155. howMany: 1,
  156. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  157. baseVersion
  158. },
  159. {
  160. type: MoveOperation,
  161. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  162. howMany: 2,
  163. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  164. baseVersion: baseVersion + 1
  165. }
  166. ]
  167. } );
  168. } );
  169. it( 'split in same parent, incoming delta splits further', () => {
  170. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  171. const transformed = transform( splitDelta, splitDeltaB, context );
  172. baseVersion = splitDeltaB.operations.length;
  173. expect( transformed.length ).to.equal( 1 );
  174. expectDelta( transformed[ 0 ], {
  175. type: SplitDelta,
  176. operations: [
  177. {
  178. type: InsertOperation,
  179. position: new Position( root, [ 3, 3, 5 ] ),
  180. baseVersion
  181. },
  182. {
  183. type: MoveOperation,
  184. sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
  185. howMany: 9,
  186. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  187. baseVersion: baseVersion + 1
  188. }
  189. ]
  190. } );
  191. // Test if deltas do what they should after applying transformed delta.
  192. applyDelta( splitDeltaB, doc );
  193. applyDelta( transformed[ 0 ], doc );
  194. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  195. // P element is correctly split, there are three P elements, letters in P elements are in correct order.
  196. expect( nodesAndText ).to.equal( 'XXXXXabcdXPaPPbcPPfoobarxyzP' );
  197. } );
  198. it( 'split in same parent, incoming delta splits further, split deltas have reinsert operations', () => {
  199. let reOp = new ReinsertOperation(
  200. new Position( gy, [ 1 ] ),
  201. 1,
  202. Position.createFromPosition( splitDelta.operations[ 0 ].position ),
  203. splitDelta.operations[ 0 ].baseVersion
  204. );
  205. splitDelta.operations[ 0 ] = reOp;
  206. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  207. reOp = new ReinsertOperation(
  208. new Position( gy, [ 0 ] ),
  209. 1,
  210. Position.createFromPosition( splitDeltaB.operations[ 0 ].position ),
  211. splitDeltaB.operations[ 0 ].baseVersion
  212. );
  213. splitDeltaB.operations[ 0 ] = reOp;
  214. const transformed = transform( splitDelta, splitDeltaB, context );
  215. baseVersion = splitDeltaB.operations.length;
  216. expect( transformed.length ).to.equal( 1 );
  217. expectDelta( transformed[ 0 ], {
  218. type: SplitDelta,
  219. operations: [
  220. {
  221. type: ReinsertOperation,
  222. sourcePosition: new Position( gy, [ 0 ] ),
  223. howMany: 1,
  224. targetPosition: new Position( root, [ 3, 3, 5 ] ),
  225. baseVersion
  226. },
  227. {
  228. type: MoveOperation,
  229. sourcePosition: new Position( root, [ 3, 3, 4, 2 ] ),
  230. howMany: 9,
  231. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  232. baseVersion: baseVersion + 1
  233. }
  234. ]
  235. } );
  236. } );
  237. it( 'split in split parent', () => {
  238. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3 ] ), new Element( 'div' ), 1, baseVersion );
  239. const transformed = transform( splitDelta, splitDeltaB, context );
  240. baseVersion = splitDeltaB.operations.length;
  241. expect( transformed.length ).to.equal( 1 );
  242. expectDelta( transformed[ 0 ], {
  243. type: SplitDelta,
  244. operations: [
  245. {
  246. type: InsertOperation,
  247. position: new Position( root, [ 3, 4, 1 ] ),
  248. baseVersion
  249. },
  250. {
  251. type: MoveOperation,
  252. sourcePosition: new Position( root, [ 3, 4, 0, 3 ] ),
  253. howMany: 9,
  254. targetPosition: new Position( root, [ 3, 4, 1, 0 ] ),
  255. baseVersion: baseVersion + 1
  256. }
  257. ]
  258. } );
  259. // Test if deltas do what they should after applying transformed delta.
  260. applyDelta( splitDeltaB, doc );
  261. applyDelta( transformed[ 0 ], doc );
  262. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  263. // DIV and P elements are correctly split.
  264. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVPabcPPfoobarxyzPDIV' );
  265. } );
  266. } );
  267. describe( 'UnwrapDelta', () => {
  268. it( 'split position directly in unwrapped node', () => {
  269. const unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  270. const transformed = transform( splitDelta, unwrapDelta, context );
  271. baseVersion = unwrapDelta.operations.length;
  272. expect( transformed.length ).to.equal( 1 );
  273. expectDelta( transformed[ 0 ], {
  274. type: Delta,
  275. operations: [
  276. {
  277. type: NoOperation,
  278. baseVersion
  279. }
  280. ]
  281. } );
  282. // Test if deltas do what they should after applying transformed delta.
  283. applyDelta( unwrapDelta, doc );
  284. applyDelta( transformed[ 0 ], doc );
  285. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  286. // UnwrapDelta is applied. SplitDelta is discarded.
  287. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  288. } );
  289. it( 'split position indirectly in unwrapped node', () => {
  290. const unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
  291. const transformed = transform( splitDelta, unwrapDelta, context );
  292. expect( transformed.length ).to.equal( 1 );
  293. baseVersion = unwrapDelta.operations.length;
  294. expectDelta( transformed[ 0 ], {
  295. type: SplitDelta,
  296. operations: [
  297. {
  298. type: InsertOperation,
  299. position: new Position( root, [ 3, 7 ] ),
  300. baseVersion
  301. },
  302. {
  303. type: MoveOperation,
  304. sourcePosition: new Position( root, [ 3, 6, 3 ] ),
  305. howMany: 9,
  306. targetPosition: new Position( root, [ 3, 7, 0 ] ),
  307. baseVersion: baseVersion + 1
  308. }
  309. ]
  310. } );
  311. // Test if deltas do what they should after applying transformed delta.
  312. applyDelta( unwrapDelta, doc );
  313. applyDelta( transformed[ 0 ], doc );
  314. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3 ] ), 1 ) );
  315. // UnwrapDelta and SplitDelta are correctly applied.
  316. expect( nodesAndText ).to.equal( 'DIVXXXXXaXXXXXXabcdXPabcPPfoobarxyzPDIV' );
  317. } );
  318. } );
  319. describe( 'WrapDelta', () => {
  320. it( 'split position is between wrapped nodes', () => {
  321. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  322. const wrapElement = new Element( 'E' );
  323. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  324. const transformed = transform( splitDelta, wrapDelta, context );
  325. baseVersion = wrapDelta.operations.length;
  326. expect( transformed.length ).to.equal( 1 );
  327. expectDelta( transformed[ 0 ], {
  328. type: Delta,
  329. operations: [
  330. {
  331. type: NoOperation,
  332. baseVersion
  333. }
  334. ]
  335. } );
  336. // Test if deltas do what they should after applying transformed delta.
  337. applyDelta( wrapDelta, doc );
  338. applyDelta( transformed[ 0 ], doc );
  339. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
  340. // WrapDelta is applied. SplitDelta is discarded.
  341. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  342. } );
  343. it( 'split position is before wrapped nodes', () => {
  344. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 5 ] ), new Position( root, [ 3, 3, 3, 7 ] ) );
  345. const wrapElement = new Element( 'E' );
  346. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  347. const transformed = transform( splitDelta, wrapDelta, context );
  348. expect( transformed.length ).to.equal( 1 );
  349. baseVersion = wrapDelta.operations.length;
  350. expectDelta( transformed[ 0 ], {
  351. type: SplitDelta,
  352. operations: [
  353. {
  354. type: InsertOperation,
  355. position: new Position( root, [ 3, 3, 4 ] ),
  356. baseVersion
  357. },
  358. {
  359. type: MoveOperation,
  360. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  361. howMany: 8,
  362. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  363. baseVersion: baseVersion + 1
  364. }
  365. ]
  366. } );
  367. // Test if deltas do what they should after applying transformed delta.
  368. applyDelta( wrapDelta, doc );
  369. applyDelta( transformed[ 0 ], doc );
  370. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  371. // WrapDelta and SplitDelta are correctly applied.
  372. expect( nodesAndText ).to.equal( 'PabcPPfoEobEarxyzP' );
  373. } );
  374. it( 'split position is inside wrapped node', () => {
  375. const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  376. const wrapElement = new Element( 'E' );
  377. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  378. const transformed = transform( splitDelta, wrapDelta, context );
  379. expect( transformed.length ).to.equal( 1 );
  380. baseVersion = wrapDelta.operations.length;
  381. expectDelta( transformed[ 0 ], {
  382. type: SplitDelta,
  383. operations: [
  384. {
  385. type: InsertOperation,
  386. position: new Position( root, [ 3, 3, 2, 2 ] ),
  387. baseVersion
  388. },
  389. {
  390. type: MoveOperation,
  391. sourcePosition: new Position( root, [ 3, 3, 2, 1, 3 ] ),
  392. howMany: 9,
  393. targetPosition: new Position( root, [ 3, 3, 2, 2, 0 ] ),
  394. baseVersion: baseVersion + 1
  395. }
  396. ]
  397. } );
  398. // Test if deltas do what they should after applying transformed delta.
  399. applyDelta( wrapDelta, doc );
  400. applyDelta( transformed[ 0 ], doc );
  401. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  402. // WrapDelta and SplitDelta are correctly applied.
  403. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  404. } );
  405. } );
  406. describe( 'AttributeDelta', () => {
  407. it( 'attribute changed on split element', () => {
  408. const attributeDelta = new AttributeDelta();
  409. attributeDelta.addOperation( new AttributeOperation(
  410. Range.createFromParentsAndOffsets( root, 0, root, 2 ), 'key', 'oldValue', 'newValue', baseVersion
  411. ) );
  412. attributeDelta.addOperation( new AttributeOperation(
  413. Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 3 ), 'key', null, 'newValue', baseVersion + 1
  414. ) );
  415. const transformed = transform( splitDelta, attributeDelta, context );
  416. baseVersion = attributeDelta.operations.length;
  417. expect( transformed.length ).to.equal( 1 );
  418. expectDelta( transformed[ 0 ], {
  419. type: SplitDelta,
  420. operations: [
  421. {
  422. type: InsertOperation,
  423. position: new Position( root, [ 3, 3, 4 ] ),
  424. baseVersion
  425. },
  426. {
  427. type: MoveOperation,
  428. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  429. howMany: 9,
  430. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  431. baseVersion: baseVersion + 1
  432. }
  433. ]
  434. } );
  435. expect( transformed[ 0 ].operations[ 0 ].nodes.getNode( 0 ).getAttribute( 'key' ) ).to.equal( 'newValue' );
  436. } );
  437. it( 'attribute removed from split element', () => {
  438. splitDelta.operations[ 0 ].nodes.getNode( 0 ).setAttribute( 'key', 'oldValue' );
  439. const attributeDelta = new AttributeDelta();
  440. attributeDelta.addOperation( new AttributeOperation(
  441. Range.createFromParentsAndOffsets( root, 0, root, 2 ), 'key', 'otherValue', null, baseVersion
  442. ) );
  443. attributeDelta.addOperation( new AttributeOperation(
  444. Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 3 ), 'key', 'oldValue', null, baseVersion + 1
  445. ) );
  446. const transformed = transform( splitDelta, attributeDelta, context );
  447. baseVersion = attributeDelta.operations.length;
  448. expect( transformed.length ).to.equal( 1 );
  449. expectDelta( transformed[ 0 ], {
  450. type: SplitDelta,
  451. operations: [
  452. {
  453. type: InsertOperation,
  454. position: new Position( root, [ 3, 3, 4 ] ),
  455. baseVersion
  456. },
  457. {
  458. type: MoveOperation,
  459. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  460. howMany: 9,
  461. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  462. baseVersion: baseVersion + 1
  463. }
  464. ]
  465. } );
  466. expect( transformed[ 0 ].operations[ 0 ].nodes.getNode( 0 ).hasAttribute( 'key' ) ).to.be.false;
  467. } );
  468. it( 'attribute changed on split element that is reinserted from graveyard', () => {
  469. splitDelta.operations[ 0 ] = new ReinsertOperation(
  470. new Position( gy, [ 1 ] ),
  471. 1,
  472. new Position( root, [ 3, 3, 4 ] ),
  473. baseVersion
  474. );
  475. const attributeDelta = new AttributeDelta();
  476. attributeDelta.addOperation( new AttributeOperation(
  477. Range.createFromParentsAndOffsets( root, 0, root, 4 ), 'key', 'oldValue', 'newValue', baseVersion
  478. ) );
  479. const transformed = transform( splitDelta, attributeDelta, context );
  480. baseVersion = attributeDelta.operations.length;
  481. expect( transformed.length ).to.equal( 1 );
  482. expectDelta( transformed[ 0 ], {
  483. type: SplitDelta,
  484. operations: [
  485. {
  486. type: ReinsertOperation,
  487. sourcePosition: new Position( gy, [ 1 ] ),
  488. howMany: 1,
  489. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  490. baseVersion
  491. },
  492. {
  493. type: MoveOperation,
  494. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  495. howMany: 9,
  496. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  497. baseVersion: baseVersion + 1
  498. }
  499. ]
  500. } );
  501. } );
  502. } );
  503. describe( 'RenameDelta', () => {
  504. it( 'renamed split element', () => {
  505. const renameDelta = new RenameDelta();
  506. renameDelta.addOperation( new RenameOperation(
  507. new Position( root, [ 3, 3, 3 ] ), 'h2', 'li', baseVersion
  508. ) );
  509. const transformed = transform( splitDelta, renameDelta, context );
  510. baseVersion = renameDelta.operations.length;
  511. expect( transformed.length ).to.equal( 2 );
  512. expectDelta( transformed[ 0 ], {
  513. type: SplitDelta,
  514. operations: [
  515. {
  516. type: InsertOperation,
  517. position: new Position( root, [ 3, 3, 4 ] ),
  518. baseVersion
  519. },
  520. {
  521. type: MoveOperation,
  522. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  523. howMany: 9,
  524. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  525. baseVersion: baseVersion + 1
  526. }
  527. ]
  528. } );
  529. expectDelta( transformed[ 1 ], {
  530. type: RenameDelta,
  531. operations: [
  532. {
  533. type: RenameOperation,
  534. position: new Position( root, [ 3, 3, 4 ] ),
  535. oldName: 'p', // `oldName` taken from SplitDelta.
  536. newName: 'li',
  537. baseVersion: baseVersion + 2
  538. }
  539. ]
  540. } );
  541. } );
  542. it( 'split element is different than renamed element', () => {
  543. const renameDelta = new RenameDelta();
  544. renameDelta.addOperation( new RenameOperation(
  545. new Position( root, [ 4 ] ), 'p', 'li', baseVersion
  546. ) );
  547. const transformed = transform( splitDelta, renameDelta, context );
  548. baseVersion = renameDelta.operations.length;
  549. expect( transformed.length ).to.equal( 1 );
  550. expectDelta( transformed[ 0 ], {
  551. type: SplitDelta,
  552. operations: [
  553. {
  554. type: InsertOperation,
  555. position: new Position( root, [ 3, 3, 4 ] ),
  556. baseVersion
  557. },
  558. {
  559. type: MoveOperation,
  560. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  561. howMany: 9,
  562. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  563. baseVersion: baseVersion + 1
  564. }
  565. ]
  566. } );
  567. } );
  568. it( 'renamed split element that is reinserted from graveyard', () => {
  569. splitDelta.operations[ 0 ] = new ReinsertOperation(
  570. new Position( gy, [ 1 ] ),
  571. 1,
  572. new Position( root, [ 3, 3, 4 ] ),
  573. baseVersion
  574. );
  575. const renameDelta = new RenameDelta();
  576. renameDelta.addOperation( new RenameOperation(
  577. new Position( root, [ 3, 3, 3 ] ), 'p', 'li', baseVersion
  578. ) );
  579. context.aWasUndone = true;
  580. const transformed = transform( splitDelta, renameDelta, context );
  581. baseVersion = renameDelta.operations.length;
  582. expect( transformed.length ).to.equal( 1 );
  583. expectDelta( transformed[ 0 ], {
  584. type: SplitDelta,
  585. operations: [
  586. {
  587. type: ReinsertOperation,
  588. sourcePosition: new Position( gy, [ 1 ] ),
  589. howMany: 1,
  590. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  591. baseVersion
  592. },
  593. {
  594. type: MoveOperation,
  595. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  596. howMany: 9,
  597. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  598. baseVersion: baseVersion + 1
  599. }
  600. ]
  601. } );
  602. } );
  603. } );
  604. describe( 'RemoveDelta', () => {
  605. it( 'node inside the removed range was a node that has been split', () => {
  606. splitPosition = new Position( root, [ 3, 3, 2, 2 ] );
  607. splitDelta = getSplitDelta( splitPosition, new Element( 'x' ), 2, baseVersion );
  608. const removePosition = new Position( root, [ 3, 3, 1 ] );
  609. const removeDelta = getRemoveDelta( removePosition, 3, baseVersion );
  610. const removeOperation = removeDelta.operations[ 0 ];
  611. const transformed = transform( splitDelta, removeDelta, context );
  612. expect( transformed.length ).to.equal( 1 );
  613. baseVersion = removeDelta.operations.length;
  614. const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
  615. const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 );
  616. newMoveSourcePosition.path.push( 2 );
  617. const newMoveTargetPosition = Position.createAt( newInsertPosition );
  618. newMoveTargetPosition.path.push( 0 );
  619. expectDelta( transformed[ 0 ], {
  620. type: SplitDelta,
  621. operations: [
  622. {
  623. type: InsertOperation,
  624. position: newInsertPosition,
  625. baseVersion
  626. },
  627. {
  628. type: MoveOperation,
  629. sourcePosition: newMoveSourcePosition,
  630. howMany: 2,
  631. targetPosition: newMoveTargetPosition,
  632. baseVersion: baseVersion + 1
  633. }
  634. ]
  635. } );
  636. } );
  637. it( 'last node in the removed range was a node that has been split', () => {
  638. const removePosition = new Position( root, [ 3, 3, 2 ] );
  639. const removeDelta = getRemoveDelta( removePosition, 2, baseVersion );
  640. const removeOperation = removeDelta.operations[ 0 ];
  641. const transformed = transform( splitDelta, removeDelta, context );
  642. expect( transformed.length ).to.equal( 1 );
  643. baseVersion = removeDelta.operations.length;
  644. const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
  645. const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 );
  646. newMoveSourcePosition.path.push( 3 );
  647. const newMoveTargetPosition = Position.createAt( newInsertPosition );
  648. newMoveTargetPosition.path.push( 0 );
  649. expectDelta( transformed[ 0 ], {
  650. type: SplitDelta,
  651. operations: [
  652. {
  653. type: InsertOperation,
  654. position: newInsertPosition,
  655. baseVersion
  656. },
  657. {
  658. type: MoveOperation,
  659. sourcePosition: newMoveSourcePosition,
  660. howMany: 9,
  661. targetPosition: newMoveTargetPosition,
  662. baseVersion: baseVersion + 1
  663. }
  664. ]
  665. } );
  666. } );
  667. } );
  668. } );
  669. } );