From ad3753142e84f4f5165c8edfc8cf14b37816e9c5 Mon Sep 17 00:00:00 2001
From: omniError
Date: Thu, 5 May 2022 12:18:14 -0500
Subject: [PATCH] [HtmlSanitizer] Fix node renderer handling of self-closing
(void) elements
---
Tests/HtmlSanitizerAllTest.php | 14 ++++++++++++--
Visitor/Node/Node.php | 21 ++++++++++++++++++++-
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/Tests/HtmlSanitizerAllTest.php b/Tests/HtmlSanitizerAllTest.php
index 7e53d8c..18c175b 100644
--- a/Tests/HtmlSanitizerAllTest.php
+++ b/Tests/HtmlSanitizerAllTest.php
@@ -237,16 +237,21 @@ public function provideSanitizeBody()
],
[
'',
- '',
+ '
',
],
[
'',
- '',
+ '',
],
[
'
',
'
',
],
+ [
+ '
',
+ '
',
+ ],
+
[
'',
'',
@@ -445,6 +450,11 @@ public function provideSanitizeBody()
'Lorem ipsum',
'Lorem ipsum',
],
+ [
+ '',
+ '',
+ ],
+
[
'Lorem ipsum',
'Lorem ipsum',
diff --git a/Visitor/Node/Node.php b/Visitor/Node/Node.php
index 7683802..8a4e5c3 100644
--- a/Visitor/Node/Node.php
+++ b/Visitor/Node/Node.php
@@ -20,6 +20,25 @@
*/
final class Node implements NodeInterface
{
+ // HTML5 elements which are self-closing
+ private const VOID_ELEMENTS = [
+ 'area' => true,
+ 'base' => true,
+ 'br' => true,
+ 'col' => true,
+ 'embed' => true,
+ 'hr' => true,
+ 'img' => true,
+ 'input' => true,
+ 'keygen' => true,
+ 'link' => true,
+ 'meta' => true,
+ 'param' => true,
+ 'source' => true,
+ 'track' => true,
+ 'wbr' => true,
+ ];
+
private NodeInterface $parent;
private string $tagName;
private array $attributes = [];
@@ -56,7 +75,7 @@ public function addChild(NodeInterface $node): void
public function render(): string
{
- if (!$this->children) {
+ if (isset(self::VOID_ELEMENTS[$this->tagName])) {
return '<'.$this->tagName.$this->renderAttributes().' />';
}