unwrapdelta.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 MoveOperation from '../../../../src/model/operation/moveoperation';
  12. import MergeDelta from '../../../../src/model/delta/mergedelta';
  13. import UnwrapDelta from '../../../../src/model/delta/unwrapdelta';
  14. import { getNodesAndText } from '../../../../tests/model/_utils/utils';
  15. import {
  16. applyDelta,
  17. expectDelta,
  18. getFilledDocument,
  19. getSplitDelta,
  20. getUnwrapDelta
  21. } from '../../../../tests/model/delta/transform/_utils/utils';
  22. describe( 'transform', () => {
  23. let doc, root, gy, baseVersion;
  24. beforeEach( () => {
  25. doc = getFilledDocument();
  26. root = doc.getRoot();
  27. gy = doc.graveyard;
  28. baseVersion = doc.version;
  29. } );
  30. describe( 'UnwrapDelta by', () => {
  31. let unwrapDelta;
  32. beforeEach( () => {
  33. unwrapDelta = getUnwrapDelta( new Position( root, [ 3, 3, 3 ] ), 12, baseVersion );
  34. } );
  35. describe( 'SplitDelta', () => {
  36. it( 'split position directly in unwrapped node', () => {
  37. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  38. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  39. const transformed = transform( unwrapDelta, splitDelta );
  40. expect( transformed.length ).to.equal( 2 );
  41. baseVersion = splitDelta.operations.length;
  42. expectDelta( transformed[ 0 ], {
  43. type: MergeDelta,
  44. operations: [
  45. {
  46. type: MoveOperation,
  47. sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
  48. howMany: 9,
  49. targetPosition: new Position( root, [ 3, 3, 3, 3 ] ),
  50. baseVersion
  51. },
  52. {
  53. type: MoveOperation,
  54. sourcePosition: new Position( root, [ 3, 3, 4 ] ),
  55. howMany: 1,
  56. targetPosition: new Position( gy, [ 0 ] ),
  57. baseVersion: baseVersion + 1
  58. }
  59. ]
  60. } );
  61. expectDelta( transformed[ 1 ], {
  62. type: UnwrapDelta,
  63. operations: [
  64. {
  65. type: MoveOperation,
  66. sourcePosition: new Position( root, [ 3, 3, 3, 0 ] ),
  67. howMany: 12,
  68. targetPosition: new Position( root, [ 3, 3, 3 ] ),
  69. baseVersion: baseVersion + 2
  70. },
  71. {
  72. type: MoveOperation,
  73. sourcePosition: new Position( root, [ 3, 3, 15 ] ),
  74. howMany: 1,
  75. targetPosition: new Position( gy, [ 0 ] ),
  76. baseVersion: baseVersion + 3
  77. }
  78. ]
  79. } );
  80. // Test if deltas do what they should after applying transformed delta.
  81. applyDelta( splitDelta, doc );
  82. applyDelta( transformed[ 0 ], doc );
  83. applyDelta( transformed[ 1 ], doc );
  84. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 1 ) );
  85. // UnwrapDelta is applied. SplitDelta is discarded.
  86. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXabcfoobarxyzDIV' );
  87. } );
  88. it( 'split position before unwrapped node', () => {
  89. const splitPosition = new Position( root, [ 3, 3, 3 ] );
  90. const splitDelta = getSplitDelta( splitPosition, new Element( 'div' ), 1, baseVersion );
  91. const transformed = transform( unwrapDelta, splitDelta );
  92. expect( transformed.length ).to.equal( 1 );
  93. baseVersion = splitDelta.operations.length;
  94. expectDelta( transformed[ 0 ], {
  95. type: UnwrapDelta,
  96. operations: [
  97. {
  98. type: MoveOperation,
  99. sourcePosition: new Position( root, [ 3, 4, 0, 0 ] ),
  100. howMany: 12,
  101. targetPosition: new Position( root, [ 3, 4, 0 ] ),
  102. baseVersion
  103. },
  104. {
  105. type: MoveOperation,
  106. sourcePosition: new Position( root, [ 3, 4, 12 ] ),
  107. howMany: 1,
  108. targetPosition: new Position( gy, [ 0 ] ),
  109. baseVersion: baseVersion + 1
  110. }
  111. ]
  112. } );
  113. // Test if deltas do what they should after applying transformed delta.
  114. applyDelta( splitDelta, doc );
  115. applyDelta( transformed[ 0 ], doc );
  116. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3 ] ), 2 ) );
  117. // UnwrapDelta and SplitDelta are applied.
  118. expect( nodesAndText ).to.equal( 'DIVXXXXXabcdXDIVDIVabcfoobarxyzDIV' );
  119. } );
  120. } );
  121. } );
  122. } );