removedelta.js 2.6 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. 'use strict';
  7. import { getNodesAndText } from '/tests/engine/model/_utils/utils.js';
  8. import Document from '/ckeditor5/engine/model/document.js';
  9. import Position from '/ckeditor5/engine/model/position.js';
  10. import Range from '/ckeditor5/engine/model/range.js';
  11. import Element from '/ckeditor5/engine/model/element.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( '$root', 'root' );
  18. div = new Element( 'div', [], 'foobar' );
  19. p = new Element( 'p', [], 'abcxyz' );
  20. div.insertChildren( 4, [ new Element( 'p', [], 'gggg' ) ] );
  21. div.insertChildren( 2, [ new Element( 'p', [], '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, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  27. } );
  28. describe( 'remove', () => {
  29. it( 'should remove specified node', () => {
  30. batch.remove( div );
  31. expect( root.getChildCount() ).to.equal( 1 );
  32. expect( getNodesAndText( Range.createFromElement( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  33. } );
  34. it( 'should move any range of nodes', () => {
  35. batch.remove( range );
  36. expect( getNodesAndText( Range.createFromElement( root.getChild( 0 ) ) ) ).to.equal( 'foPhhPar' );
  37. expect( getNodesAndText( Range.createFromElement( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  38. } );
  39. it( 'should create minimal number of operations when removing a range', () => {
  40. batch.remove( range );
  41. expect( batch.deltas.length ).to.equal( 1 );
  42. expect( batch.deltas[ 0 ].operations.length ).to.equal( 2 );
  43. } );
  44. it( 'should be chainable', () => {
  45. chain = batch.remove( range );
  46. expect( chain ).to.equal( batch );
  47. } );
  48. it( 'should add delta to batch and operation to delta before applying operation', () => {
  49. sinon.spy( doc, 'applyOperation' );
  50. batch.remove( div );
  51. const correctDeltaMatcher = sinon.match( ( operation ) => {
  52. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  53. } );
  54. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  55. } );
  56. } );
  57. } );
  58. describe( 'RemoveDelta', ()=> {
  59. it( 'should provide proper className', () => {
  60. expect( RemoveDelta.className ).to.equal( 'engine.model.delta.RemoveDelta' );
  61. } );
  62. } );