8
0

removedelta.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, delta */
  6. import { getNodesAndText } from '/tests/engine/model/_utils/utils.js';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Position from '/ckeditor5/engine/model/position.js';
  9. import Range from '/ckeditor5/engine/model/range.js';
  10. import Element from '/ckeditor5/engine/model/element.js';
  11. import Text from '/ckeditor5/engine/model/text.js';
  12. import RemoveDelta from '/ckeditor5/engine/model/delta/removedelta.js';
  13. describe( 'Batch', () => {
  14. let doc, root, div, p, batch, chain, range;
  15. beforeEach( () => {
  16. doc = new Document();
  17. root = doc.createRoot();
  18. div = new Element( 'div', [], new Text( 'foobar' ) );
  19. p = new Element( 'p', [], new Text( 'abcxyz' ) );
  20. div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
  21. div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
  22. root.insertChildren( 0, [ div, p ] );
  23. batch = doc.batch();
  24. // Range starts in ROOT > DIV > P > gg|gg.
  25. // Range ends in ROOT > DIV > ...|ar.
  26. range = new Range( new Position( root, [ 0, 0, 2 ] ), new Position( root, [ 0, 5 ] ) );
  27. } );
  28. describe( 'remove', () => {
  29. it( 'should remove specified node', () => {
  30. batch.remove( div );
  31. expect( root.maxOffset ).to.equal( 1 );
  32. expect( root.childCount ).to.equal( 1 );
  33. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  34. } );
  35. it( 'should remove any range of nodes', () => {
  36. batch.remove( range );
  37. expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggParPhhhhP' );
  38. expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  39. } );
  40. it( 'should create minimal number of operations when removing a range', () => {
  41. batch.remove( range );
  42. expect( batch.deltas.length ).to.equal( 1 );
  43. expect( batch.deltas[ 0 ].operations.length ).to.equal( 2 );
  44. } );
  45. it( 'should be chainable', () => {
  46. chain = batch.remove( range );
  47. expect( chain ).to.equal( batch );
  48. } );
  49. it( 'should add delta to batch and operation to delta before applying operation', () => {
  50. sinon.spy( doc, 'applyOperation' );
  51. batch.remove( div );
  52. const correctDeltaMatcher = sinon.match( ( operation ) => {
  53. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  54. } );
  55. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  56. } );
  57. } );
  58. } );
  59. describe( 'RemoveDelta', ()=> {
  60. it( 'should provide proper className', () => {
  61. expect( RemoveDelta.className ).to.equal( 'engine.model.delta.RemoveDelta' );
  62. } );
  63. } );