From 32308c194213b2837b70c65a542064a92efb3d1e Mon Sep 17 00:00:00 2001
From: Albrecht Koehnlein <ak@koehnlein.eu>
Date: Mon, 13 Dec 2021 13:02:37 +0100
Subject: [PATCH] [BUGFIX] Recognize correct tag name
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The HTML parser failed to correctly detect single tags with closing `/`
and without white space, e.g. `<br/>`. The old tag name detection only
splitted the tag content by white spaces, which is not enough to
correctly find these valid tags. So the new way is to also remove
trailing slashes from the tag name.

Resolves: #96347
Related: #96307
Releases: main, 11.5
Change-Id: I3acefcbf046600bf118764b873f15b2a1678932d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78575
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
---
 typo3/sysext/core/Classes/Html/HtmlParser.php |  2 +-
 .../core/Tests/Unit/Html/HtmlParserTest.php   | 22 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/core/Classes/Html/HtmlParser.php b/typo3/sysext/core/Classes/Html/HtmlParser.php
index ed1f3fecb3cc..61be53dd7cf4 100644
--- a/typo3/sysext/core/Classes/Html/HtmlParser.php
+++ b/typo3/sysext/core/Classes/Html/HtmlParser.php
@@ -454,7 +454,7 @@ class HtmlParser
                     $endTag = $firstChar === '/' ? 1 : 0;
                     $tagContent = substr($tok, $endTag, $tagEnd - $endTag);
                     $tagParts = preg_split('/\\s+/s', $tagContent, 2);
-                    $tagName = strtolower($tagParts[0]);
+                    $tagName = strtolower(rtrim($tagParts[0], '/'));
                     $emptyTag = 0;
                     if (isset($tags[$tagName])) {
                         // If there is processing to do for the tag:
diff --git a/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php b/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php
index 9f99539a51bc..65bc60c57bf0 100644
--- a/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php
+++ b/typo3/sysext/core/Tests/Unit/Html/HtmlParserTest.php
@@ -152,6 +152,28 @@ class HtmlParserTest extends UnitTestCase
         self::assertSame($expected, $result);
     }
 
+    /**
+     * @test
+     * @dataProvider htmlWithDifferentSingleTagsDataProvider
+     */
+    public function htmlCleanerKeepsSingleTagsWithAndWithoutEndingSlashUnchanged(string $exampleString): void
+    {
+        $result = $this->subject->HTMLcleaner($exampleString, ['br' => true], 0);
+        self::assertSame($exampleString, $result);
+    }
+
+    /**
+     * Data provider for htmlCleanerCanHandleSingleTagsWithEndingSlashes
+     */
+    public static function htmlWithDifferentSingleTagsDataProvider(): array
+    {
+        return [
+            'no slash' => ['one<br>two'],
+            'slash without space' => ['one<br/>two'],
+            'space and slash' => ['one<br />two'],
+        ];
+    }
+
     /**
      * Data provider for spanTagCorrectlyRemovedWhenRmTagIfNoAttribIsConfigured
      */
-- 
GitLab