removeoperation.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  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. doc.version
  25. );
  26. expect( op.type ).to.equal( 'remove' );
  27. } );
  28. it( 'should not be sticky', () => {
  29. const op = new RemoveOperation(
  30. new Position( root, [ 2 ] ),
  31. 2,
  32. doc.version
  33. );
  34. expect( op.isSticky ).to.be.false;
  35. } );
  36. it( 'should extend MoveOperation class', () => {
  37. let operation = new RemoveOperation(
  38. new Position( root, [ 2 ] ),
  39. 2,
  40. doc.version
  41. );
  42. expect( operation ).to.be.instanceof( MoveOperation );
  43. } );
  44. it( 'should remove set of nodes and append them to holder element in graveyard root', () => {
  45. root.insertChildren( 0, 'fozbar' );
  46. doc.applyOperation(
  47. new RemoveOperation(
  48. new Position( root, [ 2 ] ),
  49. 2,
  50. doc.version
  51. )
  52. );
  53. expect( doc.version ).to.equal( 1 );
  54. expect( root.getChildCount() ).to.equal( 4 );
  55. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  56. expect( graveyard.getChildCount() ).to.equal( 1 );
  57. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'z' );
  58. expect( graveyard.getChild( 0 ).getChild( 1 ).character ).to.equal( 'b' );
  59. } );
  60. it( 'should create new holder element for each remove operation', () => {
  61. root.insertChildren( 0, 'fozbar' );
  62. doc.applyOperation(
  63. new RemoveOperation(
  64. new Position( root, [ 0 ] ),
  65. 1,
  66. doc.version
  67. )
  68. );
  69. doc.applyOperation(
  70. new RemoveOperation(
  71. new Position( root, [ 0 ] ),
  72. 1,
  73. doc.version
  74. )
  75. );
  76. doc.applyOperation(
  77. new RemoveOperation(
  78. new Position( root, [ 0 ] ),
  79. 1,
  80. doc.version
  81. )
  82. );
  83. expect( graveyard.getChildCount() ).to.equal( 3 );
  84. expect( graveyard.getChild( 0 ).getChild( 0 ).character ).to.equal( 'f' );
  85. expect( graveyard.getChild( 1 ).getChild( 0 ).character ).to.equal( 'o' );
  86. expect( graveyard.getChild( 2 ).getChild( 0 ).character ).to.equal( 'z' );
  87. } );
  88. it( 'should create RemoveOperation with same parameters when cloned', () => {
  89. let pos = new Position( root, [ 2 ] );
  90. let operation = new RemoveOperation( pos, 2, doc.version );
  91. let clone = operation.clone();
  92. expect( clone ).to.be.instanceof( RemoveOperation );
  93. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  94. expect( clone.howMany ).to.equal( operation.howMany );
  95. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  96. } );
  97. it( 'should create a ReinsertOperation as a reverse', () => {
  98. let position = new Position( root, [ 0 ] );
  99. let operation = new RemoveOperation( position, 2, 0 );
  100. let reverse = operation.getReversed();
  101. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  102. expect( reverse.baseVersion ).to.equal( 1 );
  103. expect( reverse.howMany ).to.equal( 2 );
  104. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  105. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  106. } );
  107. it( 'should undo remove set of nodes by applying reverse operation', () => {
  108. let position = new Position( root, [ 0 ] );
  109. let operation = new RemoveOperation( position, 3, 0 );
  110. let reverse = operation.getReversed();
  111. root.insertChildren( 0, 'bar' );
  112. doc.applyOperation( operation );
  113. expect( doc.version ).to.equal( 1 );
  114. expect( root.getChildCount() ).to.equal( 0 );
  115. doc.applyOperation( reverse );
  116. expect( doc.version ).to.equal( 2 );
  117. expect( root.getChildCount() ).to.equal( 3 );
  118. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  119. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  120. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  121. } );
  122. describe( 'toJSON', () => {
  123. it( 'should create proper json object', () => {
  124. const op = new RemoveOperation(
  125. new Position( root, [ 2 ] ),
  126. 2,
  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. movedRangeStart: jsonParseStringify( op.movedRangeStart ),
  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. 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. } );