8
0

escaping.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import MarkdownDataProcessor from '/ckeditor5/markdown-gfm/gfmdataprocessor.js';
  6. import { stringify } from '/ckeditor5/engine/dev-utils/view.js';
  7. const testCases = {
  8. 'backslash': { test: '\\\\', result: '\\' },
  9. 'underscore': { test: '\\_', result: '_' },
  10. 'left brace': { test: '\\{', result: '{' },
  11. 'right brace': { test: '\\}', result: '}' },
  12. 'left bracket': { test: '\\[', result: '[' },
  13. 'right bracket': { test: '\\]', result: ']' },
  14. 'left paren': { test: '\\(', result: '(' },
  15. 'right paren': { test: '\\)', result: ')' },
  16. 'greater than': { test: '\\>', result: '>' },
  17. 'hash': { test: '\\#', result: '#' },
  18. 'peroid': { test: '\\.', result: '.' },
  19. 'exclamation mark': { test: '\\!', result: '!' },
  20. 'plus': { test: '\\+', result: '+' },
  21. 'minus': { test: '\\-', result: '-' },
  22. };
  23. describe( 'GFMDataProcessor', () => {
  24. let dataProcessor;
  25. beforeEach( () => {
  26. dataProcessor = new MarkdownDataProcessor();
  27. } );
  28. describe( 'escaping', () => {
  29. describe( 'toView', () => {
  30. for ( let key in testCases ) {
  31. const test = testCases[ key ].test;
  32. const result = testCases[ key ].result;
  33. it( `should escape ${key}`, () => {
  34. const documentFragment = dataProcessor.toView( test );
  35. expect( stringify( documentFragment ) ).to.equal( `<p>${ result }</p>` );
  36. } );
  37. it( `should not escape ${key} in code blocks`, () => {
  38. const documentFragment = dataProcessor.toView( ` ${ test }` );
  39. expect( stringify( documentFragment ) ).to.equal( `<pre><code>${ test }</code></pre>` );
  40. } );
  41. it( `should not escape ${key} in code spans`, () => {
  42. const documentFragment = dataProcessor.toView( '`' + test + '`' );
  43. expect( stringify( documentFragment ) ).to.equal( `<p><code>${ test }</code></p>` );
  44. } );
  45. }
  46. it( 'should escape backtick', () => {
  47. const documentFragment = dataProcessor.toView( '\\`' );
  48. expect( stringify( documentFragment ) ).to.equal( '<p>`</p>' );
  49. } );
  50. it( 'should not escape backtick in code blocks', () => {
  51. const documentFragment = dataProcessor.toView( ' \\`' );
  52. expect( stringify( documentFragment ) ).to.equal( '<pre><code>\\`</code></pre>' );
  53. } );
  54. } );
  55. } );
  56. } );