utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * markdown string (which will be used if this parameter is not provided).
  16. */
  17. export function testDataProcessor( markdown, viewString, normalizedMarkdown ) {
  18. const viewDocument = new ViewDocument( new StylesProcessor() );
  19. const dataProcessor = new MarkdownDataProcessor( viewDocument );
  20. const viewFragment = dataProcessor.toView( markdown );
  21. const html = cleanHtml( stringify( viewFragment ) );
  22. // Check if view has correct data.
  23. expect( html ).to.equal( viewString );
  24. // Check if converting back gives the same result.
  25. const normalized = typeof normalizedMarkdown !== 'undefined' ? normalizedMarkdown : markdown;
  26. expect( cleanMarkdown( dataProcessor.toData( viewFragment ) ) ).to.equal( normalized );
  27. }
  28. function cleanHtml( html ) {
  29. // Space between table elements.
  30. html = html.replace( /(th|td|tr)>\s+<(\/?(?:th|td|tr))/g, '$1><$2' );
  31. return html;
  32. }
  33. function cleanMarkdown( markdown ) {
  34. // Trim spaces at the end of the lines.
  35. markdown = markdown.replace( /\s+$/gm, '' );
  36. // Trim linebreak at the very beginning.
  37. markdown = markdown.replace( /^\n/g, '' );
  38. return markdown;
  39. }