removeoperation.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  7. import RemoveOperation from '../../../src/model/operation/removeoperation';
  8. import MoveOperation from '../../../src/model/operation/moveoperation';
  9. import Position from '../../../src/model/position';
  10. import Text from '../../../src/model/text';
  11. import Element from '../../../src/model/element';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  14. describe( 'RemoveOperation', () => {
  15. let doc, root, graveyard;
  16. beforeEach( () => {
  17. doc = new Document();
  18. root = doc.createRoot();
  19. graveyard = doc.graveyard;
  20. } );
  21. it( 'should have proper type', () => {
  22. const op = new RemoveOperation(
  23. new Position( root, [ 2 ] ),
  24. 2,
  25. new Position( doc.graveyard, [ 0 ] ),
  26. doc.version
  27. );
  28. expect( op.type ).to.equal( 'remove' );
  29. } );
  30. it( 'should not be sticky', () => {
  31. const op = new RemoveOperation(
  32. new Position( root, [ 2 ] ),
  33. 2,
  34. new Position( doc.graveyard, [ 0 ] ),
  35. doc.version
  36. );
  37. expect( op.isSticky ).to.be.false;
  38. } );
  39. it( 'should extend MoveOperation class', () => {
  40. const operation = new RemoveOperation(
  41. new Position( root, [ 2 ] ),
  42. 2,
  43. new Position( doc.graveyard, [ 0 ] ),
  44. doc.version
  45. );
  46. expect( operation ).to.be.instanceof( MoveOperation );
  47. } );
  48. it( 'should be able to remove set of nodes and append them to graveyard root', () => {
  49. root.insertChildren( 0, new Text( 'fozbar' ) );
  50. doc.applyOperation( wrapInDelta(
  51. new RemoveOperation(
  52. new Position( root, [ 2 ] ),
  53. 2,
  54. new Position( doc.graveyard, [ 0 ] ),
  55. doc.version
  56. )
  57. ) );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.maxOffset ).to.equal( 4 );
  60. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  61. expect( graveyard.maxOffset ).to.equal( 2 );
  62. expect( graveyard.getChild( 0 ).data ).to.equal( 'zb' );
  63. } );
  64. it( 'should create RemoveOperation with same parameters when cloned', () => {
  65. const pos = new Position( root, [ 2 ] );
  66. const operation = new RemoveOperation( pos, 2, new Position( doc.graveyard, [ 0 ] ), doc.version );
  67. const clone = operation.clone();
  68. expect( clone ).to.be.instanceof( RemoveOperation );
  69. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  70. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  71. expect( clone.howMany ).to.equal( operation.howMany );
  72. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  73. } );
  74. it( 'should create ReinsertOperation when reversed', () => {
  75. const position = new Position( root, [ 0 ] );
  76. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  77. const reverse = operation.getReversed();
  78. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  79. expect( reverse.baseVersion ).to.equal( 1 );
  80. expect( reverse.howMany ).to.equal( 2 );
  81. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  82. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  83. } );
  84. it( 'should create correct ReinsertOperation when reversed if source range was in graveyard', () => {
  85. const operation = new RemoveOperation( new Position( doc.graveyard, [ 2 ] ), 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  86. const reverse = operation.getReversed();
  87. expect( reverse.sourcePosition.path ).to.deep.equal( [ 0 ] );
  88. expect( reverse.targetPosition.path ).to.deep.equal( [ 3 ] );
  89. } );
  90. it( 'should undo remove set of nodes by applying reverse operation', () => {
  91. const position = new Position( root, [ 0 ] );
  92. const operation = new RemoveOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), 0 );
  93. const reverse = operation.getReversed();
  94. root.insertChildren( 0, new Text( 'bar' ) );
  95. doc.applyOperation( wrapInDelta( operation ) );
  96. expect( doc.version ).to.equal( 1 );
  97. expect( root.maxOffset ).to.equal( 0 );
  98. doc.applyOperation( wrapInDelta( reverse ) );
  99. expect( doc.version ).to.equal( 2 );
  100. expect( root.maxOffset ).to.equal( 3 );
  101. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  102. } );
  103. it( 'should properly remove a node that is already in a graveyard', () => {
  104. doc.graveyard.appendChildren( [ new Element( 'x' ), new Element( 'y' ), new Element( 'z' ) ] );
  105. const position = new Position( doc.graveyard, [ 2 ] );
  106. const operation = new RemoveOperation( position, 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  107. doc.applyOperation( wrapInDelta( operation ) );
  108. expect( doc.graveyard.childCount ).to.equal( 3 );
  109. expect( doc.graveyard.getChild( 0 ).name ).to.equal( 'z' );
  110. expect( doc.graveyard.getChild( 1 ).name ).to.equal( 'x' );
  111. expect( doc.graveyard.getChild( 2 ).name ).to.equal( 'y' );
  112. } );
  113. it( 'should throw when is executed on detached item', () => {
  114. const batch = doc.batch();
  115. const docFrag = batch.createDocumentFragment();
  116. const item = batch.createElement( 'foo' );
  117. batch.append( item, docFrag );
  118. const op = new RemoveOperation(
  119. new Position( docFrag, [ 0 ] ),
  120. 1,
  121. new Position( doc.graveyard, [ 0 ] ),
  122. doc.version
  123. );
  124. expect( () => {
  125. op._execute();
  126. } ).to.throw( CKEditorError, /^remove-operation-on-detached-item/ );
  127. } );
  128. it( 'should always be a document operation', () => {
  129. const op = new RemoveOperation(
  130. new Position( root, [ 2 ] ),
  131. 2,
  132. new Position( doc.graveyard, [ 0 ] ),
  133. doc.version
  134. );
  135. expect( op.isDocumentOperation ).to.true;
  136. } );
  137. describe( 'toJSON', () => {
  138. it( 'should create proper json object', () => {
  139. const op = new RemoveOperation(
  140. new Position( root, [ 2 ] ),
  141. 2,
  142. new Position( doc.graveyard, [ 0 ] ),
  143. doc.version
  144. );
  145. const serialized = jsonParseStringify( op );
  146. expect( serialized ).to.deep.equal( {
  147. __className: 'engine.model.operation.RemoveOperation',
  148. baseVersion: 0,
  149. howMany: 2,
  150. isSticky: false,
  151. sourcePosition: jsonParseStringify( op.sourcePosition ),
  152. targetPosition: jsonParseStringify( op.targetPosition )
  153. } );
  154. } );
  155. } );
  156. describe( 'fromJSON', () => {
  157. it( 'should create proper RemoveOperation from json object', () => {
  158. const op = new RemoveOperation(
  159. new Position( root, [ 2 ] ),
  160. 2,
  161. new Position( doc.graveyard, [ 0 ] ),
  162. doc.version
  163. );
  164. const serialized = jsonParseStringify( op );
  165. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  166. expect( deserialized ).to.deep.equal( op );
  167. } );
  168. } );
  169. } );