Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Normalize HTML before comparing.
Browse files Browse the repository at this point in the history
This is mostly needed because of the tagfilter extension:

- it will convert `<foo>` into `&lt;foo>`
- that in turn gets parsed as `(entity '&lt;')(textual_content 'foo>')`
- that in turn gets rendered as `&lt;foo&gt;`
- so we need a way for the two to compare equal
  • Loading branch information
fredemmott committed Mar 29, 2018
1 parent a94c631 commit 93818dc
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions tests/SpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 93818dc

Please sign in to comment.