From c7c26601ae85f90a04937f7cbd95977b0810f472 Mon Sep 17 00:00:00 2001
From: Stefan Froemken <froemken@gmail.com>
Date: Tue, 9 May 2023 09:11:04 +0200
Subject: [PATCH] [BUGFIX] Respect multiple classes in CKEditor5 allowedClasses
 migrator

While migrating CKEditor5 property
buttons.link.properties.class.allowedClasses
the CKEditor5Migrator will now respect multiple classes.

Resolves: #100841
Releases: main, 12.4
Change-Id: I245e8b0c8b2d14ee7917471963f29a9d65480245
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78994
Tested-by: Benjamin Franzke <ben@bnf.dev>
Reviewed-by: Benjamin Kott <benjamin.kott@outlook.com>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Benjamin Franzke <ben@bnf.dev>
Tested-by: Benjamin Kott <benjamin.kott@outlook.com>
---
 .../Classes/Configuration/CKEditor5Migrator.php   | 15 ++++++++-------
 .../Unit/Configuration/CKEditor5MigratorTest.php  |  9 +++++++--
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/typo3/sysext/core/Classes/Configuration/CKEditor5Migrator.php b/typo3/sysext/core/Classes/Configuration/CKEditor5Migrator.php
index 8b21056cf9f1..f5dbacef2944 100644
--- a/typo3/sysext/core/Classes/Configuration/CKEditor5Migrator.php
+++ b/typo3/sysext/core/Classes/Configuration/CKEditor5Migrator.php
@@ -719,7 +719,7 @@ class CKEditor5Migrator
         // Ensure editor.config.style.definitions exists
         $this->configuration['editor']['config']['style']['definitions'] ??= [];
 
-        $allowedClasses = is_array($this->configuration['buttons']['link']['properties']['class']['allowedClasses'])
+        $allowedClassSets = is_array($this->configuration['buttons']['link']['properties']['class']['allowedClasses'])
             ? $this->configuration['buttons']['link']['properties']['class']['allowedClasses']
             : GeneralUtility::trimExplode(',', $this->configuration['buttons']['link']['properties']['class']['allowedClasses'], true);
 
@@ -731,19 +731,20 @@ class CKEditor5Migrator
             }
         }
 
-        foreach ($allowedClasses as $allowedClass) {
+        foreach ($allowedClassSets as $classSet) {
+            $allowedClasses = GeneralUtility::trimExplode(' ', $classSet);
             foreach ($this->configuration['editor']['config']['style']['definitions'] as $styleSetDefinition) {
-                if ($styleSetDefinition['element'] === 'a' && $styleSetDefinition['classes'] === [$allowedClass]) {
-                    // allowedClass is already configured, continue with next one
+                if ($styleSetDefinition['element'] === 'a' && $styleSetDefinition['classes'] === $allowedClasses) {
+                    // allowedClasses is already configured, continue with next one
                     continue 2;
                 }
             }
 
-            // We're still here, this means $allowedClass wasn't found
+            // We're still here, this means $allowedClasses wasn't found
             array_splice($this->configuration['editor']['config']['style']['definitions'], $indexToInsertElementsAt, 0, [[
-                'classes' => [$allowedClass],
+                'classes' => $allowedClasses,
                 'element' => 'a',
-                'name' => $allowedClass, // we lack a human-readable name here...
+                'name' => implode(' ', $allowedClasses), // we lack a human-readable name here...
             ]]);
             $indexToInsertElementsAt++;
         }
diff --git a/typo3/sysext/core/Tests/Unit/Configuration/CKEditor5MigratorTest.php b/typo3/sysext/core/Tests/Unit/Configuration/CKEditor5MigratorTest.php
index e826bbd2acdd..d31f96f35901 100644
--- a/typo3/sysext/core/Tests/Unit/Configuration/CKEditor5MigratorTest.php
+++ b/typo3/sysext/core/Tests/Unit/Configuration/CKEditor5MigratorTest.php
@@ -1557,7 +1557,7 @@ final class CKEditor5MigratorTest extends UnitTestCase
                         'link' => [
                             'properties' => [
                                 'class' => [
-                                    'allowedClasses' => 'link-arrow, link-chevron, class-karl',
+                                    'allowedClasses' => 'link-arrow, btn btn-default, link-chevron, class-karl',
                                 ],
                             ],
                         ],
@@ -1573,6 +1573,11 @@ final class CKEditor5MigratorTest extends UnitTestCase
                                         'element' => 'a',
                                         'name' => 'Link Arrow',
                                     ],
+                                    [
+                                        'classes' => ['btn', 'btn-default'],
+                                        'element' => 'a',
+                                        'name' => 'btn btn-default',
+                                    ],
                                     [
                                         'classes' => ['link-chevron'],
                                         'element' => 'a',
@@ -1616,7 +1621,7 @@ final class CKEditor5MigratorTest extends UnitTestCase
                         'link' => [
                             'properties' => [
                                 'class' => [
-                                    'allowedClasses' => 'link-arrow, link-chevron, class-karl',
+                                    'allowedClasses' => 'link-arrow, btn btn-default, link-chevron, class-karl',
                                 ],
                             ],
                         ],
-- 
GitLab