8
0

wrapdelta.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 MergeDelta from '../../../../src/model/delta/mergedelta';
  14. import WrapDelta from '../../../../src/model/delta/wrapdelta';
  15. import { getNodesAndText } from '../../../../tests/model/_utils/utils';
  16. import {
  17. applyDelta,
  18. expectDelta,
  19. getFilledDocument,
  20. getSplitDelta,
  21. getWrapDelta
  22. } from '../../../../tests/model/delta/transform/_utils/utils';
  23. describe( 'transform', () => {
  24. let doc, root, gy, baseVersion;
  25. beforeEach( () => {
  26. doc = getFilledDocument();
  27. root = doc.getRoot();
  28. gy = doc.graveyard;
  29. baseVersion = doc.version;
  30. } );
  31. describe( 'WrapDelta by', () => {
  32. let wrapDelta;
  33. beforeEach( () => {
  34. const wrapRange = new Range( new Position( root, [ 3, 3, 3, 1 ] ), new Position( root, [ 3, 3, 3, 5 ] ) );
  35. const wrapElement = new Element( 'E' );
  36. wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  37. } );
  38. describe( 'SplitDelta', () => {
  39. it( 'split position is between wrapped nodes', () => {
  40. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  41. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  42. const transformed = transform( wrapDelta, splitDelta );
  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: WrapDelta,
  66. operations: [
  67. {
  68. type: InsertOperation,
  69. position: new Position( root, [ 3, 3, 3, 5 ] ),
  70. baseVersion: baseVersion + 2
  71. },
  72. {
  73. type: MoveOperation,
  74. sourcePosition: new Position( root, [ 3, 3, 3, 1 ] ),
  75. howMany: 4,
  76. targetPosition: new Position( root, [ 3, 3, 3, 5, 0 ] ),
  77. baseVersion: baseVersion + 3
  78. }
  79. ]
  80. } );
  81. // Test if deltas do what they should after applying transformed delta.
  82. applyDelta( splitDelta, doc );
  83. applyDelta( transformed[ 0 ], doc );
  84. applyDelta( transformed[ 1 ], doc );
  85. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 1 ) );
  86. // WrapDelta is applied. SplitDelta is discarded.
  87. expect( nodesAndText ).to.equal( 'PaEbcfoEobarxyzP' );
  88. } );
  89. it( 'split position is before wrapped nodes', () => {
  90. const splitPosition = new Position( root, [ 3, 3, 3, 1 ] );
  91. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 11, baseVersion );
  92. const transformed = transform( wrapDelta, splitDelta );
  93. expect( transformed.length ).to.equal( 1 );
  94. baseVersion = wrapDelta.operations.length;
  95. expectDelta( transformed[ 0 ], {
  96. type: WrapDelta,
  97. operations: [
  98. {
  99. type: InsertOperation,
  100. position: new Position( root, [ 3, 3, 4, 4 ] ),
  101. baseVersion
  102. },
  103. {
  104. type: MoveOperation,
  105. sourcePosition: new Position( root, [ 3, 3, 4, 0 ] ),
  106. howMany: 4,
  107. targetPosition: new Position( root, [ 3, 3, 4, 4, 0 ] ),
  108. baseVersion: baseVersion + 1
  109. }
  110. ]
  111. } );
  112. // Test if deltas do what they should after applying transformed delta.
  113. applyDelta( splitDelta, doc );
  114. applyDelta( transformed[ 0 ], doc );
  115. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 3 ] ), 2 ) );
  116. // WrapDelta and SplitDelta are correctly applied.
  117. expect( nodesAndText ).to.equal( 'PaPPEbcfoEobarxyzP' );
  118. } );
  119. it( 'split position is inside wrapped node', () => {
  120. // For this case, we need different WrapDelta so it is overwritten.
  121. const wrapRange = new Range( new Position( root, [ 3, 3, 2 ] ), new Position( root, [ 3, 3, 4 ] ) );
  122. const wrapElement = new Element( 'E' );
  123. wrapDelta = getWrapDelta( wrapRange, wrapElement, baseVersion );
  124. const splitPosition = new Position( root, [ 3, 3, 3, 3 ] );
  125. const splitDelta = getSplitDelta( splitPosition, new Element( 'p' ), 9, baseVersion );
  126. const transformed = transform( wrapDelta, splitDelta );
  127. expect( transformed.length ).to.equal( 1 );
  128. baseVersion = wrapDelta.operations.length;
  129. expectDelta( transformed[ 0 ], {
  130. type: WrapDelta,
  131. operations: [
  132. {
  133. type: InsertOperation,
  134. position: new Position( root, [ 3, 3, 5 ] ),
  135. baseVersion
  136. },
  137. {
  138. type: MoveOperation,
  139. sourcePosition: new Position( root, [ 3, 3, 2 ] ),
  140. howMany: 3,
  141. targetPosition: new Position( root, [ 3, 3, 5, 0 ] ),
  142. baseVersion: baseVersion + 1
  143. }
  144. ]
  145. } );
  146. // Test if deltas do what they should after applying transformed delta.
  147. applyDelta( splitDelta, doc );
  148. applyDelta( transformed[ 0 ], doc );
  149. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 2 ] ), 1 ) );
  150. // WrapDelta and SplitDelta are correctly applied.
  151. expect( nodesAndText ).to.equal( 'EXabcdXPabcPPfoobarxyzPE' );
  152. } );
  153. } );
  154. } );
  155. } );