utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const spacesElementsOnlyRegex = />(\s+)<\//g;
  49. /**
  50. * Compares two HTML strings.
  51. *
  52. * This function is designed for comparing normalized data so expected input is preprocessed before comparing:
  53. *
  54. * * Tabs on the lines beginning are removed.
  55. * * Line breaks and empty lines are removed.
  56. * * Space preceding `<o:p></o:p>` tag is replaced with `&nbsp;`.
  57. * * Elements with spaces only have them replaced with `&nbsp;`'s.
  58. *
  59. * The expected input should be prepared in the above in mind which means every element containing text nodes must start
  60. * and end in the same line. So expected input may be formatted like:
  61. *
  62. * <span lang=PL style='mso-ansi-language:PL'> 03<span style='mso-spacerun:yes'> </span><o:p></o:p></span>
  63. *
  64. * but not like:
  65. *
  66. * <span lang=PL style='mso-ansi-language:PL'>
  67. * 03<span style='mso-spacerun:yes'> </span>
  68. * <o:p></o:p>
  69. * </span>
  70. *
  71. * because tabulator preceding `03` text will be treated as formatting character and will be removed.
  72. *
  73. * @param {String} actual
  74. * @param {String} expected
  75. */
  76. export function expectNormalized( actual, expected ) {
  77. let expectedNormalized = expected
  78. // Replace tabs on the lines beginning as normalized input files are formatted.
  79. .replace( /^\t*</gm, '<' )
  80. // Replace line breaks (after closing tags), as they may produce additional spaces during HTML normalization.
  81. .replace( /[\r\n]/gm, '' )
  82. // Replaces space before Word `<o:p></o:p>` tags so it is not removed during `normalizeHtml()` function call.
  83. .replace( / <o:p>/g, '\u00A0<o:p>' );
  84. // Replace spaces with `&nbsp;` inside elements with spaces only to prevent them from being removed during normalization.
  85. expectedNormalized = expectedNormalized.replace( spacesElementsOnlyRegex, ( match, spaces ) => {
  86. return `>${ Array( spaces.length + 1 ).join( '\u00A0' ) }</`;
  87. } );
  88. expect( stringify( actual ) ).to.equal( normalizeHtml( expectedNormalized ) );
  89. }