composer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model, composer */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Composer from '/ckeditor5/engine/model/composer/composer.js';
  9. import { setData, getData } from '/tests/engine/_utils/model.js';
  10. describe( 'Composer', () => {
  11. let document, composer;
  12. beforeEach( () => {
  13. document = new Document();
  14. document.createRoot();
  15. composer = new Composer();
  16. } );
  17. describe( 'constructor', () => {
  18. it( 'attaches deleteContents default listener', () => {
  19. setData( document, '<p>f<selection>oo</p><p>ba</selection>r</p>' );
  20. const batch = document.batch();
  21. composer.fire( 'deleteContents', { batch, selection: document.selection } );
  22. expect( getData( document ) ).to.equal( '<p>f<selection /></p><p>r</p>' );
  23. expect( batch.deltas ).to.not.be.empty;
  24. } );
  25. it( 'attaches deleteContents default listener which passes options', () => {
  26. setData( document, '<p>f<selection>oo</p><p>ba</selection>r</p>' );
  27. const batch = document.batch();
  28. composer.fire( 'deleteContents', {
  29. batch,
  30. selection: document.selection,
  31. options: { merge: true }
  32. } );
  33. expect( getData( document ) ).to.equal( '<p>f<selection />r</p>' );
  34. } );
  35. it( 'attaches modifySelection default listener', () => {
  36. setData( document, '<p>foo<selection />bar</p>' );
  37. composer.fire( 'modifySelection', {
  38. selection: document.selection,
  39. options: {
  40. direction: 'backward'
  41. }
  42. } );
  43. expect( getData( document ) )
  44. .to.equal( '<p>fo<selection backward>o</selection>bar</p>' );
  45. } );
  46. } );
  47. describe( 'deleteContents', () => {
  48. it( 'fires deleteContents event', () => {
  49. const spy = sinon.spy();
  50. const batch = document.batch();
  51. composer.on( 'deleteContents', spy );
  52. composer.deleteContents( batch, document.selection );
  53. const data = spy.args[ 0 ][ 1 ];
  54. expect( data.batch ).to.equal( batch );
  55. expect( data.selection ).to.equal( document.selection );
  56. } );
  57. } );
  58. describe( 'modifySelection', () => {
  59. it( 'fires deleteContents event', () => {
  60. const spy = sinon.spy();
  61. const opts = { direction: 'backward' };
  62. composer.on( 'modifySelection', spy );
  63. composer.modifySelection( document.selection, opts );
  64. const data = spy.args[ 0 ][ 1 ];
  65. expect( data.selection ).to.equal( document.selection );
  66. expect( data.options ).to.equal( opts );
  67. } );
  68. } );
  69. } );