composer.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.schema.registerItem( 'p', '$block' );
  15. document.createRoot();
  16. composer = new Composer();
  17. } );
  18. describe( 'constructor', () => {
  19. it( 'attaches deleteContents default listener', () => {
  20. setData( document, '<p>f<selection>oo</p><p>ba</selection>r</p>' );
  21. const batch = document.batch();
  22. composer.fire( 'deleteContents', { batch, selection: document.selection } );
  23. expect( getData( document ) ).to.equal( '<p>f<selection /></p><p>r</p>' );
  24. expect( batch.deltas ).to.not.be.empty;
  25. } );
  26. it( 'attaches deleteContents default listener which passes options', () => {
  27. setData( document, '<p>f<selection>oo</p><p>ba</selection>r</p>' );
  28. const batch = document.batch();
  29. composer.fire( 'deleteContents', {
  30. batch,
  31. selection: document.selection,
  32. options: { merge: true }
  33. } );
  34. expect( getData( document ) ).to.equal( '<p>f<selection />r</p>' );
  35. } );
  36. it( 'attaches modifySelection default listener', () => {
  37. setData( document, '<p>foo<selection />bar</p>' );
  38. composer.fire( 'modifySelection', {
  39. selection: document.selection,
  40. options: {
  41. direction: 'BACKWARD'
  42. }
  43. } );
  44. expect( getData( document ) )
  45. .to.equal( '<p>fo<selection backward>o</selection>bar</p>' );
  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. } );