utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  6. import { getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { stringify, getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. /**
  9. * Checks whether for a given editor instance pasting specific content (input) gives expected result (output).
  10. *
  11. * @param {module:core/editor/editor~Editor} editor
  12. * @param {String} input Data to paste.
  13. * @param {String} expectedModel Expected model.
  14. * @param {String} [expectedView=null] Expected view.
  15. */
  16. export function expectPaste( editor, input, expectedModel, expectedView = null ) {
  17. pasteHtml( editor, input );
  18. expect( getModelData( editor.model ) ).to.equal( expectedModel );
  19. if ( expectedView ) {
  20. expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
  21. }
  22. }
  23. /**
  24. * Fires paste event on a given editor instance with a specific HTML data.
  25. *
  26. * @param {module:core/editor/editor~Editor} editor Editor instance on which paste event will be fired.
  27. * @param {String} html The HTML data with which paste event will be fired.
  28. */
  29. export function pasteHtml( editor, html ) {
  30. editor.editing.view.document.fire( 'paste', {
  31. dataTransfer: createDataTransfer( { 'text/html': html } ),
  32. preventDefault() {}
  33. } );
  34. }
  35. /**
  36. * Mocks dataTransfer object which can be used for simulating paste.
  37. *
  38. * @param {Object} data Object containing "mime type - data" pairs.
  39. * @returns {Object} DataTransfer mock object.
  40. */
  41. export function createDataTransfer( data ) {
  42. return {
  43. getData( type ) {
  44. return data[ type ];
  45. }
  46. };
  47. }
  48. /**
  49. * Compares two HTML strings.
  50. *
  51. * This function is designed for comparing normalized data so expected input is preprocessed before comparing:
  52. *
  53. * * Tabs on the lines beginning are removed.
  54. * * Line breaks and empty lines are removed.
  55. *
  56. * The expected input should be prepared in the above in mind which means every element containing text nodes must start
  57. * and end in the same line. So expected input may be formatted like:
  58. *
  59. * <span lang=PL style='mso-ansi-language:PL'> 03<span style='mso-spacerun:yes'> </span><o:p></o:p></span>
  60. *
  61. * but not like:
  62. *
  63. * <span lang=PL style='mso-ansi-language:PL'>
  64. * 03<span style='mso-spacerun:yes'> </span>
  65. * <o:p></o:p>
  66. * </span>
  67. *
  68. * because tabulator preceding `03` text will be treated as formatting character and will be removed.
  69. *
  70. * @param {String} actual
  71. * @param {String} expected
  72. */
  73. export function expectNormalized( actual, expected ) {
  74. const expectedInlined = expected
  75. // Replace tabs on the lines beginning as normalized input files are formatted.
  76. .replace( /^\t*</gm, '<' )
  77. // Replace line breaks (after closing tags) too.
  78. .replace( /[\r\n]/gm, '' );
  79. // We are ok with both spaces and non-breaking spaces in the actual content.
  80. // Replace `&nbsp;` with regular spaces to align with expected content (where regular spaces are only used).
  81. const actualNormalized = stringify( actual ).replace( /\u00A0/g, ' ' );
  82. expect( actualNormalized ).to.equal( normalizeHtml( expectedInlined ) );
  83. }