From 64859ceaf4e9a7fbf6949d05869c58adeccc7b8e Mon Sep 17 00:00:00 2001
From: Friedemann Altrock <hallo@faltrock.de>
Date: Fri, 25 Aug 2023 17:34:05 +0200
Subject: [PATCH] [BUGFIX] Reset RteHtmlParser properly when configuration
 changes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Before this fix, only the first RTE processing configuration would be
taken into account when deciding which tags to keep in an HTML snippet.

RteHtmlParser is a DI service and as such is treated as a singleton.
Using multiple RTE processing configurations with potentially different
sets of allowed tags should work as expected.

Resolves: #101190
Releases: main, 12.4, 11.5
Change-Id: Ib0227872b542f53b30a16494aa2f979b8f601e07
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80833
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
---
 .../core/Classes/Html/RteHtmlParser.php       |  2 ++
 .../Tests/Unit/Html/RteHtmlParserTest.php     | 21 +++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/typo3/sysext/core/Classes/Html/RteHtmlParser.php b/typo3/sysext/core/Classes/Html/RteHtmlParser.php
index 30a8158c091c..c53d497d5a04 100644
--- a/typo3/sysext/core/Classes/Html/RteHtmlParser.php
+++ b/typo3/sysext/core/Classes/Html/RteHtmlParser.php
@@ -138,6 +138,8 @@ class RteHtmlParser extends HtmlParser implements LoggerAwareInterface
     protected function setProcessingConfiguration(array $processingConfiguration): void
     {
         $this->procOptions = $processingConfiguration;
+        $this->getKeepTags_cache = [];
+
         if (isset($this->procOptions['allowedClasses.'])) {
             $this->allowedClasses = (array)$this->procOptions['allowedClasses.'];
         } else {
diff --git a/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php b/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php
index 8f797541915d..33258fdf9c00 100644
--- a/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php
+++ b/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php
@@ -760,4 +760,25 @@ class RteHtmlParserTest extends UnitTestCase
         self::assertEquals('<figure class="table">' . CRLF . '<table>Allowed outside of p-tag</table>' . CRLF . '</figure>', $subject->transformTextForRichTextEditor('<figure class="table">' . CRLF . '<table>Allowed outside of p-tag</table>' . CRLF . '</figure>', $this->procOptions));
         self::assertEquals('<figure class="table">' . CRLF . '<table>Allowed outside of p-tag</table>' . CRLF . '<figcaption>My Logo</figcaption></figure>', $subject->transformTextForRichTextEditor('<figure class="table">' . CRLF . '<table>Allowed outside of p-tag</table>' . CRLF . '<figcaption>My Logo</figcaption></figure>', $this->procOptions));
     }
+
+    /**
+     * @test
+     */
+    public function resetsAllowTagsWhenProcessingConfigurationChanges(): void
+    {
+        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
+        $subject = new RteHtmlParser($eventDispatcher);
+        $input = '<remove>Foo</remove><keep>Bar</keep>';
+        $transformed = 'Foo<keep>Bar</keep>';
+        $result = $subject->transformTextForPersistence($input, [
+            'mode' => 'default',
+            'allowTags' => 'keep',
+        ]);
+        self::assertEquals($transformed, $result);
+        $result = $subject->transformTextForPersistence($input, [
+            'mode' => 'default',
+            'allowTags' => 'keep,remove',
+        ]);
+        self::assertEquals($input, $result);
+    }
 }
-- 
GitLab