splitoperation.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  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. expect( () => operation._validate() ).to.throw( CKEditorError, /split-operation-position-invalid/ );
  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, [ 1 ] ), 3, null, doc.version );
  99. expect( () => operation._validate() ).to.throw( CKEditorError, /split-operation-split-in-root/ );
  100. } );
  101. it( 'should throw an error if number of nodes to move is invalid', () => {
  102. const p1 = new Element( 'p1', null, new Text( 'Foobar' ) );
  103. root._insertChild( 0, [ p1 ] );
  104. const operation = new SplitOperation( new Position( root, [ 0, 2 ] ), 6, null, doc.version );
  105. expect( () => operation._validate() ).to.throw( CKEditorError, /split-operation-how-many-invalid/ );
  106. } );
  107. it( 'should throw an error if graveyard position is invalid', () => {
  108. const p1 = new Element( 'p1', null, new Text( 'Foobar' ) );
  109. root._insertChild( 0, [ p1 ] );
  110. const operation = new SplitOperation( new Position( root, [ 0, 2 ] ), 4, gyPos, doc.version );
  111. expect( () => operation._validate() ).to.throw( CKEditorError, /split-operation-graveyard-position-invalid/ );
  112. } );
  113. } );
  114. it( 'should create SplitOperation with the same parameters when cloned #1', () => {
  115. const position = new Position( root, [ 1, 2 ] );
  116. const howMany = 4;
  117. const baseVersion = doc.version;
  118. const op = new SplitOperation( position, howMany, null, baseVersion );
  119. const clone = op.clone();
  120. // New instance rather than a pointer to the old instance.
  121. expect( clone ).not.to.be.equal( op );
  122. expect( clone ).to.be.instanceof( SplitOperation );
  123. expect( clone.position.isEqual( position ) ).to.be.true;
  124. expect( clone.howMany ).to.equal( howMany );
  125. expect( clone.graveyardPosition ).to.be.null;
  126. expect( clone.baseVersion ).to.equal( baseVersion );
  127. } );
  128. it( 'should create SplitOperation with the same parameters when cloned #2', () => {
  129. const position = new Position( root, [ 1, 2 ] );
  130. const howMany = 4;
  131. const baseVersion = doc.version;
  132. const op = new SplitOperation( position, howMany, gyPos, baseVersion );
  133. const clone = op.clone();
  134. // New instance rather than a pointer to the old instance.
  135. expect( clone ).not.to.be.equal( op );
  136. expect( clone ).to.be.instanceof( SplitOperation );
  137. expect( clone.position.isEqual( position ) ).to.be.true;
  138. expect( clone.howMany ).to.equal( howMany );
  139. expect( clone.graveyardPosition.isEqual( gyPos ) ).to.be.true;
  140. expect( clone.baseVersion ).to.equal( baseVersion );
  141. } );
  142. describe( 'toJSON', () => {
  143. it( 'should create proper json object #1', () => {
  144. const position = new Position( root, [ 0, 3 ] );
  145. const op = new SplitOperation( position, 2, null, doc.version );
  146. const serialized = op.toJSON();
  147. expect( serialized ).to.deep.equal( {
  148. __className: 'SplitOperation',
  149. baseVersion: 0,
  150. howMany: 2,
  151. position: op.position.toJSON(),
  152. graveyardPosition: null
  153. } );
  154. } );
  155. it( 'should create proper json object #2', () => {
  156. const position = new Position( root, [ 0, 3 ] );
  157. const op = new SplitOperation( position, 2, gyPos, doc.version );
  158. const serialized = op.toJSON();
  159. expect( serialized ).to.deep.equal( {
  160. __className: 'SplitOperation',
  161. baseVersion: 0,
  162. howMany: 2,
  163. position: op.position.toJSON(),
  164. graveyardPosition: op.graveyardPosition.toJSON()
  165. } );
  166. } );
  167. } );
  168. describe( 'fromJSON', () => {
  169. it( 'should create proper SplitOperation from json object #1', () => {
  170. const position = new Position( root, [ 0, 3 ] );
  171. const op = new SplitOperation( position, 2, null, doc.version );
  172. const serialized = op.toJSON();
  173. const deserialized = SplitOperation.fromJSON( serialized, doc );
  174. expect( deserialized ).to.deep.equal( op );
  175. } );
  176. it( 'should create proper SplitOperation from json object #2', () => {
  177. const position = new Position( root, [ 0, 3 ] );
  178. const op = new SplitOperation( position, 2, gyPos, doc.version );
  179. const serialized = op.toJSON();
  180. const deserialized = SplitOperation.fromJSON( serialized, doc );
  181. expect( deserialized ).to.deep.equal( op );
  182. } );
  183. } );
  184. } );