insertdelta.js 6.2 KB

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