removeoperation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import ReinsertOperation from '/ckeditor5/engine/model/operation/reinsertoperation.js';
  9. import RemoveOperation from '/ckeditor5/engine/model/operation/removeoperation.js';
  10. import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
  11. import Position from '/ckeditor5/engine/model/position.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, '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.getChildCount() ).to.equal( 4 );
  56. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  57. expect( graveyard.getChildCount() ).to.equal( 1 );
  58. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'z' );
  59. expect( graveyard.getChild( 0 ).getChild( 1 ).character ).to.equal( 'b' );
  60. } );
  61. it( 'should create new holder element for remove operations in different deltas', () => {
  62. root.insertChildren( 0, '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.getChildCount() ).to.equal( 3 );
  85. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  86. expect( graveyard.getChild( 1 ).getChild( 0 ).character ).to.equal( 'o' );
  87. expect( graveyard.getChild( 2 ).getChild( 0 ).character ).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, '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.getChildCount() ).to.equal( 1 );
  108. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  109. expect( graveyard.getChild( 0 ).getChild( 1 ).character ).to.equal( 'o' );
  110. } );
  111. it( 'should create RemoveOperation with same parameters when cloned', () => {
  112. let pos = new Position( root, [ 2 ] );
  113. let operation = new RemoveOperation( pos, 2, doc.version );
  114. let clone = operation.clone();
  115. expect( clone ).to.be.instanceof( RemoveOperation );
  116. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  117. expect( clone.howMany ).to.equal( operation.howMany );
  118. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  119. } );
  120. it( 'should create a ReinsertOperation as a reverse', () => {
  121. let position = new Position( root, [ 0 ] );
  122. let operation = new RemoveOperation( position, 2, 0 );
  123. let reverse = operation.getReversed();
  124. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  125. expect( reverse.baseVersion ).to.equal( 1 );
  126. expect( reverse.howMany ).to.equal( 2 );
  127. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  128. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  129. } );
  130. it( 'should undo remove set of nodes by applying reverse operation', () => {
  131. let position = new Position( root, [ 0 ] );
  132. let operation = new RemoveOperation( position, 3, 0 );
  133. let reverse = operation.getReversed();
  134. root.insertChildren( 0, 'bar' );
  135. doc.applyOperation( wrapInDelta( operation ) );
  136. expect( doc.version ).to.equal( 1 );
  137. expect( root.getChildCount() ).to.equal( 0 );
  138. doc.applyOperation( wrapInDelta( reverse ) );
  139. expect( doc.version ).to.equal( 2 );
  140. expect( root.getChildCount() ).to.equal( 3 );
  141. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  142. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  143. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  144. } );
  145. describe( 'toJSON', () => {
  146. it( 'should create proper json object', () => {
  147. const op = new RemoveOperation(
  148. new Position( root, [ 2 ] ),
  149. 2,
  150. doc.version
  151. );
  152. const serialized = jsonParseStringify( op );
  153. expect( serialized ).to.deep.equal( {
  154. __className: 'engine.model.operation.RemoveOperation',
  155. baseVersion: 0,
  156. howMany: 2,
  157. isSticky: false,
  158. movedRangeStart: jsonParseStringify( op.movedRangeStart ),
  159. sourcePosition: jsonParseStringify( op.sourcePosition ),
  160. targetPosition: jsonParseStringify( op.targetPosition )
  161. } );
  162. } );
  163. } );
  164. describe( 'fromJSON', () => {
  165. it( 'should create proper RemoveOperation from json object', () => {
  166. const op = new RemoveOperation(
  167. new Position( root, [ 2 ] ),
  168. 2,
  169. doc.version
  170. );
  171. const serialized = jsonParseStringify( op );
  172. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  173. expect( deserialized ).to.deep.equal( op );
  174. } );
  175. } );
  176. } );