unwrapdelta.js 5.6 KB

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