escaping.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { testDataProcessor } from '../../tests/_utils/utils';
  8. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  9. import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
  10. const testCases = {
  11. 'backslash': { test: '\\\\', result: '\\' },
  12. 'underscore': { test: '\\_', result: '_' },
  13. 'left brace': { test: '\\{', result: '{' },
  14. 'right brace': { test: '\\}', result: '}' },
  15. 'left bracket': { test: '\\[', result: '[' },
  16. 'right bracket': { test: '\\]', result: ']' },
  17. 'left paren': { test: '\\(', result: '(' },
  18. 'right paren': { test: '\\)', result: ')' },
  19. 'greater than': { test: '\\>', result: '>' },
  20. 'hash': { test: '\\#', result: '#' },
  21. 'peroid': { test: '\\.', result: '.' },
  22. 'exclamation mark': { test: '\\!', result: '!' },
  23. 'plus': { test: '\\+', result: '+' },
  24. 'minus': { test: '\\-', result: '-' }
  25. };
  26. describe( 'GFMDataProcessor', () => {
  27. describe( 'escaping', () => {
  28. describe( 'toView', () => {
  29. let dataProcessor;
  30. beforeEach( () => {
  31. const viewDocument = new ViewDocument( new StylesProcessor() );
  32. dataProcessor = new MarkdownDataProcessor( viewDocument );
  33. } );
  34. for ( const key in testCases ) {
  35. const test = testCases[ key ].test;
  36. const result = testCases[ key ].result;
  37. it( `should escape ${ key }`, () => {
  38. const documentFragment = dataProcessor.toView( test );
  39. expect( stringify( documentFragment ) ).to.equal( `<p>${ result }</p>` );
  40. } );
  41. it( `should not escape ${ key } in code blocks`, () => {
  42. const documentFragment = dataProcessor.toView( ` ${ test }` );
  43. expect( stringify( documentFragment ) ).to.equal( `<pre><code>${ test }</code></pre>` );
  44. } );
  45. it( `should not escape ${ key } in code spans`, () => {
  46. const documentFragment = dataProcessor.toView( '`' + test + '`' );
  47. expect( stringify( documentFragment ) ).to.equal( `<p><code>${ test }</code></p>` );
  48. } );
  49. }
  50. it( 'should escape backtick', () => {
  51. const documentFragment = dataProcessor.toView( '\\`' );
  52. expect( stringify( documentFragment ) ).to.equal( '<p>`</p>' );
  53. } );
  54. it( 'should not escape backtick in code blocks', () => {
  55. const documentFragment = dataProcessor.toView( ' \\`' );
  56. expect( stringify( documentFragment ) ).to.equal( '<pre><code>\\`</code></pre>' );
  57. } );
  58. } );
  59. describe( 'HTML', () => {
  60. // To note that the test util inlines entities in text nodes, hence the expected HTML in these tests
  61. // contain the raw characters but we "know" that those are text nodes and therefore should be converted
  62. // back to entities when outputting markdown.
  63. it( 'should escape <', () => {
  64. testDataProcessor( '\\<', '<p><</p>' );
  65. } );
  66. it( 'should escape HTML as text', () => {
  67. testDataProcessor( '\\<h1>Test\\</h1>', '<p><h1>Test</h1></p>' );
  68. } );
  69. it( 'should not escape \\< inside inline code', () => {
  70. testDataProcessor( '`\\<`', '<p><code>\\<</code></p>' );
  71. } );
  72. it( 'should not touch escape-like HTML inside code blocks', () => {
  73. testDataProcessor(
  74. '```\n' +
  75. '\\<h1>Test\\</h1>\n' +
  76. '```',
  77. '<pre><code>' +
  78. '\\<h1>Test\\</h1>' +
  79. '</code></pre>' );
  80. } );
  81. // Necessary test as we're overriding Turndown's escape(). Just to be sure.
  82. it( 'should still escape markdown characters', () => {
  83. testDataProcessor( '\\* \\_', '<p>* _</p>' );
  84. } );
  85. } );
  86. } );
  87. } );