8
0

utils.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if ( options && options.setup ) {
  23. options.setup( dataProcessor );
  24. }
  25. const viewFragment = dataProcessor.toView( markdown );
  26. const html = cleanHtml( stringify( viewFragment ) );
  27. // Check if view has correct data.
  28. expect( html ).to.equal( viewString );
  29. // Check if converting back gives the same result.
  30. const normalized = typeof normalizedMarkdown !== 'undefined' ? normalizedMarkdown : markdown;
  31. expect( cleanMarkdown( dataProcessor.toData( viewFragment ) ) ).to.equal( normalized );
  32. }
  33. function cleanHtml( html ) {
  34. // Space between table elements.
  35. html = html.replace( /(th|td|tr)>\s+<(\/?(?:th|td|tr))/g, '$1><$2' );
  36. return html;
  37. }
  38. function cleanMarkdown( markdown ) {
  39. // Trim spaces at the end of the lines.
  40. markdown = markdown.replace( / +$/gm, '' );
  41. // Trim linebreak at the very beginning.
  42. markdown = markdown.replace( /^\s+/g, '' );
  43. return markdown;
  44. }