8
0

removeoperation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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( 'root' );
  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 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( 2 );
  57. expect( graveyard.getChild( 0 ).character ).to.equal( 'z' );
  58. expect( graveyard.getChild( 1 ).character ).to.equal( 'b' );
  59. } );
  60. it( 'should create RemoveOperation with same parameters when cloned', () => {
  61. let pos = new Position( root, [ 2 ] );
  62. let operation = new RemoveOperation( pos, 2, doc.version );
  63. let clone = operation.clone();
  64. expect( clone ).to.be.instanceof( RemoveOperation );
  65. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  66. expect( clone.howMany ).to.equal( operation.howMany );
  67. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  68. } );
  69. it( 'should create a ReinsertOperation as a reverse', () => {
  70. let position = new Position( root, [ 0 ] );
  71. let operation = new RemoveOperation( position, 2, 0 );
  72. let reverse = operation.getReversed();
  73. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  74. expect( reverse.baseVersion ).to.equal( 1 );
  75. expect( reverse.howMany ).to.equal( 2 );
  76. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  77. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  78. } );
  79. it( 'should undo remove set of nodes by applying reverse operation', () => {
  80. let position = new Position( root, [ 0 ] );
  81. let operation = new RemoveOperation( position, 3, 0 );
  82. let reverse = operation.getReversed();
  83. root.insertChildren( 0, 'bar' );
  84. doc.applyOperation( operation );
  85. expect( doc.version ).to.equal( 1 );
  86. expect( root.getChildCount() ).to.equal( 0 );
  87. doc.applyOperation( reverse );
  88. expect( doc.version ).to.equal( 2 );
  89. expect( root.getChildCount() ).to.equal( 3 );
  90. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  91. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  92. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  93. } );
  94. describe( 'toJSON', () => {
  95. it( 'should create proper json object', () => {
  96. const op = new RemoveOperation(
  97. new Position( root, [ 2 ] ),
  98. 2,
  99. doc.version
  100. );
  101. const serialized = jsonParseStringify( op );
  102. expect( serialized ).to.deep.equal( {
  103. __className: 'engine.model.operation.RemoveOperation',
  104. baseVersion: 0,
  105. howMany: 2,
  106. isSticky: false,
  107. movedRangeStart: jsonParseStringify( op.movedRangeStart ),
  108. sourcePosition: jsonParseStringify( op.sourcePosition ),
  109. targetPosition: jsonParseStringify( op.targetPosition )
  110. } );
  111. } );
  112. } );
  113. describe( 'fromJSON', () => {
  114. it( 'should create proper RemoveOperation from json object', () => {
  115. const op = new RemoveOperation(
  116. new Position( root, [ 2 ] ),
  117. 2,
  118. doc.version
  119. );
  120. const serialized = jsonParseStringify( op );
  121. const deserialized = RemoveOperation.fromJSON( serialized, doc );
  122. expect( deserialized ).to.deep.equal( op );
  123. } );
  124. } );
  125. } );