8
0

escaping.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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';
  7. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  8. import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  9. const testCases = {
  10. 'backslash': { test: '\\\\', result: '\\' },
  11. 'underscore': { test: '\\_', result: '_' },
  12. 'left brace': { test: '\\{', result: '{' },
  13. 'right brace': { test: '\\}', result: '}' },
  14. 'left bracket': { test: '\\[', result: '[' },
  15. 'right bracket': { test: '\\]', result: ']' },
  16. 'left paren': { test: '\\(', result: '(' },
  17. 'right paren': { test: '\\)', result: ')' },
  18. 'greater than': { test: '\\>', result: '>' },
  19. 'hash': { test: '\\#', result: '#' },
  20. 'peroid': { test: '\\.', result: '.' },
  21. 'exclamation mark': { test: '\\!', result: '!' },
  22. 'plus': { test: '\\+', result: '+' },
  23. 'minus': { test: '\\-', result: '-' }
  24. };
  25. describe( 'GFMDataProcessor', () => {
  26. let dataProcessor;
  27. beforeEach( () => {
  28. const viewDocument = new ViewDocument( new StylesProcessor() );
  29. dataProcessor = new MarkdownDataProcessor( viewDocument );
  30. } );
  31. describe( 'escaping', () => {
  32. describe( 'toView', () => {
  33. for ( const key in testCases ) {
  34. const test = testCases[ key ].test;
  35. const result = testCases[ key ].result;
  36. it( `should escape ${ key }`, () => {
  37. const documentFragment = dataProcessor.toView( test );
  38. expect( stringify( documentFragment ) ).to.equal( `<p>${ result }</p>` );
  39. } );
  40. it( `should not escape ${ key } in code blocks`, () => {
  41. const documentFragment = dataProcessor.toView( ` ${ test }` );
  42. expect( stringify( documentFragment ) ).to.equal( `<pre><code>${ test }</code></pre>` );
  43. } );
  44. it( `should not escape ${ key } in code spans`, () => {
  45. const documentFragment = dataProcessor.toView( '`' + test + '`' );
  46. expect( stringify( documentFragment ) ).to.equal( `<p><code>${ test }</code></p>` );
  47. } );
  48. }
  49. it( 'should escape backtick', () => {
  50. const documentFragment = dataProcessor.toView( '\\`' );
  51. expect( stringify( documentFragment ) ).to.equal( '<p>`</p>' );
  52. } );
  53. it( 'should not escape backtick in code blocks', () => {
  54. const documentFragment = dataProcessor.toView( ' \\`' );
  55. expect( stringify( documentFragment ) ).to.equal( '<pre><code>\\`</code></pre>' );
  56. } );
  57. } );
  58. } );
  59. } );