removeoperation.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Delta from 'ckeditor5/engine/model/delta/delta.js';
  13. import { jsonParseStringify, wrapInDelta } from 'tests/engine/model/_utils/utils.js';
  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. doc.version
  26. );
  27. expect( op.type ).to.equal( 'remove' );
  28. } );
  29. it( 'should not be sticky', () => {
  30. const op = new RemoveOperation(
  31. new Position( root, [ 2 ] ),
  32. 2,
  33. doc.version
  34. );
  35. expect( op.isSticky ).to.be.false;
  36. } );
  37. it( 'should extend MoveOperation class', () => {
  38. let operation = new RemoveOperation(
  39. new Position( root, [ 2 ] ),
  40. 2,
  41. doc.version
  42. );
  43. expect( operation ).to.be.instanceof( MoveOperation );
  44. } );
  45. it( 'should remove set of nodes and append them to holder element in graveyard root', () => {
  46. root.insertChildren( 0, new Text( 'fozbar' ) );
  47. doc.applyOperation( wrapInDelta(
  48. new RemoveOperation(
  49. new Position( root, [ 2 ] ),
  50. 2,
  51. doc.version
  52. )
  53. ) );
  54. expect( doc.version ).to.equal( 1 );
  55. expect( root.maxOffset ).to.equal( 4 );
  56. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  57. expect( graveyard.maxOffset ).to.equal( 1 );
  58. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'zb' );
  59. } );
  60. it( 'should create new holder element for remove operations in different deltas', () => {
  61. root.insertChildren( 0, new Text( '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.maxOffset ).to.equal( 3 );
  84. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'f' );
  85. expect( graveyard.getChild( 1 ).getChild( 0 ).data ).to.equal( 'o' );
  86. expect( graveyard.getChild( 2 ).getChild( 0 ).data ).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, new Text( '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.childCount ).to.equal( 1 );
  107. expect( graveyard.getChild( 0 ).getChild( 0 ).data ).to.equal( 'fo' );
  108. } );
  109. it( 'should create RemoveOperation with same parameters when cloned', () => {
  110. let pos = new Position( root, [ 2 ] );
  111. let operation = new RemoveOperation( pos, 2, doc.version );
  112. let clone = operation.clone();
  113. expect( clone ).to.be.instanceof( RemoveOperation );
  114. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  115. expect( clone.howMany ).to.equal( operation.howMany );
  116. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  117. } );
  118. it( 'should create a ReinsertOperation as a reverse', () => {
  119. let position = new Position( root, [ 0 ] );
  120. let operation = new RemoveOperation( position, 2, 0 );
  121. let reverse = operation.getReversed();
  122. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  123. expect( reverse.baseVersion ).to.equal( 1 );
  124. expect( reverse.howMany ).to.equal( 2 );
  125. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  126. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  127. } );
  128. it( 'should undo remove set of nodes by applying reverse operation', () => {
  129. let position = new Position( root, [ 0 ] );
  130. let operation = new RemoveOperation( position, 3, 0 );
  131. let reverse = operation.getReversed();
  132. root.insertChildren( 0, new Text( 'bar' ) );
  133. doc.applyOperation( wrapInDelta( operation ) );
  134. expect( doc.version ).to.equal( 1 );
  135. expect( root.maxOffset ).to.equal( 0 );
  136. doc.applyOperation( wrapInDelta( reverse ) );
  137. expect( doc.version ).to.equal( 2 );
  138. expect( root.maxOffset ).to.equal( 3 );
  139. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  140. } );
  141. describe( 'toJSON', () => {
  142. it( 'should create proper json object', () => {
  143. const op = new RemoveOperation(
  144. new Position( root, [ 2 ] ),
  145. 2,
  146. doc.version
  147. );
  148. const serialized = jsonParseStringify( op );
  149. expect( serialized ).to.deep.equal( {
  150. __className: 'engine.model.operation.RemoveOperation',
  151. baseVersion: 0,
  152. howMany: 2,
  153. isSticky: false,
  154. sourcePosition: jsonParseStringify( op.sourcePosition ),
  155. targetPosition: jsonParseStringify( op.targetPosition )
  156. } );
  157. } );
  158. } );
  159. describe( 'fromJSON', () => {
  160. it( 'should create proper RemoveOperation from json object', () => {
  161. const op = new RemoveOperation(
  162. new Position( root, [ 2 ] ),
  163. 2,
  164. doc.version
  165. );
  166. const serialized = jsonParseStringify( op );
  167. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  168. expect( deserialized ).to.deep.equal( op );
  169. } );
  170. } );
  171. } );