PHP and Javascript implementations of a simple markdown parser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpreadsheetMarkdownIntegrationTests.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. class SpreadsheetMarkdownIntegrationTests extends BaseTest {
  2. parser;
  3. setUp() {
  4. this.parser = new Markdown([ ...Markdown.allReaders, new MDSpreadsheetReader() ]);
  5. }
  6. md(markdown) {
  7. return normalizeWhitespace(this.parser.toHTML(markdown));
  8. }
  9. test_integration() {
  10. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |';
  11. const expected = '<table><thead>' +
  12. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  13. '</thead><tbody><tr>' +
  14. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' +
  15. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' +
  16. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' +
  17. '</tr></tbody></table>';
  18. const actual = this.md(markdown);
  19. this.assertEqual(expected, actual);
  20. }
  21. test_bailOut() {
  22. // If no formulas found table isn't modified
  23. const markdown = '| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |';
  24. const expected = '<table><thead>' +
  25. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  26. '</thead><tbody><tr>' +
  27. '<td>3</td>' +
  28. '<td>4</td>' +
  29. '<td>A*B</td>' +
  30. '</tr></tbody></table>';
  31. const actual = this.md(markdown);
  32. this.assertEqual(expected, actual);
  33. }
  34. test_observedError1() {
  35. // Saw "Uncaught Error: columnIndex must be number, got string" from this
  36. const markdown = '| Unit Price | Qty | Subtotal |\n| ---: | ---: | ---: |\n| $1.23 | 2 | =A*B FILL |\n| $4.99 | 6 | |\n| Total | | =SUM(C:C) |';
  37. const expected = '<table><thead><tr>' +
  38. '<th style="text-align: right;">Unit Price</th>' +
  39. '<th style="text-align: right;">Qty</th>' +
  40. '<th>Subtotal</th>' +
  41. '</tr></thead><tbody><tr>' +
  42. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="1.23" data-string-value="1.23">$1.23</td>' +
  43. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="2" data-string-value="2">2</td>' +
  44. '<td class="calculated spreadsheet-type-currency" data-numeric-value="2.46" data-string-value="2.46">$2.46</td>' +
  45. '</tr><tr>' +
  46. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="4.99" data-string-value="4.99">$4.99</td>' +
  47. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="6" data-string-value="6">6</td>' +
  48. '<td class="calculated spreadsheet-type-currency" data-numeric-value="29.94" data-string-value="29.94">$29.94</td>' +
  49. '</tr><tr>' +
  50. '<td class="spreadsheet-type-string" style="text-align: right;" data-string-value="Total">Total</td>' +
  51. '<td class="spreadsheet-type-blank" style="text-align: right;" data-numeric-value="0" data-string-value=""></td>' +
  52. '<td class="calculated spreadsheet-type-currency" data-numeric-value="32.4" data-string-value="32.4">$32.40</td>' +
  53. '</tr></tbody></table>';
  54. const actual = this.md(markdown);
  55. this.assertEqual(expected, actual);
  56. }
  57. test_observedError2() {
  58. // A FILLed difference formula in row 2 of `=B2-B1` threw an exception
  59. // due to a transposed address to the row above of `=B1-B0`.
  60. const markdown = `| TB | Price | $/TB | Incr |
  61. | --- | --- | --- | --- |
  62. | 2 | $99.99 | =B/A FILL | |
  63. | 4 | $186.41 | | =B2-B1 FILL |
  64. | 6 | $209.99 | | |`;
  65. const actual = this.md(markdown);
  66. }
  67. test_styledValue() {
  68. const markdown = "| A | B | C |\n| --- | --- | --- |\n| **3** | _4_ | =A*B |";
  69. const expected = '<table><thead>' +
  70. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  71. '</thead><tbody><tr>' +
  72. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3"><strong>3</strong></td>' +
  73. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4"><em>4</em></td>' +
  74. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' +
  75. '</tr></tbody></table>';
  76. const actual = this.md(markdown);
  77. this.assertEqual(expected, actual);
  78. }
  79. test_styledFormula() {
  80. const markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | **=A*B** |";
  81. const expected = '<table><thead>' +
  82. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  83. '</thead><tbody><tr>' +
  84. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' +
  85. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' +
  86. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><strong>12</strong></td>' +
  87. '</tr></tbody></table>';
  88. const actual = this.md(markdown);
  89. this.assertEqual(expected, actual);
  90. }
  91. test_styledFormula_multiple() {
  92. const markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | ~~**=A*B**~~ |";
  93. const expected = '<table><thead>' +
  94. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  95. '</thead><tbody><tr>' +
  96. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' +
  97. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' +
  98. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12"><s><strong>12</strong></s></td>' +
  99. '</tr></tbody></table>';
  100. const actual = this.md(markdown);
  101. this.assertEqual(expected, actual);
  102. }
  103. test_preservedOperators() {
  104. // Regular markdown will want to convert *B* into emphasized B
  105. const markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B*4 |";
  106. const expected = '<table><thead>' +
  107. '<tr><th>A</th><th>B</th><th>C</th></tr>' +
  108. '</thead><tbody><tr>' +
  109. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' +
  110. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' +
  111. '<td class="calculated spreadsheet-type-number" data-numeric-value="48" data-string-value="48">48</td>' +
  112. '</tr></tbody></table>';
  113. const actual = this.md(markdown);
  114. this.assertEqual(expected, actual);
  115. }
  116. }