utils.js 1.3 KB

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