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 /** 5872b2703bSAndreas Gohr * Render markdown text through DokuWiki's full parser + XHTML renderer 5972b2703bSAndreas Gohr * pipeline under the `markdown` syntax setting. 6072b2703bSAndreas Gohr */ 6172b2703bSAndreas Gohr private function renderMarkdown(string $text): string 6272b2703bSAndreas Gohr { 6372b2703bSAndreas Gohr global $conf; 6472b2703bSAndreas Gohr $conf['syntax'] = 'markdown'; 6572b2703bSAndreas Gohr ModeRegistry::reset(); 6672b2703bSAndreas Gohr 6772b2703bSAndreas Gohr $instructions = p_get_instructions($text); 6872b2703bSAndreas Gohr $info = []; 6972b2703bSAndreas Gohr return p_render('xhtml', $instructions, $info); 7072b2703bSAndreas Gohr } 7172b2703bSAndreas Gohr 7272b2703bSAndreas Gohr /** 7372b2703bSAndreas Gohr * Assert two HTML strings are equivalent after whitespace normalization. 7472b2703bSAndreas Gohr * 7572b2703bSAndreas Gohr * DokuWiki's XHTML renderer emits extra whitespace around block tags 7672b2703bSAndreas Gohr * that the spec's reference HTML omits. The comparator strips whitespace 7772b2703bSAndreas Gohr * only around **block-level** tags (p, div, h1-h6, ul/ol/li, table/tr/td, 7872b2703bSAndreas Gohr * blockquote, pre, hr). Whitespace around **inline** tags (em, strong, 7972b2703bSAndreas Gohr * a, code, span, img, br, etc.) is preserved, because `<em>x</em> y` 8072b2703bSAndreas Gohr * and `<em>x</em>y` render differently. 8172b2703bSAndreas Gohr */ 8272b2703bSAndreas Gohr private function assertHtmlEquals(string $expected, string $actual): void 8372b2703bSAndreas Gohr { 8472b2703bSAndreas Gohr $this->assertEquals( 8572b2703bSAndreas Gohr $this->normalizeHtml($expected), 8672b2703bSAndreas Gohr $this->normalizeHtml($actual) 8772b2703bSAndreas Gohr ); 8872b2703bSAndreas Gohr } 8972b2703bSAndreas Gohr 9072b2703bSAndreas Gohr /** 9172b2703bSAndreas Gohr * Strip whitespace adjacent to block-level tags; leave inline tags alone. 92*8719732dSAndreas Gohr * 93*8719732dSAndreas Gohr * Additionally drops DokuWiki-specific heading decoration that carries no 94*8719732dSAndreas Gohr * semantic meaning for GFM-conformance checks: 95*8719732dSAndreas Gohr * 96*8719732dSAndreas Gohr * - `<div class="levelN">` / matching `</div>` section wrappers the 97*8719732dSAndreas Gohr * renderer emits after every header call. 98*8719732dSAndreas Gohr * - `class="..."` / `id="..."` attributes on h1-h6 (section-edit anchor 99*8719732dSAndreas Gohr * and header-id generation; fine to ignore, the spec output has none). 10072b2703bSAndreas Gohr */ 10172b2703bSAndreas Gohr private function normalizeHtml(string $html): string 10272b2703bSAndreas Gohr { 10372b2703bSAndreas Gohr $block = 'p|div|h[1-6]|hr|ul|ol|li|blockquote|pre|table|thead|tbody|tfoot|tr|th|td'; 10472b2703bSAndreas Gohr 105*8719732dSAndreas Gohr // Drop DokuWiki's `<div class="levelN">` section wrappers and the 106*8719732dSAndreas Gohr // HTML comments (`<!-- EDIT... -->`) its section-edit machinery 107*8719732dSAndreas Gohr // inserts after each heading. Neither is semantically part of the 108*8719732dSAndreas Gohr // heading and GFM reference output never contains them. 109*8719732dSAndreas Gohr $html = preg_replace('#<div class="level[1-6]">\s*#', '', $html); 110*8719732dSAndreas Gohr $html = preg_replace('#\s*</div>\s*#', '', $html); 111*8719732dSAndreas Gohr $html = preg_replace('#<!--[^<]*?-->#', '', $html); 112*8719732dSAndreas Gohr 113*8719732dSAndreas Gohr // Strip sectionedit/id decoration from headings. 114*8719732dSAndreas Gohr $html = preg_replace('#<(h[1-6])(?:\s+(?:class|id)="[^"]*")+\s*>#', '<$1>', $html); 115*8719732dSAndreas Gohr 11672b2703bSAndreas Gohr // Whitespace before/after an opening block tag (including attributes) 11772b2703bSAndreas Gohr $html = preg_replace('#\s*<(' . $block . ')((?:\s[^>]*)?)>\s*#', '<$1$2>', $html); 11872b2703bSAndreas Gohr // Whitespace before/after a closing block tag 11972b2703bSAndreas Gohr $html = preg_replace('#\s*</(' . $block . ')>\s*#', '</$1>', $html); 12072b2703bSAndreas Gohr 12172b2703bSAndreas Gohr return trim($html); 12272b2703bSAndreas Gohr } 12372b2703bSAndreas Gohr} 124