diff --git a/src/Implementation/Html5TagRewriter.php b/src/Implementation/Html5TagRewriter.php index c42f2bf..f2829b9 100644 --- a/src/Implementation/Html5TagRewriter.php +++ b/src/Implementation/Html5TagRewriter.php @@ -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] @@ -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 @@ -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('#(]*))>#', '$1 />', $html) ?? $html; + return preg_replace('#(]*))/>#i', '$1>', $html) ?? $html; + } + + private function convertEsiEmptyElementsToSelfClosingTags(string $html): string + { + return preg_replace('#(]*))>#i', '$1 />', $html) ?? $html; } } diff --git a/tests/Implementation/Html5TagRewriterTest.php b/tests/Implementation/Html5TagRewriterTest.php index ef0f988..d5c368d 100644 --- a/tests/Implementation/Html5TagRewriterTest.php +++ b/tests/Implementation/Html5TagRewriterTest.php @@ -185,7 +185,16 @@ public static function providePreservedFragments(): iterable ]; yield 'ESI tag' => [ - '', + '', + ]; + + yield 'ESI tags in context' => [ + '

test

+
+ +
+ +
' ]; } @@ -201,8 +210,8 @@ public function processBodyFragment_preserves_fragment(string $fragment): void public static function provideFragmentsCleanedUp(): iterable { yield 'empty ESI include tag' => [ - '', - '', + '', + '', ]; yield 'qouted entities are replaced' => [