utils.js 1.6 KB

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