removeoperation.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  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 DocumentFragment from '../../../src/model/documentfragment';
  11. import Element from '../../../src/model/element';
  12. import Text from '../../../src/model/text';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  15. describe( 'RemoveOperation', () => {
  16. let model, doc, root, graveyard;
  17. beforeEach( () => {
  18. model = new Model();
  19. doc = model.document;
  20. root = doc.createRoot();
  21. graveyard = doc.graveyard;
  22. } );
  23. it( 'should have proper type', () => {
  24. const op = new RemoveOperation(
  25. new Position( root, [ 2 ] ),
  26. 2,
  27. new Position( doc.graveyard, [ 0 ] ),
  28. doc.version
  29. );
  30. expect( op.type ).to.equal( 'remove' );
  31. } );
  32. it( 'should extend MoveOperation class', () => {
  33. const operation = new RemoveOperation(
  34. new Position( root, [ 2 ] ),
  35. 2,
  36. new Position( doc.graveyard, [ 0 ] ),
  37. doc.version
  38. );
  39. expect( operation ).to.be.instanceof( MoveOperation );
  40. } );
  41. it( 'should be able to remove set of nodes and append them to graveyard root', () => {
  42. root._insertChild( 0, new Text( 'fozbar' ) );
  43. model.applyOperation(
  44. new RemoveOperation(
  45. new Position( root, [ 2 ] ),
  46. 2,
  47. new Position( doc.graveyard, [ 0 ] ),
  48. doc.version
  49. )
  50. );
  51. expect( doc.version ).to.equal( 1 );
  52. expect( root.maxOffset ).to.equal( 4 );
  53. expect( root.getChild( 0 ).data ).to.equal( 'foar' );
  54. expect( graveyard.maxOffset ).to.equal( 2 );
  55. expect( graveyard.getChild( 0 ).data ).to.equal( 'zb' );
  56. } );
  57. it( 'should create RemoveOperation with same parameters when cloned', () => {
  58. const pos = new Position( root, [ 2 ] );
  59. const operation = new RemoveOperation( pos, 2, new Position( doc.graveyard, [ 0 ] ), doc.version );
  60. const clone = operation.clone();
  61. expect( clone ).to.be.instanceof( RemoveOperation );
  62. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  63. expect( clone.targetPosition.isEqual( operation.targetPosition ) ).to.be.true;
  64. expect( clone.howMany ).to.equal( operation.howMany );
  65. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  66. } );
  67. it( 'should create ReinsertOperation when reversed', () => {
  68. const position = new Position( root, [ 0 ] );
  69. const operation = new RemoveOperation( position, 2, new Position( doc.graveyard, [ 0 ] ), 0 );
  70. const reverse = operation.getReversed();
  71. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  72. expect( reverse.baseVersion ).to.equal( 1 );
  73. expect( reverse.howMany ).to.equal( 2 );
  74. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  75. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  76. } );
  77. it( 'should create correct ReinsertOperation when reversed if source range was in graveyard', () => {
  78. const operation = new RemoveOperation( new Position( doc.graveyard, [ 2 ] ), 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  79. const reverse = operation.getReversed();
  80. expect( reverse.sourcePosition.path ).to.deep.equal( [ 0 ] );
  81. expect( reverse.targetPosition.path ).to.deep.equal( [ 3 ] );
  82. } );
  83. it( 'should undo remove set of nodes by applying reverse operation', () => {
  84. const position = new Position( root, [ 0 ] );
  85. const operation = new RemoveOperation( position, 3, new Position( doc.graveyard, [ 0 ] ), 0 );
  86. const reverse = operation.getReversed();
  87. root._insertChild( 0, new Text( 'bar' ) );
  88. model.applyOperation( operation );
  89. expect( doc.version ).to.equal( 1 );
  90. expect( root.maxOffset ).to.equal( 0 );
  91. model.applyOperation( reverse );
  92. expect( doc.version ).to.equal( 2 );
  93. expect( root.maxOffset ).to.equal( 3 );
  94. expect( root.getChild( 0 ).data ).to.equal( 'bar' );
  95. } );
  96. it( 'should properly remove a node that is already in a graveyard', () => {
  97. doc.graveyard._appendChild( [ new Element( 'x' ), new Element( 'y' ), new Element( 'z' ) ] );
  98. const position = new Position( doc.graveyard, [ 2 ] );
  99. const operation = new RemoveOperation( position, 1, new Position( doc.graveyard, [ 0 ] ), 0 );
  100. model.applyOperation( operation );
  101. expect( doc.graveyard.childCount ).to.equal( 3 );
  102. expect( doc.graveyard.getChild( 0 ).name ).to.equal( 'z' );
  103. expect( doc.graveyard.getChild( 1 ).name ).to.equal( 'x' );
  104. expect( doc.graveyard.getChild( 2 ).name ).to.equal( 'y' );
  105. } );
  106. describe( '_validate()', () => {
  107. it( 'should throw when is executed on detached item', () => {
  108. const docFrag = new DocumentFragment();
  109. const item = new Element( 'foo' );
  110. docFrag._appendChild( [ item ] );
  111. const op = new RemoveOperation(
  112. new Position( docFrag, [ 0 ] ),
  113. 1,
  114. new Position( doc.graveyard, [ 0 ] ),
  115. doc.version
  116. );
  117. expect( () => {
  118. op._validate();
  119. } ).to.throw( CKEditorError, /^remove-operation-on-detached-item/ );
  120. } );
  121. } );
  122. describe( 'toJSON', () => {
  123. it( 'should create proper json object', () => {
  124. const op = new RemoveOperation(
  125. new Position( root, [ 2 ] ),
  126. 2,
  127. new Position( doc.graveyard, [ 0 ] ),
  128. doc.version
  129. );
  130. const serialized = jsonParseStringify( op );
  131. expect( serialized ).to.deep.equal( {
  132. __className: 'engine.model.operation.RemoveOperation',
  133. baseVersion: 0,
  134. howMany: 2,
  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. } );