From 38ead2b0b4d4ed4ed699e8ac8e64f1a70e7722ae Mon Sep 17 00:00:00 2001
From: Matthias Pigulla
Date: Thu, 29 Jan 2026 17:45:56 +0100
Subject: [PATCH 1/2] Fix handling of self-closing ESI tags
---
demo.php | 11 +++--------
src/Implementation/Html5TagRewriter.php | 17 +++++++++++------
tests/Implementation/Html5TagRewriterTest.php | 15 ++++++++++++---
3 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/demo.php b/demo.php
index 98ea798..2575099 100644
--- a/demo.php
+++ b/demo.php
@@ -23,15 +23,10 @@ public function match(Node $node): void
}
$tagrewriter = new Html5TagRewriter();
-$tagrewriter->register(new DemoRewriteHandler());
+#$tagrewriter->register(new DemoRewriteHandler());
$document = <<
-
-
- link
-
-
+ link
HTML;
-echo $tagrewriter->process($document);
+echo $tagrewriter->processBodyFragment($document);
diff --git a/src/Implementation/Html5TagRewriter.php b/src/Implementation/Html5TagRewriter.php
index c42f2bf..fae6289 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);
+ }
+
+ private function convertEsiEmptyElementsToSelfClosingTags(string $html): string
+ {
+ return preg_replace('#(]*))>#i', '$1 />', $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' => [
+ ''
];
}
@@ -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' => [
From 751d9e4ca395cc44554f42787b45d48b53afdee1 Mon Sep 17 00:00:00 2001
From: Matthias Pigulla
Date: Thu, 29 Jan 2026 17:49:20 +0100
Subject: [PATCH 2/2] Deal with potential `null` values
---
demo.php | 11 ++++++++---
src/Implementation/Html5TagRewriter.php | 4 ++--
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/demo.php b/demo.php
index 2575099..98ea798 100644
--- a/demo.php
+++ b/demo.php
@@ -23,10 +23,15 @@ public function match(Node $node): void
}
$tagrewriter = new Html5TagRewriter();
-#$tagrewriter->register(new DemoRewriteHandler());
+$tagrewriter->register(new DemoRewriteHandler());
$document = <<link
+
+
+
+ link
+
+
HTML;
-echo $tagrewriter->processBodyFragment($document);
+echo $tagrewriter->process($document);
diff --git a/src/Implementation/Html5TagRewriter.php b/src/Implementation/Html5TagRewriter.php
index fae6289..f2829b9 100644
--- a/src/Implementation/Html5TagRewriter.php
+++ b/src/Implementation/Html5TagRewriter.php
@@ -77,11 +77,11 @@ private function applyHandlers(Document $document, Node $context): void
private function convertEsiSelfClosingTagsToEmptyElements(string $html): string
{
- return preg_replace('#(]*))/>#i', '$1>', $html);
+ return preg_replace('#(]*))/>#i', '$1>', $html) ?? $html;
}
private function convertEsiEmptyElementsToSelfClosingTags(string $html): string
{
- return preg_replace('#(]*))>#i', '$1 />', $html);
+ return preg_replace('#(]*))>#i', '$1 />', $html) ?? $html;
}
}