| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- declare(strict_types=1);
-
- use PHPUnit\Framework\TestCase;
-
- require_once __DIR__ . '/../php/markdown.php';
-
- final class BlockTests extends TestCase {
- private ?Markdown $parser = null;
-
- protected function setUp(): void {
- parent::setUp();
- $this->parser = Markdown::completeParser();
- }
-
- private function md(string $markdown): string {
- return $this->normalizeWhitespace($this->parser->toHTML($markdown));
- }
-
- private function normalizeWhitespace(string $str): string {
- $str = mb_eregi_replace('\\s+', ' ', $str);
- $str = mb_eregi_replace('>\\s+<', '><', $str);
- return trim($str);
- }
-
- public function test_paragraphs() {
- $markdown = "Lorem ipsum\n\nDolor sit amet";
- $expected = "<p>Lorem ipsum</p><p>Dolor sit amet</p>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_paragraph_lineGrouping() {
- $markdown = "Lorem ipsum\ndolor sit amet";
- $expected = "Lorem ipsum dolor sit amet";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_header_underlineH1() {
- $markdown = "Header 1\n===\n\nLorem ipsum";
- $expected = "<h1>Header 1</h1><p>Lorem ipsum</p>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_header_underlineH2() {
- $markdown = "Header 2\n---\n\nLorem ipsum";
- $expected = "<h2>Header 2</h2><p>Lorem ipsum</p>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_header_hash() {
- $markdown = "# Header 1\n## Header 2\n### Header 3\n#### Header 4\n##### Header 5\n###### Header 6\n";
- $expected = '<h1>Header 1</h1><h2>Header 2</h2><h3>Header 3</h3><h4>Header 4</h4><h5>Header 5</h5><h6>Header 6</h6>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_header_hash_trailing() {
- $markdown = "# Header 1 #\n## Header 2 ##\n### Header 3 ######";
- $expected = '<h1>Header 1</h1><h2>Header 2</h2><h3>Header 3</h3>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_unorderedList() {
- $markdown = "* Lorem\n* Ipsum\n* Dolor";
- $expected = '<ul><li>Lorem</li><li>Ipsum</li><li>Dolor</li></ul>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_unorderedList_nested() {
- $markdown = "* Lorem\n + Ipsum\n* Dolor";
- $expected = '<ul><li> Lorem<ul><li>Ipsum</li></ul></li><li>Dolor</li></ul>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_unorderedList_hitch() {
- // This incomplete bulleted list locked up the browser at one
- // point, not forever but a REALLY long time
- $markdown = "Testing\n\n* ";
- $expected = '<p>Testing</p><ul><li></li></ul>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_orderedList() {
- $markdown = "1. Lorem\n1. Ipsum\n5. Dolor";
- $expected = '<ol><li>Lorem</li><li>Ipsum</li><li>Dolor</li></ol>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_orderedList_numbering() {
- $markdown = "4. Lorem\n1. Ipsum\n9. Dolor";
- $expected = '<ol start="4"><li>Lorem</li><li>Ipsum</li><li>Dolor</li></ol>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_orderedList_nested1() {
- $markdown = "1. Lorem\n 1. Ipsum\n1. Dolor";
- $expected = '<ol><li> Lorem<ol><li>Ipsum</li></ol></li><li>Dolor</li></ol>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_orderedList_nested2() {
- $markdown = "1. Lorem\n 1. Ipsum\n 1. Dolor\n 1. Sit\n1. Amet";
- $expected = '<ol><li> Lorem<ol><li> Ipsum<ol><li>Dolor</li></ol></li><li>Sit</li></ol></li><li>Amet</li></ol>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_blockquote() {
- $markdown = '> Lorem ipsum dolor';
- $expected = '<blockquote> Lorem ipsum dolor </blockquote>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_blockquote_paragraphs() {
- $markdown = "> Lorem ipsum dolor\n>\n>Sit amet";
- $expected = '<blockquote><p>Lorem ipsum dolor</p><p>Sit amet</p></blockquote>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_blockquote_list() {
- $markdown = "> 1. Lorem\n> 2. Ipsum";
- $expected = '<blockquote><ol><li>Lorem</li><li>Ipsum</li></ol></blockquote>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_codeBlock_indented() {
- $markdown = "Code\n\n function foo() {\n return 'bar';\n }\n\nend";
- $expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
- $actual = trim($this->parser->toHTML($markdown)); // don't normalize whitespace
- $this->assertSame(mb_ereg_replace(' ', '⎵', $expected), mb_ereg_replace(' ', '⎵', $actual));
- }
-
- public function test_codeBlock_fenced() {
- $markdown = "Code\n\n```\nfunction foo() {\n return 'bar';\n}\n```\n\nend";
- $expected = "<p>Code</p>\n\n<pre><code>function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
- $actual = trim($this->parser->toHTML($markdown)); // don't normalize whitespace
- $this->assertSame(mb_ereg_replace(' ', '⎵', $expected), mb_ereg_replace(' ', '⎵', $actual));
- }
-
- public function test_codeBlock_fenced_language() {
- $markdown = "Code\n\n```javascript\nfunction foo() {\n return 'bar';\n}\n```\n\nend";
- $expected = "<p>Code</p>\n\n<pre><code class=\"language-javascript\">function foo() {\n return 'bar';\n}</code></pre>\n\n<p>end</p>";
- $actual = trim($this->parser->toHTML($markdown)); // don't normalize whitespace
- $this->assertSame(mb_ereg_replace(' ', '⎵', $expected), mb_ereg_replace(' ', '⎵', $actual));
- }
-
- public function test_horizontalRule() {
- $markdown = "Before\n\n---\n\n- - -\n\n***\n\n* * * * * * *\n\nafter";
- $expected = "<p>Before</p><hr><hr><hr><hr><p>after</p>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_table_unfenced() {
- $markdown = "Column A | Column B | Column C\n--- | --- | ---\n1 | 2 | 3\n4 | 5 | 6";
- $expected = "<table><thead><tr><th>Column A</th><th>Column B</th><th>Column C</th></tr></thead><tbody><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></tbody></table>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_table_fenced() {
- $markdown = "| Column A | Column B | Column C |\n| --- | --- | --- |\n| 1 | 2 | 3\n4 | 5 | 6 |";
- $expected = "<table><thead><tr><th>Column A</th><th>Column B</th><th>Column C</th></tr></thead><tbody><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></tbody></table>";
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_table_alignment() {
- $markdown = "Column A | Column B | Column C\n" .
- ":--- | :---: | ---:\n" .
- "1 | 2 | 3\n" .
- "4 | 5 | 6";
- $expected = '<table>' .
- '<thead>' .
- '<tr>' .
- '<th style="text-align: left;">Column A</th>' .
- '<th style="text-align: center;">Column B</th>' .
- '<th style="text-align: right;">Column C</th>' .
- '</tr>' .
- '</thead>' .
- '<tbody>' .
- '<tr>' .
- '<td style="text-align: left;">1</td>' .
- '<td style="text-align: center;">2</td>' .
- '<td style="text-align: right;">3</td>' .
- '</tr>' .
- '<tr>' .
- '<td style="text-align: left;">4</td>' .
- '<td style="text-align: center;">5</td>' .
- '<td style="text-align: right;">6</td>' .
- '</tr>' .
- '</tbody>' .
- '</table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_table_holes() {
- $markdown = "Column A||Column C\n" .
- "---|---|---\n" .
- "|1|2||\n" .
- "|4||6|\n" .
- "||8|9|";
- $expected = '<table>' .
- '<thead>' .
- '<tr>' .
- '<th>Column A</th>' .
- '<th></th>' .
- '<th>Column C</th>' .
- '</tr>' .
- '</thead>' .
- '<tbody>' .
- '<tr>' .
- '<td>1</td>' .
- '<td>2</td>' .
- '<td></td>' .
- '</tr>' .
- '<tr>' .
- '<td>4</td>' .
- '<td></td>' .
- '<td>6</td>' .
- '</tr>' .
- '<tr>' .
- '<td></td>' .
- '<td>8</td>' .
- '<td>9</td>' .
- '</tr>' .
- '</tbody>' .
- '</table>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_definitionList() {
- $markdown = "term\n" .
- ": definition\n" .
- "another" .
- " term\n" .
- ": def 1\n" .
- " broken on next line\n" .
- ": def 2";
- $expected = '<dl>' .
- '<dt>term</dt>' .
- '<dd>definition</dd>' .
- '<dt>another term</dt>' .
- '<dd>def 1 broken on next line</dd>' .
- '<dd>def 2</dd>' .
- '</dl>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_footnotes() {
- $markdown = "Lorem ipsum[^1] dolor[^abc] sit[^1] amet\n\n[^1]: A footnote\n[^abc]: Another footnote";
- $expected = '<p>Lorem ipsum<sup class="footnote" id="footnoteref_1"><a href="#footnote_1">1</a></sup> ' .
- 'dolor<sup class="footnote" id="footnoteref_2"><a href="#footnote_2">2</a></sup> ' .
- 'sit<sup class="footnote" id="footnoteref_3"><a href="#footnote_1">1</a></sup> amet</p>' .
- '<div class="footnotes">' .
- '<ol>' .
- '<li value="1" id="footnote_1">A footnote <a href="#footnoteref_1" class="footnote-backref">↩︎</a><a href="#footnoteref_3" class="footnote-backref">↩︎</a></li>' .
- '<li value="2" id="footnote_2">Another footnote <a href="#footnoteref_2" class="footnote-backref">↩︎</a></li>' .
- '</ol>' .
- '</div>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
-
- public function test_abbreviations() {
- $markdown = "Lorem ipsum HTML dolor HTML sit\n" .
- "\n" .
- "*[HTML]: Hypertext Markup Language";
- $expected = '<p>Lorem ipsum <abbr title="Hypertext Markup Language">HTML</abbr> dolor <abbr title="Hypertext Markup Language">HTML</abbr> sit</p>';
- $actual = $this->md($markdown);
- $this->assertSame($expected, $actual);
- }
- }
|