8
0

mergeoperation.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import MergeOperation from '../../../src/model/operation/mergeoperation';
  7. import SplitOperation from '../../../src/model/operation/splitoperation';
  8. import Position from '../../../src/model/position';
  9. import Element from '../../../src/model/element';
  10. import Text from '../../../src/model/text';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'MergeOperation', () => {
  13. let model, doc, root, gy, gyPos;
  14. beforeEach( () => {
  15. model = new Model();
  16. doc = model.document;
  17. root = doc.createRoot();
  18. gy = doc.graveyard;
  19. gyPos = new Position( gy, [ 0 ] );
  20. } );
  21. it( 'should have proper type', () => {
  22. const merge = new MergeOperation( new Position( root, [ 1, 0 ] ), 2, new Position( root, [ 0, 1 ] ), gyPos, 1 );
  23. expect( merge.type ).to.equal( 'merge' );
  24. } );
  25. it( 'should have proper deletionPosition', () => {
  26. const merge = new MergeOperation( new Position( root, [ 1, 0 ] ), 2, new Position( root, [ 0, 1 ] ), gyPos, 1 );
  27. expect( merge.deletionPosition.path ).to.deep.equal( [ 1 ] );
  28. } );
  29. it( 'should have proper movedRange', () => {
  30. const merge = new MergeOperation( new Position( root, [ 1, 0 ] ), 2, new Position( root, [ 0, 1 ] ), gyPos, 1 );
  31. expect( merge.movedRange.start.path ).to.deep.equal( [ 1, 0 ] );
  32. expect( merge.movedRange.end.path ).to.deep.equal( [ 1, Number.POSITIVE_INFINITY ] );
  33. } );
  34. it( 'should merge two nodes together', () => {
  35. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  36. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  37. root._insertChild( 0, [ p1, p2 ] );
  38. model.applyOperation(
  39. new MergeOperation(
  40. new Position( root, [ 1, 0 ] ),
  41. 3,
  42. new Position( root, [ 0, 3 ] ),
  43. gyPos,
  44. doc.version
  45. )
  46. );
  47. expect( doc.version ).to.equal( 1 );
  48. expect( root.maxOffset ).to.equal( 1 );
  49. expect( root.getChild( 0 ).name ).to.equal( 'p1' );
  50. expect( p1.maxOffset ).to.equal( 6 );
  51. expect( p1.getChild( 0 ).data ).to.equal( 'Foobar' );
  52. } );
  53. it( 'should create a proper SplitOperation as a reverse', () => {
  54. const sourcePosition = new Position( root, [ 1, 0 ] );
  55. const targetPosition = new Position( root, [ 0, 3 ] );
  56. const operation = new MergeOperation( sourcePosition, 2, targetPosition, gyPos, doc.version );
  57. const reverse = operation.getReversed();
  58. expect( reverse ).to.be.an.instanceof( SplitOperation );
  59. expect( reverse.baseVersion ).to.equal( 1 );
  60. expect( reverse.howMany ).to.equal( 2 );
  61. expect( reverse.splitPosition.isEqual( targetPosition ) ).to.be.true;
  62. expect( reverse.insertionPosition.isEqual( new Position( root, [ 1 ] ) ) ).to.be.true;
  63. expect( reverse.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  64. } );
  65. it( 'should undo merge by applying reverse operation', () => {
  66. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  67. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  68. root._insertChild( 0, [ p1, p2 ] );
  69. const operation = new MergeOperation(
  70. new Position( root, [ 1, 0 ] ),
  71. 3,
  72. new Position( root, [ 0, 3 ] ),
  73. gyPos,
  74. doc.version
  75. );
  76. model.applyOperation( operation );
  77. model.applyOperation( operation.getReversed() );
  78. expect( doc.version ).to.equal( 2 );
  79. expect( root.maxOffset ).to.equal( 2 );
  80. expect( p1.maxOffset ).to.equal( 3 );
  81. expect( p1.getChild( 0 ).data ).to.equal( 'Foo' );
  82. expect( p2.maxOffset ).to.equal( 3 );
  83. expect( p2.getChild( 0 ).data ).to.equal( 'bar' );
  84. } );
  85. describe( '_validate()', () => {
  86. it( 'should throw an error if source position is invalid', () => {
  87. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  88. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  89. root._insertChild( 0, [ p1, p2 ] );
  90. const operation = new MergeOperation(
  91. new Position( root, [ 0, 3 ] ),
  92. 3,
  93. new Position( root, [ 2, 0 ] ),
  94. gyPos,
  95. doc.version
  96. );
  97. expectToThrowCKEditorError( () => operation._validate(), /model-position-path-incorrect/, model );
  98. } );
  99. it( 'should throw an error if source position is in root', () => {
  100. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  101. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  102. root._insertChild( 0, [ p1, p2 ] );
  103. const operation = new MergeOperation(
  104. new Position( root, [ 0 ] ),
  105. 3,
  106. new Position( root, [ 0, 3 ] ),
  107. gyPos,
  108. doc.version
  109. );
  110. expectToThrowCKEditorError( () => operation._validate(), /merge-operation-source-position-invalid/, model );
  111. } );
  112. it( 'should throw an error if target position is in root', () => {
  113. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  114. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  115. root._insertChild( 0, [ p1, p2 ] );
  116. const operation = new MergeOperation(
  117. new Position( root, [ 0, 3 ] ),
  118. 3,
  119. new Position( root, [ 0 ] ),
  120. gyPos,
  121. doc.version
  122. );
  123. expectToThrowCKEditorError( () => operation._validate(), /merge-operation-target-position-invalid/, model );
  124. } );
  125. it( 'should throw an error if target position is invalid', () => {
  126. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  127. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  128. root._insertChild( 0, [ p1, p2 ] );
  129. const operation = new MergeOperation(
  130. new Position( root, [ 2, 3 ] ),
  131. 3,
  132. new Position( root, [ 1, 0 ] ),
  133. gyPos,
  134. doc.version
  135. );
  136. expectToThrowCKEditorError( () => operation._validate(), /model-position-path-incorrect/, model );
  137. } );
  138. it( 'should throw an error if number of nodes to move is invalid', () => {
  139. const p1 = new Element( 'p1', null, new Text( 'Foo' ) );
  140. const p2 = new Element( 'p2', null, new Text( 'bar' ) );
  141. root._insertChild( 0, [ p1, p2 ] );
  142. const operation = new MergeOperation(
  143. new Position( root, [ 0, 3 ] ),
  144. 5,
  145. new Position( root, [ 1, 0 ] ),
  146. gyPos,
  147. doc.version
  148. );
  149. expectToThrowCKEditorError( () => operation._validate(), /merge-operation-how-many-invalid/, model );
  150. } );
  151. } );
  152. it( 'should create MergeOperation with the same parameters when cloned', () => {
  153. const sourcePosition = new Position( root, [ 1, 0 ] );
  154. const targetPosition = new Position( root, [ 0, 3 ] );
  155. const howMany = 4;
  156. const baseVersion = doc.version;
  157. const op = new MergeOperation( sourcePosition, howMany, targetPosition, gyPos, baseVersion );
  158. const clone = op.clone();
  159. // New instance rather than a pointer to the old instance.
  160. expect( clone ).not.to.equal( op );
  161. expect( clone ).to.be.instanceof( MergeOperation );
  162. expect( clone.sourcePosition.isEqual( sourcePosition ) ).to.be.true;
  163. expect( clone.targetPosition.isEqual( targetPosition ) ).to.be.true;
  164. expect( clone.howMany ).to.equal( howMany );
  165. expect( clone.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  166. expect( clone.baseVersion ).to.equal( baseVersion );
  167. } );
  168. describe( 'toJSON', () => {
  169. it( 'should create proper json object', () => {
  170. const sourcePosition = new Position( root, [ 1, 0 ] );
  171. const targetPosition = new Position( root, [ 0, 3 ] );
  172. const op = new MergeOperation( sourcePosition, 1, targetPosition, gyPos, doc.version );
  173. const serialized = op.toJSON();
  174. expect( serialized ).to.deep.equal( {
  175. __className: 'MergeOperation',
  176. baseVersion: 0,
  177. howMany: 1,
  178. sourcePosition: op.sourcePosition.toJSON(),
  179. targetPosition: op.targetPosition.toJSON(),
  180. graveyardPosition: gyPos.toJSON()
  181. } );
  182. } );
  183. } );
  184. describe( 'fromJSON', () => {
  185. it( 'should create proper MergeOperation from json object', () => {
  186. const sourcePosition = new Position( root, [ 1, 0 ] );
  187. const targetPosition = new Position( root, [ 0, 3 ] );
  188. const op = new MergeOperation( sourcePosition, 1, targetPosition, gyPos, doc.version );
  189. const serialized = op.toJSON();
  190. const deserialized = MergeOperation.fromJSON( serialized, doc );
  191. expect( deserialized ).to.deep.equal( op );
  192. } );
  193. } );
  194. } );