From 93818dce53879d1468a2ce876269d70ec03a29eb Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Thu, 29 Mar 2018 13:15:09 -0700 Subject: [PATCH] Normalize HTML before comparing. This is mostly needed because of the tagfilter extension: - it will convert `` into `<foo>` - that in turn gets parsed as `(entity '<')(textual_content 'foo>')` - that in turn gets rendered as `<foo>` - so we need a way for the two to compare equal --- tests/SpecTest.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/tests/SpecTest.php b/tests/SpecTest.php index 311de0c..58c0b82 100644 --- a/tests/SpecTest.php +++ b/tests/SpecTest.php @@ -110,15 +110,39 @@ public function testSpecExampleNormalizesWithoutHTMLChange( print($original_md."\n"); print("--- NORMALIZED ---\n"); print($normalized_md."\n"); - print("--- AST ---\n"); + print("--- ORIGINAL AST ---\n"); \var_dump($ast); print("--- END ---\n"); - $this->assertExampleMatches( - $name, - $normalized_md, + $normalized_ast = parse($parser_ctx, $normalized_md); + $actual_html = (new HTMLRenderer($render_ctx))->render($normalized_ast); + + $actual_html = self::normalizeHTML($actual_html); + $expected_html = self::normalizeHTML($expected_html); + + expect($actual_html)->toBeSame( $expected_html, - $extension, + "HTML differs for %s:\n%s", + $name, + $original_md, ); } + + private static function normalizeHTML(string $html): string { + if ($html === '') { + return ''; + } + $html = Str\replace($html, "\t", self::TAB_REPLACEMENT); + $old = \libxml_use_internal_errors(true); + try { + $doc = new \DOMDocument(); + $doc->loadHTML( + $html, + \LIBXML_NOENT | \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD, + ); + return $doc->saveHTML(); + } finally { + \libxml_use_internal_errors($old); + } + } }