8
0

utils.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import MarkdownDataProcessor from '../../src/gfmdataprocessor';
  6. import { stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/view.js';
  7. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  8. import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  9. /**
  10. * Tests MarkdownDataProcessor.
  11. *
  12. * @param {String} markdown Markdown to be processed to view.
  13. * @param {String} viewString Expected view structure.
  14. * @param {String} [normalizedMarkdown] When converting back to the markdown it might be different than provided input
  15. * @param {Object} [options] Additional options.
  16. * @param {Function} [options.setup] A function that receives the data processor instance before its execution.
  17. * markdown string (which will be used if this parameter is not provided).
  18. */
  19. export function testDataProcessor( markdown, viewString, normalizedMarkdown, options ) {
  20. const viewDocument = new ViewDocument( new StylesProcessor() );
  21. const dataProcessor = new MarkdownDataProcessor( viewDocument );
  22. options && options.setup && options.setup( dataProcessor );
  23. const viewFragment = dataProcessor.toView( markdown );
  24. const html = cleanHtml( stringify( viewFragment ) );
  25. // Check if view has correct data.
  26. expect( html ).to.equal( viewString );
  27. // Check if converting back gives the same result.
  28. const normalized = typeof normalizedMarkdown !== 'undefined' ? normalizedMarkdown : markdown;
  29. expect( cleanMarkdown( dataProcessor.toData( viewFragment ) ) ).to.equal( normalized );
  30. }
  31. function cleanHtml( html ) {
  32. // Space between table elements.
  33. html = html.replace( /(th|td|tr)>\s+<(\/?(?:th|td|tr))/g, '$1><$2' );
  34. return html;
  35. }
  36. function cleanMarkdown( markdown ) {
  37. // Trim spaces at the end of the lines.
  38. markdown = markdown.replace( / +$/gm, '' );
  39. // Trim linebreak at the very beginning.
  40. markdown = markdown.replace( /^\s+/g, '' );
  41. return markdown;
  42. }