From ef15a77107863dcf2f11913b3b6522b2cda36ee0 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/+/80832
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
---
 .../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 54078c845215..a16f5e21d7d8 100644
--- a/typo3/sysext/core/Classes/Html/RteHtmlParser.php
+++ b/typo3/sysext/core/Classes/Html/RteHtmlParser.php
@@ -119,6 +119,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 f7ea24b92b27..40979236d58b 100644
--- a/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php
+++ b/typo3/sysext/core/Tests/Unit/Html/RteHtmlParserTest.php
@@ -754,4 +754,25 @@ final 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