utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. /**
  23. * Mocks dataTransfer object which can be used for simulating paste.
  24. *
  25. * @param {Object} data Object containing "mime type - data" pairs.
  26. * @returns {Object} DataTransfer mock object.
  27. */
  28. export function createDataTransfer( data ) {
  29. return {
  30. getData( type ) {
  31. return data[ type ];
  32. }
  33. };
  34. }
  35. // Fires paste event on a given editor instance with a specific HTML data.
  36. //
  37. // @param {module:core/editor/editor~Editor} editor Editor instance on which paste event will be fired.
  38. // @param {String} html The HTML data with which paste event will be fired.
  39. function pasteHtml( editor, html ) {
  40. editor.editing.view.document.fire( 'paste', {
  41. dataTransfer: createDataTransfer( { 'text/html': html } ),
  42. preventDefault() {}
  43. } );
  44. }