removeoperation.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 not be sticky', () => {
  28. const op = new RemoveOperation(
  29. new Position( root, [ 2 ] ),
  30. 2,
  31. doc.version
  32. );
  33. expect( op.isSticky ).to.be.false;
  34. } );
  35. it( 'should extend MoveOperation class', () => {
  36. let operation = new RemoveOperation(
  37. new Position( root, [ 2 ] ),
  38. 2,
  39. doc.version
  40. );
  41. expect( operation ).to.be.instanceof( MoveOperation );
  42. } );
  43. it( 'should remove set of nodes and append them to graveyard root', () => {
  44. root.insertChildren( 0, 'fozbar' );
  45. doc.applyOperation(
  46. new RemoveOperation(
  47. new Position( root, [ 2 ] ),
  48. 2,
  49. doc.version
  50. )
  51. );
  52. expect( doc.version ).to.equal( 1 );
  53. expect( root.getChildCount() ).to.equal( 4 );
  54. expect( root.getChild( 2 ).character ).to.equal( 'a' );
  55. expect( graveyard.getChildCount() ).to.equal( 2 );
  56. expect( graveyard.getChild( 0 ).character ).to.equal( 'z' );
  57. expect( graveyard.getChild( 1 ).character ).to.equal( 'b' );
  58. } );
  59. it( 'should create RemoveOperation with same parameters when cloned', () => {
  60. let pos = new Position( root, [ 2 ] );
  61. let operation = new RemoveOperation( pos, 2, doc.version );
  62. let clone = operation.clone();
  63. expect( clone ).to.be.instanceof( RemoveOperation );
  64. expect( clone.sourcePosition.isEqual( pos ) ).to.be.true;
  65. expect( clone.howMany ).to.equal( operation.howMany );
  66. expect( clone.baseVersion ).to.equal( operation.baseVersion );
  67. } );
  68. it( 'should create a ReinsertOperation as a reverse', () => {
  69. let position = new Position( root, [ 0 ] );
  70. let operation = new RemoveOperation( position, 2, 0 );
  71. let reverse = operation.getReversed();
  72. expect( reverse ).to.be.an.instanceof( ReinsertOperation );
  73. expect( reverse.baseVersion ).to.equal( 1 );
  74. expect( reverse.howMany ).to.equal( 2 );
  75. expect( reverse.sourcePosition.isEqual( operation.targetPosition ) ).to.be.true;
  76. expect( reverse.targetPosition.isEqual( position ) ).to.be.true;
  77. } );
  78. it( 'should undo remove set of nodes by applying reverse operation', () => {
  79. let position = new Position( root, [ 0 ] );
  80. let operation = new RemoveOperation( position, 3, 0 );
  81. let reverse = operation.getReversed();
  82. root.insertChildren( 0, 'bar' );
  83. doc.applyOperation( operation );
  84. expect( doc.version ).to.equal( 1 );
  85. expect( root.getChildCount() ).to.equal( 0 );
  86. doc.applyOperation( reverse );
  87. expect( doc.version ).to.equal( 2 );
  88. expect( root.getChildCount() ).to.equal( 3 );
  89. expect( root.getChild( 0 ).character ).to.equal( 'b' );
  90. expect( root.getChild( 1 ).character ).to.equal( 'a' );
  91. expect( root.getChild( 2 ).character ).to.equal( 'r' );
  92. } );
  93. } );