splitdelta.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. it( 'should use default algorithm and not throw if transformed split delta has NoOperation', () => {
  267. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  268. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  269. const transformed = transform( splitDelta, splitDeltaB, context );
  270. baseVersion = splitDeltaB.operations.length;
  271. expect( transformed.length ).to.equal( 1 );
  272. expectDelta( transformed[ 0 ], {
  273. type: SplitDelta,
  274. operations: [
  275. {
  276. type: InsertOperation,
  277. position: new Position( root, [ 3, 3, 5 ] ),
  278. baseVersion
  279. },
  280. {
  281. type: NoOperation,
  282. baseVersion: baseVersion + 1
  283. }
  284. ]
  285. } );
  286. } );
  287. it( 'should use default algorithm and not throw if split delta to transform by has NoOperation', () => {
  288. const splitDeltaB = getSplitDelta( new Position( root, [ 3, 3, 3, 1 ] ), new Element( 'p' ), 11, baseVersion );
  289. splitDeltaB.operations[ 1 ] = new NoOperation( 1 );
  290. const transformed = transform( splitDelta, splitDeltaB, context );
  291. baseVersion = splitDeltaB.operations.length;
  292. expect( transformed.length ).to.equal( 1 );
  293. expectDelta( transformed[ 0 ], {
  294. type: SplitDelta,
  295. operations: [
  296. {
  297. type: InsertOperation,
  298. position: new Position( root, [ 3, 3, 5 ] ),
  299. baseVersion
  300. },
  301. {
  302. type: MoveOperation,
  303. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  304. howMany: 9,
  305. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  306. baseVersion: baseVersion + 1
  307. }
  308. ]
  309. } );
  310. } );
  311. } );
  312. describe( 'UnwrapDelta', () => {
  313. it( 'split position directly in unwrapped node', () => {
  314. const unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  315. const transformed = transform( splitDelta, unwrapDelta, context );
  316. baseVersion = unwrapDelta.operations.length;
  317. expect( transformed.length ).to.equal( 1 );
  318. expectDelta( transformed[ 0 ], {
  319. type: Delta,
  320. operations: [
  321. {
  322. type: NoOperation,
  323. baseVersion
  324. }
  325. ]
  326. } );
  327. // Test if deltas do what they should after applying transformed delta.
  328. applyDelta( unwrapDelta, doc );
  329. applyDelta( transformed[ 0 ], doc );
  330. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  331. // UnwrapDelta is applied. SplitDelta is discarded.
  332. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  333. } );
  334. it( 'split position indirectly in unwrapped node', () => {
  335. const unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
  336. const transformed = transform( splitDelta, unwrapDelta, context );
  337. expect( transformed.length ).to.equal( 1 );
  338. baseVersion = unwrapDelta.operations.length;
  339. expectDelta( transformed[ 0 ], {
  340. type: SplitDelta,
  341. operations: [
  342. {
  343. type: InsertOperation,
  344. position: new Position( root, [ 3, 7 ] ),
  345. baseVersion
  346. },
  347. {
  348. type: MoveOperation,
  349. sourcePosition: new Position( root, [ 3, 6, 3 ] ),
  350. howMany: 9,
  351. targetPosition: new Position( root, [ 3, 7, 0 ] ),
  352. baseVersion: baseVersion + 1
  353. }
  354. ]
  355. } );
  356. // Test if deltas do what they should after applying transformed delta.
  357. applyDelta( unwrapDelta, doc );
  358. applyDelta( transformed[ 0 ], doc );
  359. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3 ] ), 1 ) );
  360. // UnwrapDelta and SplitDelta are correctly applied.
  361. expect( nodesAndText ).to.equal( 'DIVXXXXXaXXXXXXabcdXPabcPPfoobarxyzPDIV' );
  362. } );
  363. it( 'should use default algorithm and not throw if split delta has NoOperation', () => {
  364. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  365. const unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3 ] ), 4, baseVersion );
  366. const transformed = transform( splitDelta, unwrapDelta, context );
  367. expect( transformed.length ).to.equal( 1 );
  368. baseVersion = unwrapDelta.operations.length;
  369. expectDelta( transformed[ 0 ], {
  370. type: SplitDelta,
  371. operations: [
  372. {
  373. type: InsertOperation,
  374. position: new Position( root, [ 3, 7 ] ),
  375. baseVersion
  376. },
  377. {
  378. type: NoOperation,
  379. baseVersion: baseVersion + 1
  380. }
  381. ]
  382. } );
  383. } );
  384. } );
  385. describe( 'WrapDelta', () => {
  386. it( 'split position is between wrapped nodes', () => {
  387. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  388. const wrapElement = new Element( 'E' );
  389. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  390. const transformed = transform( splitDelta, wrapDelta, context );
  391. baseVersion = wrapDelta.operations.length;
  392. expect( transformed.length ).to.equal( 1 );
  393. expectDelta( transformed[ 0 ], {
  394. type: Delta,
  395. operations: [
  396. {
  397. type: NoOperation,
  398. baseVersion
  399. }
  400. ]
  401. } );
  402. // Test if deltas do what they should after applying transformed delta.
  403. applyDelta( wrapDelta, doc );
  404. applyDelta( transformed[ 0 ], doc );
  405. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
  406. // WrapDelta is applied. SplitDelta is discarded.
  407. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  408. } );
  409. it( 'split position is before wrapped nodes', () => {
  410. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 5 ] ), new Position( root, [ 3, 3, 3, 7 ] ) );
  411. const wrapElement = new Element( 'E' );
  412. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  413. const transformed = transform( splitDelta, wrapDelta, context );
  414. expect( transformed.length ).to.equal( 1 );
  415. baseVersion = wrapDelta.operations.length;
  416. expectDelta( transformed[ 0 ], {
  417. type: SplitDelta,
  418. operations: [
  419. {
  420. type: InsertOperation,
  421. position: new Position( root, [ 3, 3, 4 ] ),
  422. baseVersion
  423. },
  424. {
  425. type: MoveOperation,
  426. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  427. howMany: 8,
  428. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  429. baseVersion: baseVersion + 1
  430. }
  431. ]
  432. } );
  433. // Test if deltas do what they should after applying transformed delta.
  434. applyDelta( wrapDelta, doc );
  435. applyDelta( transformed[ 0 ], doc );
  436. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  437. // WrapDelta and SplitDelta are correctly applied.
  438. expect( nodesAndText ).to.equal( 'PabcPPfoEobEarxyzP' );
  439. } );
  440. it( 'split position is inside wrapped node', () => {
  441. const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  442. const wrapElement = new Element( 'E' );
  443. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  444. const transformed = transform( splitDelta, wrapDelta, context );
  445. expect( transformed.length ).to.equal( 1 );
  446. baseVersion = wrapDelta.operations.length;
  447. expectDelta( transformed[ 0 ], {
  448. type: SplitDelta,
  449. operations: [
  450. {
  451. type: InsertOperation,
  452. position: new Position( root, [ 3, 3, 2, 2 ] ),
  453. baseVersion
  454. },
  455. {
  456. type: MoveOperation,
  457. sourcePosition: new Position( root, [ 3, 3, 2, 1, 3 ] ),
  458. howMany: 9,
  459. targetPosition: new Position( root, [ 3, 3, 2, 2, 0 ] ),
  460. baseVersion: baseVersion + 1
  461. }
  462. ]
  463. } );
  464. // Test if deltas do what they should after applying transformed delta.
  465. applyDelta( wrapDelta, doc );
  466. applyDelta( transformed[ 0 ], doc );
  467. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  468. // WrapDelta and SplitDelta are correctly applied.
  469. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  470. } );
  471. it( 'should use default algorithm and not throw if split delta has NoOperation', () => {
  472. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  473. const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  474. const wrapElement = new Element( 'E' );
  475. const wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  476. const transformed = transform( splitDelta, wrapDelta, context );
  477. expect( transformed.length ).to.equal( 1 );
  478. baseVersion = wrapDelta.operations.length;
  479. expectDelta( transformed[ 0 ], {
  480. type: SplitDelta,
  481. operations: [
  482. {
  483. type: InsertOperation,
  484. position: new Position( root, [ 3, 3, 3 ] ),
  485. baseVersion
  486. },
  487. {
  488. type: NoOperation,
  489. baseVersion: baseVersion + 1
  490. }
  491. ]
  492. } );
  493. } );
  494. } );
  495. describe( 'AttributeDelta', () => {
  496. it( 'attribute changed on split element', () => {
  497. const attributeDelta = new AttributeDelta();
  498. attributeDelta.addOperation( new AttributeOperation(
  499. Range.createFromParentsAndOffsets( root, 0, root, 2 ), 'key', 'oldValue', 'newValue', baseVersion
  500. ) );
  501. attributeDelta.addOperation( new AttributeOperation(
  502. Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 3 ), 'key', null, 'newValue', baseVersion + 1
  503. ) );
  504. const transformed = transform( splitDelta, attributeDelta, context );
  505. baseVersion = attributeDelta.operations.length;
  506. expect( transformed.length ).to.equal( 1 );
  507. expectDelta( transformed[ 0 ], {
  508. type: SplitDelta,
  509. operations: [
  510. {
  511. type: InsertOperation,
  512. position: new Position( root, [ 3, 3, 4 ] ),
  513. baseVersion
  514. },
  515. {
  516. type: MoveOperation,
  517. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  518. howMany: 9,
  519. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  520. baseVersion: baseVersion + 1
  521. }
  522. ]
  523. } );
  524. expect( transformed[ 0 ].operations[ 0 ].nodes.getNode( 0 ).getAttribute( 'key' ) ).to.equal( 'newValue' );
  525. } );
  526. it( 'attribute removed from split element', () => {
  527. splitDelta.operations[ 0 ].nodes.getNode( 0 ).setAttribute( 'key', 'oldValue' );
  528. const attributeDelta = new AttributeDelta();
  529. attributeDelta.addOperation( new AttributeOperation(
  530. Range.createFromParentsAndOffsets( root, 0, root, 2 ), 'key', 'otherValue', null, baseVersion
  531. ) );
  532. attributeDelta.addOperation( new AttributeOperation(
  533. Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 3 ), 'key', 'oldValue', null, baseVersion + 1
  534. ) );
  535. const transformed = transform( splitDelta, attributeDelta, context );
  536. baseVersion = attributeDelta.operations.length;
  537. expect( transformed.length ).to.equal( 1 );
  538. expectDelta( transformed[ 0 ], {
  539. type: SplitDelta,
  540. operations: [
  541. {
  542. type: InsertOperation,
  543. position: new Position( root, [ 3, 3, 4 ] ),
  544. baseVersion
  545. },
  546. {
  547. type: MoveOperation,
  548. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  549. howMany: 9,
  550. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  551. baseVersion: baseVersion + 1
  552. }
  553. ]
  554. } );
  555. expect( transformed[ 0 ].operations[ 0 ].nodes.getNode( 0 ).hasAttribute( 'key' ) ).to.be.false;
  556. } );
  557. it( 'attribute changed on split element that is reinserted from graveyard', () => {
  558. splitDelta.operations[ 0 ] = new ReinsertOperation(
  559. new Position( gy, [ 1 ] ),
  560. 1,
  561. new Position( root, [ 3, 3, 4 ] ),
  562. baseVersion
  563. );
  564. const attributeDelta = new AttributeDelta();
  565. attributeDelta.addOperation( new AttributeOperation(
  566. Range.createFromParentsAndOffsets( root, 0, root, 4 ), 'key', 'oldValue', 'newValue', baseVersion
  567. ) );
  568. const transformed = transform( splitDelta, attributeDelta, context );
  569. baseVersion = attributeDelta.operations.length;
  570. expect( transformed.length ).to.equal( 1 );
  571. expectDelta( transformed[ 0 ], {
  572. type: SplitDelta,
  573. operations: [
  574. {
  575. type: ReinsertOperation,
  576. sourcePosition: new Position( gy, [ 1 ] ),
  577. howMany: 1,
  578. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  579. baseVersion
  580. },
  581. {
  582. type: MoveOperation,
  583. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  584. howMany: 9,
  585. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  586. baseVersion: baseVersion + 1
  587. }
  588. ]
  589. } );
  590. } );
  591. it( 'should use default algorithm and not throw if split delta has NoOperation', () => {
  592. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  593. const attributeDelta = new AttributeDelta();
  594. attributeDelta.addOperation( new AttributeOperation(
  595. Range.createFromParentsAndOffsets( root, 0, root, 4 ), 'key', 'oldValue', 'newValue', baseVersion
  596. ) );
  597. const transformed = transform( splitDelta, attributeDelta, context );
  598. baseVersion = attributeDelta.operations.length;
  599. expect( transformed.length ).to.equal( 1 );
  600. expectDelta( transformed[ 0 ], {
  601. type: SplitDelta,
  602. operations: [
  603. {
  604. type: InsertOperation,
  605. position: new Position( root, [ 3, 3, 4 ] ),
  606. baseVersion
  607. },
  608. {
  609. type: NoOperation,
  610. baseVersion: baseVersion + 1
  611. }
  612. ]
  613. } );
  614. } );
  615. } );
  616. describe( 'RenameDelta', () => {
  617. it( 'renamed split element', () => {
  618. const renameDelta = new RenameDelta();
  619. renameDelta.addOperation( new RenameOperation(
  620. new Position( root, [ 3, 3, 3 ] ), 'h2', 'li', baseVersion
  621. ) );
  622. const transformed = transform( splitDelta, renameDelta, context );
  623. baseVersion = renameDelta.operations.length;
  624. expect( transformed.length ).to.equal( 2 );
  625. expectDelta( transformed[ 0 ], {
  626. type: SplitDelta,
  627. operations: [
  628. {
  629. type: InsertOperation,
  630. position: new Position( root, [ 3, 3, 4 ] ),
  631. baseVersion
  632. },
  633. {
  634. type: MoveOperation,
  635. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  636. howMany: 9,
  637. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  638. baseVersion: baseVersion + 1
  639. }
  640. ]
  641. } );
  642. expectDelta( transformed[ 1 ], {
  643. type: RenameDelta,
  644. operations: [
  645. {
  646. type: RenameOperation,
  647. position: new Position( root, [ 3, 3, 4 ] ),
  648. oldName: 'p', // `oldName` taken from SplitDelta.
  649. newName: 'li',
  650. baseVersion: baseVersion + 2
  651. }
  652. ]
  653. } );
  654. } );
  655. it( 'split element is different than renamed element', () => {
  656. const renameDelta = new RenameDelta();
  657. renameDelta.addOperation( new RenameOperation(
  658. new Position( root, [ 4 ] ), 'p', 'li', baseVersion
  659. ) );
  660. const transformed = transform( splitDelta, renameDelta, context );
  661. baseVersion = renameDelta.operations.length;
  662. expect( transformed.length ).to.equal( 1 );
  663. expectDelta( transformed[ 0 ], {
  664. type: SplitDelta,
  665. operations: [
  666. {
  667. type: InsertOperation,
  668. position: new Position( root, [ 3, 3, 4 ] ),
  669. baseVersion
  670. },
  671. {
  672. type: MoveOperation,
  673. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  674. howMany: 9,
  675. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  676. baseVersion: baseVersion + 1
  677. }
  678. ]
  679. } );
  680. } );
  681. it( 'renamed split element that is reinserted from graveyard', () => {
  682. splitDelta.operations[ 0 ] = new ReinsertOperation(
  683. new Position( gy, [ 1 ] ),
  684. 1,
  685. new Position( root, [ 3, 3, 4 ] ),
  686. baseVersion
  687. );
  688. const renameDelta = new RenameDelta();
  689. renameDelta.addOperation( new RenameOperation(
  690. new Position( root, [ 3, 3, 3 ] ), 'p', 'li', baseVersion
  691. ) );
  692. context.aWasUndone = true;
  693. const transformed = transform( splitDelta, renameDelta, context );
  694. baseVersion = renameDelta.operations.length;
  695. expect( transformed.length ).to.equal( 1 );
  696. expectDelta( transformed[ 0 ], {
  697. type: SplitDelta,
  698. operations: [
  699. {
  700. type: ReinsertOperation,
  701. sourcePosition: new Position( gy, [ 1 ] ),
  702. howMany: 1,
  703. targetPosition: new Position( root, [ 3, 3, 4 ] ),
  704. baseVersion
  705. },
  706. {
  707. type: MoveOperation,
  708. sourcePosition: new Position( root, [ 3, 3, 3, 3 ] ),
  709. howMany: 9,
  710. targetPosition: new Position( root, [ 3, 3, 4, 0 ] ),
  711. baseVersion: baseVersion + 1
  712. }
  713. ]
  714. } );
  715. } );
  716. it( 'should not throw if clone operation is NoOperation and use default transformation in that case', () => {
  717. const noOpSplitDelta = new SplitDelta();
  718. noOpSplitDelta.addOperation( new NoOperation( 0 ) );
  719. noOpSplitDelta.addOperation( new MoveOperation( new Position( root, [ 1, 2 ] ), 3, new Position( root, [ 2, 0 ] ), 1 ) );
  720. const renameDelta = new RenameDelta();
  721. renameDelta.addOperation( new RenameOperation(
  722. new Position( root, [ 1 ] ),
  723. 'p',
  724. 'li',
  725. baseVersion
  726. ) );
  727. const transformed = transform( noOpSplitDelta, renameDelta, context );
  728. expect( transformed.length ).to.equal( 1 );
  729. expectDelta( transformed[ 0 ], {
  730. type: SplitDelta,
  731. operations: [
  732. {
  733. type: NoOperation,
  734. baseVersion: 1
  735. },
  736. {
  737. type: MoveOperation,
  738. sourcePosition: new Position( root, [ 1, 2 ] ),
  739. howMany: 3,
  740. targetPosition: new Position( root, [ 2, 0 ] ),
  741. baseVersion: 2
  742. }
  743. ]
  744. } );
  745. } );
  746. } );
  747. describe( 'RemoveDelta', () => {
  748. it( 'node inside the removed range was a node that has been split', () => {
  749. splitPosition = new Position( root, [ 3, 3, 2, 2 ] );
  750. splitDelta = getSplitDelta( splitPosition, new Element( 'x' ), 2, baseVersion );
  751. const removePosition = new Position( root, [ 3, 3, 1 ] );
  752. const removeDelta = getRemoveDelta( removePosition, 3, baseVersion );
  753. const removeOperation = removeDelta.operations[ 0 ];
  754. const transformed = transform( splitDelta, removeDelta, context );
  755. expect( transformed.length ).to.equal( 1 );
  756. baseVersion = removeDelta.operations.length;
  757. const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
  758. const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 );
  759. newMoveSourcePosition.path.push( 2 );
  760. const newMoveTargetPosition = Position.createAt( newInsertPosition );
  761. newMoveTargetPosition.path.push( 0 );
  762. expectDelta( transformed[ 0 ], {
  763. type: SplitDelta,
  764. operations: [
  765. {
  766. type: InsertOperation,
  767. position: newInsertPosition,
  768. baseVersion
  769. },
  770. {
  771. type: MoveOperation,
  772. sourcePosition: newMoveSourcePosition,
  773. howMany: 2,
  774. targetPosition: newMoveTargetPosition,
  775. baseVersion: baseVersion + 1
  776. }
  777. ]
  778. } );
  779. } );
  780. it( 'last node in the removed range was a node that has been split', () => {
  781. const removePosition = new Position( root, [ 3, 3, 2 ] );
  782. const removeDelta = getRemoveDelta( removePosition, 2, baseVersion );
  783. const removeOperation = removeDelta.operations[ 0 ];
  784. const transformed = transform( splitDelta, removeDelta, context );
  785. expect( transformed.length ).to.equal( 1 );
  786. baseVersion = removeDelta.operations.length;
  787. const newInsertPosition = removeOperation.targetPosition.getShiftedBy( 2 );
  788. const newMoveSourcePosition = removeOperation.targetPosition.getShiftedBy( 1 );
  789. newMoveSourcePosition.path.push( 3 );
  790. const newMoveTargetPosition = Position.createAt( newInsertPosition );
  791. newMoveTargetPosition.path.push( 0 );
  792. expectDelta( transformed[ 0 ], {
  793. type: SplitDelta,
  794. operations: [
  795. {
  796. type: InsertOperation,
  797. position: newInsertPosition,
  798. baseVersion
  799. },
  800. {
  801. type: MoveOperation,
  802. sourcePosition: newMoveSourcePosition,
  803. howMany: 9,
  804. targetPosition: newMoveTargetPosition,
  805. baseVersion: baseVersion + 1
  806. }
  807. ]
  808. } );
  809. } );
  810. it( 'split node has been removed - undo context', () => {
  811. const removePosition = new Position( root, [ 3, 3, 3 ] );
  812. const removeDelta = getRemoveDelta( removePosition, 1, baseVersion );
  813. context.bWasUndone = true;
  814. const transformed = transform( splitDelta, removeDelta, context );
  815. expect( transformed.length ).to.equal( 1 );
  816. baseVersion = removeDelta.operations.length;
  817. expectDelta( transformed[ 0 ], {
  818. type: SplitDelta,
  819. operations: [
  820. {
  821. type: InsertOperation,
  822. position: splitDelta.operations[ 0 ].position.getShiftedBy( -1 ),
  823. baseVersion
  824. },
  825. {
  826. type: ReinsertOperation,
  827. sourcePosition: new Position( gy, [ 0, 3 ] ),
  828. howMany: splitDelta.operations[ 1 ].howMany,
  829. targetPosition: new Position( root, [ 3, 3, 3, 0 ] ),
  830. baseVersion: baseVersion + 1
  831. }
  832. ]
  833. } );
  834. } );
  835. it( 'should not throw if clone operation is NoOperation and use default transformation in that case', () => {
  836. const noOpSplitDelta = new SplitDelta();
  837. noOpSplitDelta.addOperation( new NoOperation( 0 ) );
  838. noOpSplitDelta.addOperation( new MoveOperation( new Position( root, [ 1, 2 ] ), 3, new Position( root, [ 2, 0 ] ), 1 ) );
  839. const removeDelta = getRemoveDelta( new Position( root, [ 0 ] ), 1, 0 );
  840. const transformed = transform( noOpSplitDelta, removeDelta, context );
  841. expect( transformed.length ).to.equal( 1 );
  842. expectDelta( transformed[ 0 ], {
  843. type: SplitDelta,
  844. operations: [
  845. {
  846. type: NoOperation,
  847. baseVersion: 1
  848. },
  849. {
  850. type: MoveOperation,
  851. sourcePosition: new Position( root, [ 0, 2 ] ),
  852. howMany: 3,
  853. targetPosition: new Position( root, [ 1, 0 ] ),
  854. baseVersion: 2
  855. }
  856. ]
  857. } );
  858. } );
  859. } );
  860. } );
  861. } );