172b2703bSAndreas Gohr<?php 272b2703bSAndreas Gohr 372b2703bSAndreas Gohrnamespace dokuwiki\test\Parsing\Markdown; 472b2703bSAndreas Gohr 572b2703bSAndreas Gohruse dokuwiki\Parsing\ModeRegistry; 672b2703bSAndreas Gohr 772b2703bSAndreas Gohr/** 872b2703bSAndreas Gohr * Roundtrip tests driven by GFM's spec.txt. 972b2703bSAndreas Gohr * 1072b2703bSAndreas Gohr * Each example in gfm-spec/spec.txt becomes one data-provider case. The 1172b2703bSAndreas Gohr * markdown input is run through DokuWiki's full pipeline (parser + XHTML 1272b2703bSAndreas Gohr * renderer) and the result is compared to the expected HTML from the spec, 1372b2703bSAndreas Gohr * tolerating whitespace differences around block-level tags. 1472b2703bSAndreas Gohr * 1572b2703bSAndreas Gohr * Most examples are expected to FAIL until the relevant GFM parser modes 1672b2703bSAndreas Gohr * are implemented — they are the branch's living TODO list for GFM parity. 1772b2703bSAndreas Gohr * Do not mark such failures incomplete or skipped. 1872b2703bSAndreas Gohr * 1972b2703bSAndreas Gohr * `gfm-spec/skip.php` lists examples that are deliberately out of scope 2072b2703bSAndreas Gohr * for DokuWiki (e.g. CommonMark flanking-delimiter edge cases). Those are 2172b2703bSAndreas Gohr * reported as skipped with a reason. 2272b2703bSAndreas Gohr */ 2372b2703bSAndreas Gohrclass GfmSpecTest extends \DokuWikiTest 2472b2703bSAndreas Gohr{ 2572b2703bSAndreas Gohr private const FIXTURE_DIR = __DIR__ . '/gfm-spec/'; 2672b2703bSAndreas Gohr 2772b2703bSAndreas Gohr public static function specProvider(): iterable 2872b2703bSAndreas Gohr { 2972b2703bSAndreas Gohr $reader = new SpecReader(self::FIXTURE_DIR . 'spec.txt'); 3072b2703bSAndreas Gohr $skip = require self::FIXTURE_DIR . 'skip.php'; 3172b2703bSAndreas Gohr 3272b2703bSAndreas Gohr foreach ($reader->examples() as $ex) { 3372b2703bSAndreas Gohr $reason = $skip[$ex['number']] ?? null; 3472b2703bSAndreas Gohr $label = sprintf('#%d %s', $ex['number'], $ex['section']); 3572b2703bSAndreas Gohr yield $label => [$ex['markdown'], $ex['html'], $reason]; 3672b2703bSAndreas Gohr } 3772b2703bSAndreas Gohr } 3872b2703bSAndreas Gohr 3972b2703bSAndreas Gohr /** 4072b2703bSAndreas Gohr * @dataProvider specProvider 4172b2703bSAndreas Gohr */ 4272b2703bSAndreas Gohr public function testExample(string $md, string $expected, ?string $skipReason): void 4372b2703bSAndreas Gohr { 4472b2703bSAndreas Gohr if ($skipReason !== null) { 4572b2703bSAndreas Gohr $this->markTestSkipped($skipReason); 4672b2703bSAndreas Gohr } 4772b2703bSAndreas Gohr $actual = $this->renderMarkdown($md); 4872b2703bSAndreas Gohr $this->assertHtmlEquals($expected, $actual); 4972b2703bSAndreas Gohr } 5072b2703bSAndreas Gohr 5172b2703bSAndreas Gohr public function tearDown(): void 5272b2703bSAndreas Gohr { 5372b2703bSAndreas Gohr ModeRegistry::reset(); 5472b2703bSAndreas Gohr parent::tearDown(); 5572b2703bSAndreas Gohr } 5672b2703bSAndreas Gohr 5772b2703bSAndreas Gohr /** 583440a8c0SAndreas Gohr * Render markdown text through DokuWiki's full parser pipeline under 59*13a62f81SAndreas Gohr * the `md` syntax setting, using {@see SpecCompatRenderer} — 603440a8c0SAndreas Gohr * an XHTML renderer subclass that emits the minimal link/media HTML 613440a8c0SAndreas Gohr * shape the GFM spec expects. Production rendering is unchanged; 623440a8c0SAndreas Gohr * this override exists so spec output can be compared byte-for-byte. 6372b2703bSAndreas Gohr */ 6472b2703bSAndreas Gohr private function renderMarkdown(string $text): string 6572b2703bSAndreas Gohr { 6672b2703bSAndreas Gohr global $conf; 67*13a62f81SAndreas Gohr $conf['syntax'] = 'md'; 6872b2703bSAndreas Gohr ModeRegistry::reset(); 6972b2703bSAndreas Gohr 7072b2703bSAndreas Gohr $instructions = p_get_instructions($text); 713440a8c0SAndreas Gohr 723440a8c0SAndreas Gohr $renderer = new SpecCompatRenderer(); 733440a8c0SAndreas Gohr $renderer->reset(); 743440a8c0SAndreas Gohr $renderer->smileys = getSmileys(); 753440a8c0SAndreas Gohr $renderer->entities = getEntities(); 763440a8c0SAndreas Gohr $renderer->acronyms = getAcronyms(); 773440a8c0SAndreas Gohr $renderer->interwiki = getInterwiki(); 783440a8c0SAndreas Gohr 793440a8c0SAndreas Gohr foreach ($instructions as $instruction) { 803440a8c0SAndreas Gohr if (method_exists($renderer, $instruction[0])) { 813440a8c0SAndreas Gohr call_user_func_array([$renderer, $instruction[0]], $instruction[1] ?: []); 823440a8c0SAndreas Gohr } 833440a8c0SAndreas Gohr } 843440a8c0SAndreas Gohr return $renderer->doc; 8572b2703bSAndreas Gohr } 8672b2703bSAndreas Gohr 8772b2703bSAndreas Gohr /** 8872b2703bSAndreas Gohr * Assert two HTML strings are equivalent after whitespace normalization. 8972b2703bSAndreas Gohr * 9072b2703bSAndreas Gohr * DokuWiki's XHTML renderer emits extra whitespace around block tags 9172b2703bSAndreas Gohr * that the spec's reference HTML omits. The comparator strips whitespace 9272b2703bSAndreas Gohr * only around **block-level** tags (p, div, h1-h6, ul/ol/li, table/tr/td, 9372b2703bSAndreas Gohr * blockquote, pre, hr). Whitespace around **inline** tags (em, strong, 9472b2703bSAndreas Gohr * a, code, span, img, br, etc.) is preserved, because `<em>x</em> y` 9572b2703bSAndreas Gohr * and `<em>x</em>y` render differently. 9672b2703bSAndreas Gohr */ 9772b2703bSAndreas Gohr private function assertHtmlEquals(string $expected, string $actual): void 9872b2703bSAndreas Gohr { 9972b2703bSAndreas Gohr $this->assertEquals( 10072b2703bSAndreas Gohr $this->normalizeHtml($expected), 10172b2703bSAndreas Gohr $this->normalizeHtml($actual) 10272b2703bSAndreas Gohr ); 10372b2703bSAndreas Gohr } 10472b2703bSAndreas Gohr 10572b2703bSAndreas Gohr /** 10672b2703bSAndreas Gohr * Strip whitespace adjacent to block-level tags; leave inline tags alone. 1078719732dSAndreas Gohr * 1088719732dSAndreas Gohr * Additionally drops DokuWiki-specific heading decoration that carries no 1098719732dSAndreas Gohr * semantic meaning for GFM-conformance checks: 1108719732dSAndreas Gohr * 1118719732dSAndreas Gohr * - `<div class="levelN">` / matching `</div>` section wrappers the 1128719732dSAndreas Gohr * renderer emits after every header call. 1138719732dSAndreas Gohr * - `class="..."` / `id="..."` attributes on h1-h6 (section-edit anchor 1148719732dSAndreas Gohr * and header-id generation; fine to ignore, the spec output has none). 11572b2703bSAndreas Gohr */ 11672b2703bSAndreas Gohr private function normalizeHtml(string $html): string 11772b2703bSAndreas Gohr { 11872b2703bSAndreas Gohr $block = 'p|div|h[1-6]|hr|ul|ol|li|blockquote|pre|table|thead|tbody|tfoot|tr|th|td'; 11972b2703bSAndreas Gohr 1208719732dSAndreas Gohr // Drop DokuWiki's `<div class="levelN">` section wrappers and the 1218719732dSAndreas Gohr // HTML comments (`<!-- EDIT... -->`) its section-edit machinery 1228719732dSAndreas Gohr // inserts after each heading. Neither is semantically part of the 1238719732dSAndreas Gohr // heading and GFM reference output never contains them. 1248719732dSAndreas Gohr $html = preg_replace('#<div class="level[1-6]">\s*#', '', $html); 1258719732dSAndreas Gohr $html = preg_replace('#\s*</div>\s*#', '', $html); 1268719732dSAndreas Gohr $html = preg_replace('#<!--[^<]*?-->#', '', $html); 1278719732dSAndreas Gohr 1288719732dSAndreas Gohr // Strip sectionedit/id decoration from headings. 1298719732dSAndreas Gohr $html = preg_replace('#<(h[1-6])(?:\s+(?:class|id)="[^"]*")+\s*>#', '<$1>', $html); 1308719732dSAndreas Gohr 13172b2703bSAndreas Gohr // Whitespace before/after an opening block tag (including attributes) 13272b2703bSAndreas Gohr $html = preg_replace('#\s*<(' . $block . ')((?:\s[^>]*)?)>\s*#', '<$1$2>', $html); 13372b2703bSAndreas Gohr // Whitespace before/after a closing block tag 13472b2703bSAndreas Gohr $html = preg_replace('#\s*</(' . $block . ')>\s*#', '</$1>', $html); 13572b2703bSAndreas Gohr 13672b2703bSAndreas Gohr return trim($html); 13772b2703bSAndreas Gohr } 13872b2703bSAndreas Gohr} 139