removeoperation.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import ReinsertOperation from '../../../src/model/operation/reinsertoperation';
  7. import NoOperation from '../../../src/model/operation/nooperation';
  8. import RemoveOperation from '../../../src/model/operation/removeoperation';
  9. import MoveOperation from '../../../src/model/operation/moveoperation';
  10. import Position from '../../../src/model/position';
  11. import Text from '../../../src/model/text';
  12. import Element from '../../../src/model/element';
  13. import { jsonParseStringify, wrapInDelta } from '../../../tests/model/_utils/utils';
  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. new Position( doc.graveyard, [ 0 ] ),
  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. new Position( doc.graveyard, [ 0 ] ),
  35. doc.version
  36. );
  37. expect( op.isSticky ).to.be.false;
  38. } );
  39. it( 'should extend MoveOperation class', () => {
  40. const operation = new RemoveOperation(
  41. new Position( root, [ 2 ] ),
  42. 2,
  43. new Position( doc.graveyard, [ 0 ] ),
  44. doc.version
  45. );
  46. expect( operation ).to.be.instanceof( MoveOperation );
  47. } );
  48. it( 'should be able to remove set of nodes and append them to graveyard root', () => {
  49. root.insertChildren( 0, new Text( 'fozbar' ) );
  50. doc.applyOperation( wrapInDelta(
  51. new RemoveOperation(
  52. new Position( root, [ 2 ] ),
  53. 2,
  54. new Position( doc.graveyard, [ 0 ] ),
  55. doc.version
  56. )
  57. ) );
  58. expect( doc.version ).to.equal( 1 );
  59. expect( root.maxOffset ).to.equal( 4 );
  60. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  61. expect( graveyard.maxOffset ).to.equal( 2 );
  62. expect( graveyard.getChild( 0 ).data ).to.equal( 'zb' );
  63. } );
  64. it( 'should create RemoveOperation with same parameters when cloned', () => {
  65. const pos = new Position( root, [ 2 ] );
  66. const operation = new RemoveOperation( pos, 2, new Position( doc.graveyard, [ 0 ] ), doc.version );
  67. const clone = operation.clone();
  68. expect( clone ).to.be.instanceof( RemoveOperation );
  69. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  70. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  71. expect( clone.howMany ).to.equal( operation.howMany );
  72. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  73. } );
  74. it( 'should create ReinsertOperation when reversed', () => {
  75. const position = new Position( root, [ 0 ] );
  76. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  77. const reverse = operation.getReversed();
  78. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  79. expect( reverse.baseVersion ).to.equal( 1 );
  80. expect( reverse.howMany ).to.equal( 2 );
  81. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  82. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  83. } );
  84. it( 'should create correct ReinsertOperation when reversed if source range was in graveyard', () => {
  85. const operation = new RemoveOperation( new Position( doc.graveyard, [ 2 ] ), 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  86. const reverse = operation.getReversed();
  87. expect( reverse.sourcePosition.path ).to.deep.equal( [ 0 ] );
  88. expect( reverse.targetPosition.path ).to.deep.equal( [ 3 ] );
  89. } );
  90. it( 'should create NoOperation when reversed if was permanent', () => {
  91. const position = new Position( root, [ 0 ] );
  92. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  93. operation.isPermanent = true;
  94. const reverse = operation.getReversed();
  95. expect( reverse ).to.be.an.instanceof( NoOperation );
  96. expect( reverse.baseVersion ).to.equal( 1 );
  97. } );
  98. it( 'should undo remove set of nodes by applying reverse operation', () => {
  99. const position = new Position( root, [ 0 ] );
  100. const operation = new RemoveOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), 0 );
  101. const reverse = operation.getReversed();
  102. root.insertChildren( 0, new Text( 'bar' ) );
  103. doc.applyOperation( wrapInDelta( operation ) );
  104. expect( doc.version ).to.equal( 1 );
  105. expect( root.maxOffset ).to.equal( 0 );
  106. doc.applyOperation( wrapInDelta( reverse ) );
  107. expect( doc.version ).to.equal( 2 );
  108. expect( root.maxOffset ).to.equal( 3 );
  109. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  110. } );
  111. it( 'should properly remove a node that is already in a graveyard', () => {
  112. doc.graveyard.appendChildren( [ new Element( 'x' ), new Element( 'y' ), new Element( 'z' ) ] );
  113. const position = new Position( doc.graveyard, [ 2 ] );
  114. const operation = new RemoveOperation( position, 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  115. doc.applyOperation( wrapInDelta( operation ) );
  116. expect( doc.graveyard.childCount ).to.equal( 3 );
  117. expect( doc.graveyard.getChild( 0 ).name ).to.equal( 'z' );
  118. expect( doc.graveyard.getChild( 1 ).name ).to.equal( 'x' );
  119. expect( doc.graveyard.getChild( 2 ).name ).to.equal( 'y' );
  120. } );
  121. describe( 'toJSON', () => {
  122. it( 'should create proper json object', () => {
  123. const op = new RemoveOperation(
  124. new Position( root, [ 2 ] ),
  125. 2,
  126. new Position( doc.graveyard, [ 0 ] ),
  127. doc.version
  128. );
  129. const serialized = jsonParseStringify( op );
  130. expect( serialized ).to.deep.equal( {
  131. __className: 'engine.model.operation.RemoveOperation',
  132. baseVersion: 0,
  133. howMany: 2,
  134. isSticky: false,
  135. isPermanent: false,
  136. sourcePosition: jsonParseStringify( op.sourcePosition ),
  137. targetPosition: jsonParseStringify( op.targetPosition )
  138. } );
  139. } );
  140. } );
  141. describe( 'fromJSON', () => {
  142. it( 'should create proper RemoveOperation from json object', () => {
  143. const op = new RemoveOperation(
  144. new Position( root, [ 2 ] ),
  145. 2,
  146. new Position( doc.graveyard, [ 0 ] ),
  147. doc.version
  148. );
  149. const serialized = jsonParseStringify( op );
  150. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  151. expect( deserialized ).to.deep.equal( op );
  152. } );
  153. } );
  154. } );