8
0

removedelta.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 RemoveDelta from '/ckeditor5/engine/model/delta/removedelta.js';
  12. describe( 'Batch', () => {
  13. let doc, root, div, p, batch, chain, range;
  14. beforeEach( () => {
  15. doc = new Document();
  16. root = doc.createRoot();
  17. div = new Element( 'div', [], 'foobar' );
  18. p = new Element( 'p', [], 'abcxyz' );
  19. div.insertChildren( 4, [ new Element( 'p', [], 'gggg' ) ] );
  20. div.insertChildren( 2, [ new Element( 'p', [], 'hhhh' ) ] );
  21. root.insertChildren( 0, [ div, p ] );
  22. batch = doc.batch();
  23. // Range starts in ROOT > DIV > P > gg|gg.
  24. // Range ends in ROOT > DIV > ...|ar.
  25. range = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
  26. } );
  27. describe( 'remove', () => {
  28. it( 'should remove specified node', () => {
  29. batch.remove( div );
  30. expect( root.getChildCount() ).to.equal( 1 );
  31. expect( getNodesAndText( Range.createFromElement( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
  32. } );
  33. it( 'should move any range of nodes', () => {
  34. batch.remove( range );
  35. expect( getNodesAndText( Range.createFromElement( root.getChild( 0 ) ) ) ).to.equal( 'foPhhPar' );
  36. expect( getNodesAndText( Range.createFromElement( root.getChild( 1 ) ) ) ).to.equal( 'abcxyz' );
  37. } );
  38. it( 'should create minimal number of operations when removing a range', () => {
  39. batch.remove( range );
  40. expect( batch.deltas.length ).to.equal( 1 );
  41. expect( batch.deltas[ 0 ].operations.length ).to.equal( 2 );
  42. } );
  43. it( 'should be chainable', () => {
  44. chain = batch.remove( range );
  45. expect( chain ).to.equal( batch );
  46. } );
  47. it( 'should add delta to batch and operation to delta before applying operation', () => {
  48. sinon.spy( doc, 'applyOperation' );
  49. batch.remove( div );
  50. const correctDeltaMatcher = sinon.match( ( operation ) => {
  51. return operation.delta && operation.delta.batch && operation.delta.batch == batch;
  52. } );
  53. expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
  54. } );
  55. } );
  56. } );
  57. describe( 'RemoveDelta', ()=> {
  58. it( 'should provide proper className', () => {
  59. expect( RemoveDelta.className ).to.equal( 'engine.model.delta.RemoveDelta' );
  60. } );
  61. } );