PHP and Javascript implementations of a simple markdown parser
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SpreadsheetMarkdownIntegrationTests.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\TestCase;
  4. require_once __DIR__ . '/../../php/markdown.php';
  5. require_once __DIR__ . '/../../php/spreadsheet.php';
  6. final class SpreadsheetMarkdownIntegrationTests extends TestCase {
  7. private ?Markdown $parser;
  8. protected function setUp(): void {
  9. $this->parser = new Markdown(array_merge(Markdown::allReaders(), [ new MDSpreadsheetReader() ]));
  10. }
  11. private function md(string $markdown): string {
  12. return $this->normalizeWhitespace($this->parser->toHTML($markdown));
  13. }
  14. private function normalizeWhitespace(string $str): string {
  15. $str = mb_eregi_replace('\\s+', ' ', $str);
  16. $str = mb_eregi_replace('>\\s+<', '><', $str);
  17. return trim($str);
  18. }
  19. public function test_integration() {
  20. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B |";
  21. $expected = '<table><thead>' .
  22. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  23. '</thead><tbody><tr>' .
  24. '<td class="spreadsheet-type-number" data-numeric-value="3" data-string-value="3">3</td>' .
  25. '<td class="spreadsheet-type-number" data-numeric-value="4" data-string-value="4">4</td>' .
  26. '<td class="calculated spreadsheet-type-number" data-numeric-value="12" data-string-value="12">12</td>' .
  27. '</tr></tbody></table>';
  28. $actual = $this->md($markdown);
  29. $this->assertSame($expected, $actual);
  30. }
  31. public function test_bailOut() {
  32. // If no formulas found table isn't modified
  33. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | A*B |";
  34. $expected = '<table><thead>' .
  35. '<tr><th>A</th><th>B</th><th>C</th></tr>' .
  36. '</thead><tbody><tr>' .
  37. '<td>3</td>' .
  38. '<td>4</td>' .
  39. '<td>A*B</td>' .
  40. '</tr></tbody></table>';
  41. $actual = $this->md($markdown);
  42. $this->assertSame($expected, $actual);
  43. }
  44. public function test_observedError1() {
  45. // Saw "Uncaught Error: columnIndex must be number, got string" from this
  46. $markdown = "| Unit Price | Qty | Subtotal |\n| ---: | ---: | ---: |\n| $1.23 | 2 | =A*B FILL |\n| $4.99 | 6 | |\n| Total | | =SUM(C:C) |";
  47. $expected = '<table><thead><tr>' .
  48. '<th style="text-align: right;">Unit Price</th>' .
  49. '<th style="text-align: right;">Qty</th>' .
  50. '<th>Subtotal</th>' .
  51. '</tr></thead><tbody><tr>' .
  52. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="1.23" data-string-value="1.23">$1.23</td>' .
  53. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="2" data-string-value="2">2</td>' .
  54. '<td class="calculated spreadsheet-type-currency" data-numeric-value="2.46" data-string-value="2.46">$2.46</td>' .
  55. '</tr><tr>' .
  56. '<td class="spreadsheet-type-currency" style="text-align: right;" data-numeric-value="4.99" data-string-value="4.99">$4.99</td>' .
  57. '<td class="spreadsheet-type-number" style="text-align: right;" data-numeric-value="6" data-string-value="6">6</td>' .
  58. '<td class="calculated spreadsheet-type-currency" data-numeric-value="29.94" data-string-value="29.94">$29.94</td>' .
  59. '</tr><tr>' .
  60. '<td class="spreadsheet-type-string" style="text-align: right;" data-string-value="Total">Total</td>' .
  61. '<td class="spreadsheet-type-blank" style="text-align: right;" data-numeric-value="0" data-string-value=""></td>' .
  62. '<td class="calculated spreadsheet-type-currency" data-numeric-value="32.4" data-string-value="32.4">$32.40</td>' .
  63. '</tr></tbody></table>';
  64. $actual = $this->md($markdown);
  65. $this->assertSame($expected, $actual);
  66. }
  67. public function test_styledValue() {
  68. $markdown = "| A | B | C |\n| --- | --- | --- |\n| **3** | _4_ | =A*B |";
  69. $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. $actual = $this->md($markdown);
  77. $this->assertSame($expected, $actual);
  78. }
  79. public function test_styledFormula() {
  80. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | **=A*B** |";
  81. $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. $actual = $this->md($markdown);
  89. $this->assertSame($expected, $actual);
  90. }
  91. public function test_styledFormula_multiple() {
  92. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | ~~**=A*B**~~ |";
  93. $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. $actual = $this->md($markdown);
  101. $this->assertSame($expected, $actual);
  102. }
  103. public function test_preservedOperators() {
  104. // Regular markdown will want to convert *B* into emphasized B
  105. $markdown = "| A | B | C |\n| --- | --- | --- |\n| 3 | 4 | =A*B*4 |";
  106. $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. $actual = $this->md($markdown);
  114. $this->assertSame($expected, $actual);
  115. }
  116. }
  117. ?>