utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  6. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  7. /**
  8. * Checks whether for a given editor instance pasting specific content (input) gives expected result (output).
  9. *
  10. * @param {module:core/editor/editor~Editor} editor
  11. * @param {String} input Data to paste.
  12. * @param {String} expectedModel Expected model.
  13. * @param {String} [expectedView=null] Expected view.
  14. */
  15. export function expectPaste( editor, input, expectedModel, expectedView = null ) {
  16. pasteHtml( editor, input );
  17. expect( getModelData( editor.model ) ).to.equal( expectedModel );
  18. if ( expectedView ) {
  19. expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
  20. }
  21. }
  22. // Fires paste event on a given editor instance with a specific HTML data.
  23. //
  24. // @param {module:core/editor/editor~Editor} editor Editor instance on which paste event will be fired.
  25. // @param {String} html The HTML data with which paste event will be fired.
  26. function pasteHtml( editor, html ) {
  27. editor.editing.view.document.fire( 'paste', {
  28. dataTransfer: createDataTransfer( { 'text/html': html } ),
  29. preventDefault() {}
  30. } );
  31. }
  32. // Mocks dataTransfer object which can be used for simulating paste.
  33. //
  34. // @param {Object} data Object containing "mime type - data" pairs.
  35. // @returns {Object} DataTransfer mock object.
  36. function createDataTransfer( data ) {
  37. return {
  38. getData( type ) {
  39. return data[ type ];
  40. }
  41. };
  42. }