8
0

removeoperation.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel, operation */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import ReinsertOperation from '/ckeditor5/core/treemodel/operation/reinsertoperation.js';
  9. import RemoveOperation from '/ckeditor5/core/treemodel/operation/removeoperation.js';
  10. import MoveOperation from '/ckeditor5/core/treemodel/operation/moveoperation.js';
  11. import Position from '/ckeditor5/core/treemodel/position.js';
  12. describe( 'RemoveOperation', () => {
  13. let doc, root, graveyard;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot( 'root' );
  17. graveyard = doc.graveyard;
  18. } );
  19. it( 'should have proper type', () => {
  20. const op = new RemoveOperation(
  21. new Position( root, [ 2 ] ),
  22. 2,
  23. doc.version
  24. );
  25. expect( op.type ).to.equal( 'remove' );
  26. } );
  27. it( 'should extend MoveOperation class', () => {
  28. let operation = new RemoveOperation(
  29. new Position( root, [ 2 ] ),
  30. 2,
  31. doc.version
  32. );
  33. expect( operation ).to.be.instanceof( MoveOperation );
  34. } );
  35. it( 'should remove set of nodes and append them to graveyard root', () => {
  36. root.insertChildren( 0, 'fozbar' );
  37. doc.applyOperation(
  38. new RemoveOperation(
  39. new Position( root, [ 2 ] ),
  40. 2,
  41. doc.version
  42. )
  43. );
  44. expect( doc.version ).to.equal( 1 );
  45. expect( root.getChildCount() ).to.equal( 4 );
  46. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  47. expect( graveyard.getChildCount() ).to.equal( 2 );
  48. expect( graveyard.getChild( 0 ).character ).to.equal( 'z' );
  49. expect( graveyard.getChild( 1 ).character ).to.equal( 'b' );
  50. } );
  51. it( 'should create RemoveOperation with same parameters when cloned', () => {
  52. let pos = new Position( root, [ 2 ] );
  53. let operation = new RemoveOperation( pos, 2, doc.version );
  54. let clone = operation.clone();
  55. expect( clone ).to.be.instanceof( RemoveOperation );
  56. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  57. expect( clone.howMany ).to.equal( operation.howMany );
  58. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  59. } );
  60. it( 'should create a ReinsertOperation as a reverse', () => {
  61. let position = new Position( root, [ 0 ] );
  62. let operation = new RemoveOperation( position, 2, 0 );
  63. let reverse = operation.getReversed();
  64. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  65. expect( reverse.baseVersion ).to.equal( 1 );
  66. expect( reverse.howMany ).to.equal( 2 );
  67. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  68. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  69. } );
  70. it( 'should undo remove set of nodes by applying reverse operation', () => {
  71. let position = new Position( root, [ 0 ] );
  72. let operation = new RemoveOperation( position, 3, 0 );
  73. let reverse = operation.getReversed();
  74. root.insertChildren( 0, 'bar' );
  75. doc.applyOperation( operation );
  76. expect( doc.version ).to.equal( 1 );
  77. expect( root.getChildCount() ).to.equal( 0 );
  78. doc.applyOperation( reverse );
  79. expect( doc.version ).to.equal( 2 );
  80. expect( root.getChildCount() ).to.equal( 3 );
  81. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  82. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  83. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  84. } );
  85. } );