wrapdelta.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 InsertOperation from '../../../../src/model/operation/insertoperation';
  13. import NoOperation from '../../../../src/model/operation/nooperation';
  14. import MergeDelta from '../../../../src/model/delta/mergedelta';
  15. import WrapDelta from '../../../../src/model/delta/wrapdelta';
  16. import { getNodesAndText } from '../../../../tests/model/_utils/utils';
  17. import {
  18. applyDelta,
  19. expectDelta,
  20. getFilledDocument,
  21. getSplitDelta,
  22. getWrapDelta
  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( 'WrapDelta by', () => {
  34. let wrapDelta;
  35. beforeEach( () => {
  36. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  37. const wrapElement = new Element( 'E' );
  38. wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  39. } );
  40. describe( 'SplitDelta', () => {
  41. it( 'split position is between wrapped nodes', () => {
  42. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  43. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  44. const transformed = transform( wrapDelta, splitDelta, context );
  45. expect( transformed.length ).to.equal( 2 );
  46. baseVersion = splitDelta.operations.length;
  47. expectDelta( transformed[ 0 ], {
  48. type: MergeDelta,
  49. operations: [
  50. {
  51. type: MoveOperation,
  52. sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
  53. howMany: 9,
  54. targetPosition: new Position( root, [ 3, 3, 3, 3 ] ),
  55. baseVersion
  56. },
  57. {
  58. type: MoveOperation,
  59. sourcePosition: new Position( root, [ 3, 3, 4 ] ),
  60. howMany: 1,
  61. targetPosition: new Position( gy, [ 0 ] ),
  62. baseVersion: baseVersion + 1
  63. }
  64. ]
  65. } );
  66. expectDelta( transformed[ 1 ], {
  67. type: WrapDelta,
  68. operations: [
  69. {
  70. type: InsertOperation,
  71. position: new Position( root, [ 3, 3, 3, 5 ] ),
  72. baseVersion: baseVersion + 2
  73. },
  74. {
  75. type: MoveOperation,
  76. sourcePosition: new Position( root, [ 3, 3, 3, 1 ] ),
  77. howMany: 4,
  78. targetPosition: new Position( root, [ 3, 3, 3, 5, 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, 3 ] ), 1 ) );
  88. // WrapDelta is applied. SplitDelta is discarded.
  89. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  90. } );
  91. it( 'split position is before wrapped nodes', () => {
  92. const splitPosition = new Position( root, [ 3, 3, 3, 1 ] );
  93. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 11, baseVersion );
  94. const transformed = transform( wrapDelta, splitDelta, context );
  95. expect( transformed.length ).to.equal( 1 );
  96. baseVersion = wrapDelta.operations.length;
  97. expectDelta( transformed[ 0 ], {
  98. type: WrapDelta,
  99. operations: [
  100. {
  101. type: InsertOperation,
  102. position: new Position( root, [ 3, 3, 4, 4 ] ),
  103. baseVersion
  104. },
  105. {
  106. type: MoveOperation,
  107. sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
  108. howMany: 4,
  109. targetPosition: new Position( root, [ 3, 3, 4, 4, 0 ] ),
  110. baseVersion: baseVersion + 1
  111. }
  112. ]
  113. } );
  114. // Test if deltas do what they should after applying transformed delta.
  115. applyDelta( splitDelta, doc );
  116. applyDelta( transformed[ 0 ], doc );
  117. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  118. // WrapDelta and SplitDelta are correctly applied.
  119. expect( nodesAndText ).to.equal( 'PaPPEbcfoEobarxyzP' );
  120. } );
  121. it( 'split position is inside wrapped node', () => {
  122. // For this case, we need different WrapDelta so it is overwritten.
  123. const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  124. const wrapElement = new Element( 'E' );
  125. wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  126. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  127. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  128. const transformed = transform( wrapDelta, splitDelta, context );
  129. expect( transformed.length ).to.equal( 1 );
  130. baseVersion = wrapDelta.operations.length;
  131. expectDelta( transformed[ 0 ], {
  132. type: WrapDelta,
  133. operations: [
  134. {
  135. type: InsertOperation,
  136. position: new Position( root, [ 3, 3, 5 ] ),
  137. baseVersion
  138. },
  139. {
  140. type: MoveOperation,
  141. sourcePosition: new Position( root, [ 3, 3, 2 ] ),
  142. howMany: 3,
  143. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  144. baseVersion: baseVersion + 1
  145. }
  146. ]
  147. } );
  148. // Test if deltas do what they should after applying transformed delta.
  149. applyDelta( splitDelta, doc );
  150. applyDelta( transformed[ 0 ], doc );
  151. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  152. // WrapDelta and SplitDelta are correctly applied.
  153. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  154. } );
  155. it( 'should use default algorithm and not throw if split delta has NoOperation', () => {
  156. const splitPosition = new Position( root, [ 3, 3, 2, 1 ] );
  157. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 11, baseVersion );
  158. splitDelta.operations[ 1 ] = new NoOperation( 1 );
  159. const transformed = transform( wrapDelta, splitDelta, context );
  160. expect( transformed.length ).to.equal( 1 );
  161. baseVersion = wrapDelta.operations.length;
  162. expectDelta( transformed[ 0 ], {
  163. type: WrapDelta,
  164. operations: [
  165. {
  166. type: InsertOperation,
  167. position: new Position( root, [ 3, 3, 4, 5 ] ),
  168. baseVersion
  169. },
  170. {
  171. type: MoveOperation,
  172. sourcePosition: new Position( root, [ 3, 3, 4, 1 ] ),
  173. howMany: 4,
  174. targetPosition: new Position( root, [ 3, 3, 4, 5, 0 ] ),
  175. baseVersion: baseVersion + 1
  176. }
  177. ]
  178. } );
  179. } );
  180. } );
  181. } );
  182. } );