8
0

splitoperation.js 9.0 KB

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