removeoperation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Delta from '/ckeditor5/engine/model/delta/delta.js';
  12. import { jsonParseStringify, wrapInDelta } from '/tests/engine/model/_utils/utils.js';
  13. describe( 'RemoveOperation', () => {
  14. let doc, root, graveyard;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. graveyard = doc.graveyard;
  19. } );
  20. it( 'should have proper type', () => {
  21. const op = new RemoveOperation(
  22. new Position( root, [ 2 ] ),
  23. 2,
  24. doc.version
  25. );
  26. expect( op.type ).to.equal( 'remove' );
  27. } );
  28. it( 'should not be sticky', () => {
  29. const op = new RemoveOperation(
  30. new Position( root, [ 2 ] ),
  31. 2,
  32. doc.version
  33. );
  34. expect( op.isSticky ).to.be.false;
  35. } );
  36. it( 'should extend MoveOperation class', () => {
  37. let operation = new RemoveOperation(
  38. new Position( root, [ 2 ] ),
  39. 2,
  40. doc.version
  41. );
  42. expect( operation ).to.be.instanceof( MoveOperation );
  43. } );
  44. it( 'should remove set of nodes and append them to holder element in graveyard root', () => {
  45. root.insertChildren( 0, 'fozbar' );
  46. doc.applyOperation( wrapInDelta(
  47. new RemoveOperation(
  48. new Position( root, [ 2 ] ),
  49. 2,
  50. doc.version
  51. )
  52. ) );
  53. expect( doc.version ).to.equal( 1 );
  54. expect( root.getChildCount() ).to.equal( 4 );
  55. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  56. expect( graveyard.getChildCount() ).to.equal( 1 );
  57. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'z' );
  58. expect( graveyard.getChild( 0 ).getChild( 1 ).character ).to.equal( 'b' );
  59. } );
  60. it( 'should create new holder element for remove operations in different deltas', () => {
  61. root.insertChildren( 0, 'fozbar' );
  62. doc.applyOperation( wrapInDelta(
  63. new RemoveOperation(
  64. new Position( root, [ 0 ] ),
  65. 1,
  66. doc.version
  67. )
  68. ) );
  69. doc.applyOperation( wrapInDelta(
  70. new RemoveOperation(
  71. new Position( root, [ 0 ] ),
  72. 1,
  73. doc.version
  74. )
  75. ) );
  76. doc.applyOperation( wrapInDelta(
  77. new RemoveOperation(
  78. new Position( root, [ 0 ] ),
  79. 1,
  80. doc.version
  81. )
  82. ) );
  83. expect( graveyard.getChildCount() ).to.equal( 3 );
  84. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  85. expect( graveyard.getChild( 1 ).getChild( 0 ).character ).to.equal( 'o' );
  86. expect( graveyard.getChild( 2 ).getChild( 0 ).character ).to.equal( 'z' );
  87. } );
  88. it( 'should not create new holder element for remove operation if it was already created for given delta', () => {
  89. root.insertChildren( 0, 'fozbar' );
  90. let delta = new Delta();
  91. // This simulates i.e. RemoveOperation that got split into two operations during OT.
  92. let removeOpA = new RemoveOperation(
  93. new Position( root, [ 1 ] ),
  94. 1,
  95. doc.version
  96. );
  97. let removeOpB = new RemoveOperation(
  98. new Position( root, [ 0 ] ),
  99. 1,
  100. doc.version + 1
  101. );
  102. delta.addOperation( removeOpA );
  103. delta.addOperation( removeOpB );
  104. doc.applyOperation( removeOpA );
  105. doc.applyOperation( removeOpB );
  106. expect( graveyard.getChildCount() ).to.equal( 1 );
  107. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  108. expect( graveyard.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  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, 'bar' );
  134. doc.applyOperation( wrapInDelta( operation ) );
  135. expect( doc.version ).to.equal( 1 );
  136. expect( root.getChildCount() ).to.equal( 0 );
  137. doc.applyOperation( wrapInDelta( reverse ) );
  138. expect( doc.version ).to.equal( 2 );
  139. expect( root.getChildCount() ).to.equal( 3 );
  140. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  141. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  142. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  143. } );
  144. describe( 'toJSON', () => {
  145. it( 'should create proper json object', () => {
  146. const op = new RemoveOperation(
  147. new Position( root, [ 2 ] ),
  148. 2,
  149. doc.version
  150. );
  151. const serialized = jsonParseStringify( op );
  152. expect( serialized ).to.deep.equal( {
  153. __className: 'engine.model.operation.RemoveOperation',
  154. baseVersion: 0,
  155. howMany: 2,
  156. isSticky: false,
  157. movedRangeStart: jsonParseStringify( op.movedRangeStart ),
  158. sourcePosition: jsonParseStringify( op.sourcePosition ),
  159. targetPosition: jsonParseStringify( op.targetPosition )
  160. } );
  161. } );
  162. } );
  163. describe( 'fromJSON', () => {
  164. it( 'should create proper RemoveOperation from json object', () => {
  165. const op = new RemoveOperation(
  166. new Position( root, [ 2 ] ),
  167. 2,
  168. doc.version
  169. );
  170. const serialized = jsonParseStringify( op );
  171. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  172. expect( deserialized ).to.deep.equal( op );
  173. } );
  174. } );
  175. } );