composer.js 2.4 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. import Document from 'ckeditor5/engine/model/document.js';
  7. import Composer from 'ckeditor5/engine/model/composer/composer.js';
  8. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  9. describe( 'Composer', () => {
  10. let document, composer;
  11. beforeEach( () => {
  12. document = new Document();
  13. document.schema.registerItem( 'p', '$block' );
  14. document.createRoot();
  15. composer = new Composer();
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'attaches deleteContents default listener', () => {
  19. setData( document, '<p>f[oo</p><p>ba]r</p>' );
  20. const batch = document.batch();
  21. composer.fire( 'deleteContents', { batch, selection: document.selection } );
  22. expect( getData( document ) ).to.equal( '<p>f[]</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[oo</p><p>ba]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[]r</p>' );
  34. } );
  35. it( 'attaches modifySelection default listener', () => {
  36. setData( document, '<p>foo[]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[o]bar</p>' );
  45. expect( document.selection.isBackward ).to.true;
  46. } );
  47. } );
  48. describe( 'deleteContents', () => {
  49. it( 'fires deleteContents event', () => {
  50. const spy = sinon.spy();
  51. const batch = document.batch();
  52. composer.on( 'deleteContents', spy );
  53. composer.deleteContents( batch, document.selection );
  54. const data = spy.args[ 0 ][ 1 ];
  55. expect( data.batch ).to.equal( batch );
  56. expect( data.selection ).to.equal( document.selection );
  57. } );
  58. } );
  59. describe( 'modifySelection', () => {
  60. it( 'fires deleteContents event', () => {
  61. const spy = sinon.spy();
  62. const opts = { direction: 'backward' };
  63. composer.on( 'modifySelection', spy );
  64. composer.modifySelection( document.selection, opts );
  65. const data = spy.args[ 0 ][ 1 ];
  66. expect( data.selection ).to.equal( document.selection );
  67. expect( data.options ).to.equal( opts );
  68. } );
  69. } );
  70. } );