removeoperation.js 6.0 KB

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