code.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, parse } from '/tests/engine/_utils/view.js';
  7. describe( 'GFMDataProcessor', () => {
  8. let dataProcessor;
  9. beforeEach( () => {
  10. dataProcessor = new MarkdownDataProcessor();
  11. } );
  12. describe( 'code', () => {
  13. describe( 'toView', () => {
  14. it( 'should process inline code', () => {
  15. const viewFragment = dataProcessor.toView( 'regular text and `inline code`' );
  16. expect( stringify( viewFragment ) ).to.equal( '<p>regular text and <code>inline code</code></p>' );
  17. } );
  18. it( 'should properly process backticks when combined', () => {
  19. const viewFragment = dataProcessor.toView( '`<fake a="` content of attribute `">`' );
  20. expect( stringify( viewFragment ) ).to.equal( '<p><code><fake a="</code> content of attribute <code>"></code></p>' );
  21. } );
  22. it( 'should properly process backticks inside code spans', () => {
  23. const viewFragment = dataProcessor.toView( '`` `backticks` ``' );
  24. // This should be checked - why there is a space after `bacticks`.
  25. expect( stringify( viewFragment ) ).to.equal( '<p><code>`backticks` </code></p>' );
  26. } );
  27. it( 'should process code blocks indented with tabs', () => {
  28. const viewFragment = dataProcessor.toView( ' code block' );
  29. expect( stringify( viewFragment ) ).to.equal( '<pre><code>code block</code></pre>' );
  30. } );
  31. it( 'should process code blocks indented with spaces', () => {
  32. const viewFragment = dataProcessor.toView( ' code block' );
  33. expect( stringify( viewFragment ) ).to.equal( '<pre><code>code block</code></pre>' );
  34. } );
  35. it( 'should process multi line code blocks indented with tabs', () => {
  36. const viewFragment = dataProcessor.toView( ' first line\n second line' );
  37. expect( stringify( viewFragment ) ).to.equal( '<pre><code>first line\nsecond line</code></pre>' );
  38. } );
  39. it( 'should process multi line code blocks indented with spaces', () => {
  40. const viewFragment = dataProcessor.toView( ' first line\n second line' );
  41. expect( stringify( viewFragment ) ).to.equal( '<pre><code>first line\nsecond line</code></pre>' );
  42. } );
  43. it( 'should process multi line code blocks with trailing spaces', () => {
  44. const viewFragment = dataProcessor.toView( ' the lines in this block \n all contain trailing spaces ' );
  45. expect( stringify( viewFragment ) ).to.equal( '<pre><code>the lines in this block \nall contain trailing spaces </code></pre>' );
  46. } );
  47. it( 'should process code block with language name', () => {
  48. const viewFragment = dataProcessor.toView(
  49. '``` js\n' +
  50. 'var a = \'hello\';\n' +
  51. 'console.log(a + \' world\');\n' +
  52. '```'
  53. );
  54. expect( stringify( viewFragment ) ).to.equal(
  55. '<pre>' +
  56. '<code class="lang-js">' +
  57. 'var a = \'hello\';\n' +
  58. 'console.log(a + \' world\');' +
  59. '</code>' +
  60. '</pre>' );
  61. } );
  62. it( 'should process code block with language name and using ~~~ as delimiter', () => {
  63. const viewFragment = dataProcessor.toView(
  64. '~~~ bash\n' +
  65. 'var a = \'hello\';\n' +
  66. 'console.log(a + \' world\');\n' +
  67. '~~~'
  68. );
  69. expect( stringify( viewFragment ) ).to.equal(
  70. '<pre>' +
  71. '<code class="lang-bash">' +
  72. 'var a = \'hello\';\n' +
  73. 'console.log(a + \' world\');' +
  74. '</code>' +
  75. '</pre>' );
  76. } );
  77. it( 'should process code block with language name and using ``````` as delimiter', () => {
  78. const viewFragment = dataProcessor.toView(
  79. '``````` js\n' +
  80. 'var a = \'hello\';\n' +
  81. 'console.log(a + \' world\');\n' +
  82. '```````'
  83. );
  84. expect( stringify( viewFragment ) ).to.equal(
  85. '<pre>' +
  86. '<code class="lang-js">' +
  87. 'var a = \'hello\';\n' +
  88. 'console.log(a + \' world\');' +
  89. '</code>' +
  90. '</pre>' );
  91. } );
  92. it( 'should process code block with language name and using ~~~~~~~~~~ as delimiter', () => {
  93. const viewFragment = dataProcessor.toView(
  94. '~~~~~~~~~~ js\n' +
  95. 'var a = \'hello\';\n' +
  96. 'console.log(a + \' world\');\n' +
  97. '~~~~~~~~~~'
  98. );
  99. expect( stringify( viewFragment ) ).to.equal(
  100. '<pre>' +
  101. '<code class="lang-js">' +
  102. 'var a = \'hello\';\n' +
  103. 'console.log(a + \' world\');' +
  104. '</code>' +
  105. '</pre>' );
  106. } );
  107. it( 'should process empty code block', () => {
  108. const viewFragment = dataProcessor.toView(
  109. '``` js\n' +
  110. '```'
  111. );
  112. expect( stringify( viewFragment ) ).to.equal(
  113. '<pre>' +
  114. '<code class="lang-js">' +
  115. '</code>' +
  116. '</pre>' );
  117. } );
  118. it( 'should process code block with empty line', () => {
  119. const viewFragment = dataProcessor.toView(
  120. '``` js\n\n' +
  121. '```'
  122. );
  123. expect( stringify( viewFragment ) ).to.equal(
  124. '<pre>' +
  125. '<code class="lang-js">' +
  126. '</code>' +
  127. '</pre>'
  128. );
  129. } );
  130. it( 'should process nested code', () => {
  131. const viewFragment = dataProcessor.toView(
  132. '````` code `` code ``` `````'
  133. );
  134. expect( stringify( viewFragment ) ).to.equal(
  135. '<p><code>code `` code ``` </code></p>'
  136. );
  137. } );
  138. } );
  139. describe( 'toData', () => {
  140. it( 'should process inline code', () => {
  141. const viewFragment = parse( '<p>regular text and <code>inline code</code></p>' );
  142. expect( dataProcessor.toData( viewFragment ) ).to.equal( 'regular text and `inline code`' );
  143. } );
  144. it( 'should properly process code blocks', () => {
  145. const viewFragment = parse( '<pre><code>code block</code></pre>' );
  146. expect( dataProcessor.toData( viewFragment ) ).to.equal( '```\ncode block\n```' );
  147. } );
  148. it( 'should process code block with language name', () => {
  149. const viewFragment = parse( '<pre><code class="lang-js">code block</code></pre>' );
  150. expect( dataProcessor.toData( viewFragment ) ).to.equal(
  151. '``` js\n' +
  152. 'code block\n' +
  153. '```'
  154. );
  155. } );
  156. } );
  157. } );
  158. } );