Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Implementation/Html5TagRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function register(RewriteHandler $handler): void
#[Override]
public function process(string $html5): string
{
$document = HTMLDocument::createFromString($html5, LIBXML_NOERROR);
$document = HTMLDocument::createFromString($this->convertEsiSelfClosingTagsToEmptyElements($html5), LIBXML_NOERROR);

$this->applyHandlers($document, $document);

return $this->cleanup($document->saveHtml());
return $this->convertEsiEmptyElementsToSelfClosingTags($document->saveHtml());
}

#[Override]
Expand All @@ -51,11 +51,11 @@ public function processBodyFragment(string $html5Fragment): string
$container = $document->body;
assert($container !== null);

$container->innerHTML = $html5Fragment;
$container->innerHTML = $this->convertEsiSelfClosingTagsToEmptyElements($html5Fragment);

$this->applyHandlers($document, $container);

return $this->cleanup($container->innerHTML);
return $this->convertEsiEmptyElementsToSelfClosingTags($container->innerHTML);
}

private function applyHandlers(Document $document, Node $context): void
Expand All @@ -75,8 +75,13 @@ private function applyHandlers(Document $document, Node $context): void
}
}

private function cleanup(string $html): string
private function convertEsiSelfClosingTagsToEmptyElements(string $html): string
{
return preg_replace('#(<esi:([a-z]+)(?:[^>]*))></esi:\\2>#', '$1 />', $html) ?? $html;
return preg_replace('#(<esi:([a-z]+)(?:[^>]*))/>#i', '$1></esi:\\2>', $html) ?? $html;
}

private function convertEsiEmptyElementsToSelfClosingTags(string $html): string
{
return preg_replace('#(<esi:([a-z]+)(?:[^>]*))></esi:\\2>#i', '$1 />', $html) ?? $html;
}
}
15 changes: 12 additions & 3 deletions tests/Implementation/Html5TagRewriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,16 @@ public static function providePreservedFragments(): iterable
];

yield 'ESI tag' => [
'<esi:include src="url" />',
'<esi:include src="url?foo=bar&amp;bar=baz" />',
];

yield 'ESI tags in context' => [
'<div><p>test</p>
<hr>
<esi:include src="/_fragment?_hash=123&amp;foo=bar" />
<hr>
<esi:include src="/_fragment?_hash=456" />
</div>'
];
}

Expand All @@ -201,8 +210,8 @@ public function processBodyFragment_preserves_fragment(string $fragment): void
public static function provideFragmentsCleanedUp(): iterable
{
yield 'empty ESI include tag' => [
'<esi:include src="url">',
'<esi:include src="url" />',
'<esi:include src="url?foo=bar&amp;bar=baz"/>',
'<esi:include src="url?foo=bar&amp;bar=baz" />',
];

yield 'qouted entities are replaced' => [
Expand Down