removeoperation.js 6.4 KB

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