insertdelta.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 InsertDelta from '../../../../src/model/delta/insertdelta';
  12. import SplitDelta from '../../../../src/model/delta/splitdelta';
  13. import InsertOperation from '../../../../src/model/operation/insertoperation';
  14. import MoveOperation from '../../../../src/model/operation/moveoperation';
  15. import ReinsertOperation from '../../../../src/model/operation/reinsertoperation';
  16. import { getNodesAndText } from '../../../../tests/model/_utils/utils';
  17. import {
  18. applyDelta,
  19. expectDelta,
  20. getFilledDocument,
  21. getInsertDelta,
  22. getMergeDelta
  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 = {
  32. isStrong: false
  33. };
  34. } );
  35. describe( 'InsertDelta by', () => {
  36. let insertDelta, insertPosition, nodeA, nodeB;
  37. beforeEach( () => {
  38. nodeA = new Element( 'a' );
  39. nodeB = new Element( 'b' );
  40. insertPosition = new Position( root, [ 3, 3, 3 ] );
  41. insertDelta = getInsertDelta( insertPosition, [ nodeA, nodeB ], baseVersion );
  42. } );
  43. describe( 'InsertDelta', () => {
  44. it( 'should be resolved in a same way as two insert operations', () => {
  45. const insertPositionB = new Position( root, [ 3, 1 ] );
  46. const insertDeltaB = getInsertDelta( insertPositionB, [ new Element( 'c' ), new Element( 'd' ) ], baseVersion );
  47. const transformed = transform( insertDelta, insertDeltaB, context );
  48. expect( transformed.length ).to.equal( 1 );
  49. expectDelta( transformed[ 0 ], {
  50. type: InsertDelta,
  51. operations: [
  52. {
  53. type: InsertOperation,
  54. position: new Position( root, [ 3, 5, 3 ] ),
  55. nodes: [ nodeA, nodeB ],
  56. baseVersion: baseVersion + 1
  57. }
  58. ]
  59. } );
  60. } );
  61. } );
  62. describe( 'MergeDelta', () => {
  63. it( 'merge in same position as insert', () => {
  64. const mergeDelta = getMergeDelta( insertPosition, 4, 12, baseVersion );
  65. const transformed = transform( insertDelta, mergeDelta, context );
  66. baseVersion = mergeDelta.operations.length;
  67. // Expected: MergeOperation gets reversed (by special case of SplitDelta that has ReinsertOperation
  68. // instead of InsertOperation. Then InsertDelta is applied as is.
  69. expect( transformed.length ).to.equal( 2 );
  70. expectDelta( transformed[ 0 ], {
  71. type: SplitDelta,
  72. operations: [
  73. {
  74. type: ReinsertOperation,
  75. sourcePosition: new Position( gy, [ 0 ] ),
  76. howMany: 1,
  77. targetPosition: new Position( root, [ 3, 3, 3 ] ),
  78. baseVersion
  79. },
  80. {
  81. type: MoveOperation,
  82. sourcePosition: new Position( root, [ 3, 3, 2, 4 ] ),
  83. howMany: 12,
  84. targetPosition: new Position( root, [ 3, 3, 3, 0 ] ),
  85. baseVersion: baseVersion + 1
  86. }
  87. ]
  88. } );
  89. expectDelta( transformed[ 1 ], {
  90. type: InsertDelta,
  91. operations: [
  92. {
  93. type: InsertOperation,
  94. position: Position.createFromPosition( insertPosition ),
  95. nodes: [ nodeA, nodeB ],
  96. baseVersion: baseVersion + 2
  97. }
  98. ]
  99. } );
  100. // Test if deltas do what they should after applying transformed delta.
  101. applyDelta( mergeDelta, doc );
  102. applyDelta( transformed[ 0 ], doc );
  103. applyDelta( transformed[ 1 ], doc );
  104. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 3, 0 ] ), 6 ) );
  105. // Merge between X with "abcd" and P with "abcfoobarxyz" should be reversed and AB should be inserted between X and P.
  106. expect( nodesAndText ).to.equal( 'XXXXXabcdXAABBPabcfoobarxyzP' );
  107. } );
  108. it( 'merge the node that is parent of insert position (sticky move test)', () => {
  109. const mergeDelta = getMergeDelta( new Position( root, [ 3, 3 ] ), 1, 4, baseVersion );
  110. const transformed = transform( insertDelta, mergeDelta, context );
  111. baseVersion = mergeDelta.operations.length;
  112. // Expected: InsertOperation in InsertDelta has it's path updated.
  113. expect( transformed.length ).to.equal( 1 );
  114. expectDelta( transformed[ 0 ], {
  115. type: InsertDelta,
  116. operations: [
  117. {
  118. type: InsertOperation,
  119. position: new Position( root, [ 3, 2, 4 ] ),
  120. nodes: [ nodeA, nodeB ],
  121. baseVersion
  122. }
  123. ]
  124. } );
  125. // Test if deltas do what they should after applying transformed delta.
  126. applyDelta( mergeDelta, doc );
  127. applyDelta( transformed[ 0 ], doc );
  128. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 3, 2, 0 ] ), 7 ) );
  129. // Merge between X with "a" and DIV should be applied. AB should be inserted in new, correct position.
  130. expect( nodesAndText ).to.equal( 'aXXXXXabcdXAABBPabcfoobarxyzP' );
  131. } );
  132. it( 'merge at affected position but resolved by default OT', () => {
  133. const mergeDelta = getMergeDelta( new Position( root, [ 3 ] ), 1, 4, baseVersion );
  134. const transformed = transform( insertDelta, mergeDelta, context );
  135. baseVersion = mergeDelta.operations.length;
  136. // Expected: InsertOperation in InsertDelta has it's path updated.
  137. expect( transformed.length ).to.equal( 1 );
  138. expectDelta( transformed[ 0 ], {
  139. type: InsertDelta,
  140. operations: [
  141. {
  142. type: InsertOperation,
  143. position: new Position( root, [ 2, 4, 3 ] ),
  144. nodes: [ nodeA, nodeB ],
  145. baseVersion
  146. }
  147. ]
  148. } );
  149. // Test if deltas do what they should after applying transformed delta.
  150. applyDelta( mergeDelta, doc );
  151. applyDelta( transformed[ 0 ], doc );
  152. const nodesAndText = getNodesAndText( Range.createFromPositionAndShift( new Position( root, [ 2, 0 ] ), 5 ) );
  153. // Merge between X with "a" and DIV should be applied. AB should be inserted in new, correct position.
  154. expect( nodesAndText ).to.equal( 'aXXXXXaXDIVXXXXXabcdXAABBPabcfoobarxyzPDIV' );
  155. } );
  156. } );
  157. } );
  158. } );