removeoperation.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, operation */
  6. import Document from 'ckeditor5/engine/model/document.js';
  7. import ReinsertOperation from 'ckeditor5/engine/model/operation/reinsertoperation.js';
  8. import RemoveOperation from 'ckeditor5/engine/model/operation/removeoperation.js';
  9. import MoveOperation from 'ckeditor5/engine/model/operation/moveoperation.js';
  10. import Position from 'ckeditor5/engine/model/position.js';
  11. import Text from 'ckeditor5/engine/model/text.js';
  12. import Element from 'ckeditor5/engine/model/element.js';
  13. import Delta from 'ckeditor5/engine/model/delta/delta.js';
  14. import { jsonParseStringify, wrapInDelta } from 'tests/engine/model/_utils/utils.js';
  15. describe( 'RemoveOperation', () => {
  16. let doc, root, graveyard;
  17. beforeEach( () => {
  18. doc = new Document();
  19. root = doc.createRoot();
  20. graveyard = doc.graveyard;
  21. } );
  22. it( 'should have proper type', () => {
  23. const op = new RemoveOperation(
  24. new Position( root, [ 2 ] ),
  25. 2,
  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. doc.version
  35. );
  36. expect( op.isSticky ).to.be.false;
  37. } );
  38. it( 'should extend MoveOperation class', () => {
  39. let operation = new RemoveOperation(
  40. new Position( root, [ 2 ] ),
  41. 2,
  42. doc.version
  43. );
  44. expect( operation ).to.be.instanceof( MoveOperation );
  45. } );
  46. it( 'should remove set of nodes and append them to holder element in graveyard root', () => {
  47. root.insertChildren( 0, new Text( 'fozbar' ) );
  48. doc.applyOperation( wrapInDelta(
  49. new RemoveOperation(
  50. new Position( root, [ 2 ] ),
  51. 2,
  52. doc.version
  53. )
  54. ) );
  55. expect( doc.version ).to.equal( 1 );
  56. expect( root.maxOffset ).to.equal( 4 );
  57. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  58. expect( graveyard.maxOffset ).to.equal( 1 );
  59. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'zb' );
  60. } );
  61. it( 'should create new holder element for remove operations in different deltas', () => {
  62. root.insertChildren( 0, new Text( 'fozbar' ) );
  63. doc.applyOperation( wrapInDelta(
  64. new RemoveOperation(
  65. new Position( root, [ 0 ] ),
  66. 1,
  67. doc.version
  68. )
  69. ) );
  70. doc.applyOperation( wrapInDelta(
  71. new RemoveOperation(
  72. new Position( root, [ 0 ] ),
  73. 1,
  74. doc.version
  75. )
  76. ) );
  77. doc.applyOperation( wrapInDelta(
  78. new RemoveOperation(
  79. new Position( root, [ 0 ] ),
  80. 1,
  81. doc.version
  82. )
  83. ) );
  84. expect( graveyard.maxOffset ).to.equal( 3 );
  85. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'f' );
  86. expect( graveyard.getChild( 1 ).getChild( 0 ).data ).to.equal( 'o' );
  87. expect( graveyard.getChild( 2 ).getChild( 0 ).data ).to.equal( 'z' );
  88. } );
  89. it( 'should not create new holder element for remove operation if it was already created for given delta', () => {
  90. root.insertChildren( 0, new Text( 'fozbar' ) );
  91. let delta = new Delta();
  92. // This simulates i.e. RemoveOperation that got split into two operations during OT.
  93. let removeOpA = new RemoveOperation(
  94. new Position( root, [ 1 ] ),
  95. 1,
  96. doc.version
  97. );
  98. let removeOpB = new RemoveOperation(
  99. new Position( root, [ 0 ] ),
  100. 1,
  101. doc.version + 1
  102. );
  103. delta.addOperation( removeOpA );
  104. delta.addOperation( removeOpB );
  105. doc.applyOperation( removeOpA );
  106. doc.applyOperation( removeOpB );
  107. expect( graveyard.childCount ).to.equal( 1 );
  108. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'fo' );
  109. } );
  110. it( 'should create RemoveOperation with same parameters when cloned', () => {
  111. let pos = new Position( root, [ 2 ] );
  112. let operation = new RemoveOperation( pos, 2, doc.version );
  113. let clone = operation.clone();
  114. expect( clone ).to.be.instanceof( RemoveOperation );
  115. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  116. expect( clone.howMany ).to.equal( operation.howMany );
  117. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  118. } );
  119. it( 'should create a ReinsertOperation as a reverse', () => {
  120. let position = new Position( root, [ 0 ] );
  121. let operation = new RemoveOperation( position, 2, 0 );
  122. let reverse = operation.getReversed();
  123. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  124. expect( reverse.baseVersion ).to.equal( 1 );
  125. expect( reverse.howMany ).to.equal( 2 );
  126. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  127. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  128. } );
  129. it( 'should undo remove set of nodes by applying reverse operation', () => {
  130. let position = new Position( root, [ 0 ] );
  131. let operation = new RemoveOperation( position, 3, 0 );
  132. let reverse = operation.getReversed();
  133. root.insertChildren( 0, new Text( 'bar' ) );
  134. doc.applyOperation( wrapInDelta( operation ) );
  135. expect( doc.version ).to.equal( 1 );
  136. expect( root.maxOffset ).to.equal( 0 );
  137. doc.applyOperation( wrapInDelta( reverse ) );
  138. expect( doc.version ).to.equal( 2 );
  139. expect( root.maxOffset ).to.equal( 3 );
  140. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  141. } );
  142. it( 'should properly remove a node that is already in a graveyard', () => {
  143. doc.graveyard.appendChildren( new Element( '$graveyardHolder', {}, [ new Text( 'foo' ) ] ) );
  144. let position = new Position( doc.graveyard, [ 0, 0 ] );
  145. let operation = new RemoveOperation( position, 1, 0 );
  146. operation.targetPosition.path = [ 0, 0 ];
  147. doc.applyOperation( wrapInDelta( operation ) );
  148. expect( doc.graveyard.childCount ).to.equal( 2 );
  149. expect( doc.graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'f' );
  150. expect( doc.graveyard.getChild( 1 ).getChild( 0 ).data ).to.equal( 'oo' );
  151. } );
  152. describe( 'toJSON', () => {
  153. it( 'should create proper json object', () => {
  154. const op = new RemoveOperation(
  155. new Position( root, [ 2 ] ),
  156. 2,
  157. doc.version
  158. );
  159. const serialized = jsonParseStringify( op );
  160. expect( serialized ).to.deep.equal( {
  161. __className: 'engine.model.operation.RemoveOperation',
  162. baseVersion: 0,
  163. howMany: 2,
  164. isSticky: false,
  165. sourcePosition: jsonParseStringify( op.sourcePosition ),
  166. targetPosition: jsonParseStringify( op.targetPosition )
  167. } );
  168. } );
  169. } );
  170. describe( 'fromJSON', () => {
  171. it( 'should create proper RemoveOperation from json object', () => {
  172. const op = new RemoveOperation(
  173. new Position( root, [ 2 ] ),
  174. 2,
  175. doc.version
  176. );
  177. doc.graveyard.appendChildren( [ new Element( '$graveyardHolder' ), new Element( '$graveyardHolder' ) ] );
  178. const serialized = jsonParseStringify( op );
  179. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  180. expect( deserialized ).to.deep.equal( op );
  181. } );
  182. } );
  183. } );