@@ -12,21 +12,16 @@ final class ContentMinifierEventListener
1212{
1313 public function __invoke (AfterCacheableContentIsGeneratedEvent $ event ): void
1414 {
15- $ event ->getController ()->content = $ this ->minify ( // @phpstan-ignore-line
16- $ event ->getController ()->content // @phpstan-ignore-line
17- );
15+ $ event ->setContent ($ this ->minify ($ event ->getContent ()));
1816 }
1917
2018 /**
21- * remove javascript inline comments
19+ * remove JavaScript inline comments
2220 * convert linebreaks to spaces
2321 * convert tabs to spaces
2422 * convert multiple spaces to one single space
2523 * remove spaces between tags, but ignore on some inline-tags
2624 * replace non-HTML5 conform closing tags
27- * remove type attributes for styles and javascript
28- * remove unnecessary whitespaces from class attributes
29- * remove unnecessary whitespaces from JSON-LD
3025 */
3126 private function minify (string $ content ): string
3227 {
@@ -39,7 +34,18 @@ private function minify(string $content): string
3934 '/" \/>/ ' => '"> ' ,
4035 ];
4136
42- $ content = str_replace (
37+ $ content = $ this ->removeUnnecessaryTypeAttributesForStyleAndScriptTags ($ content );
38+ $ content = $ this ->removeUnnecessaryWhitespacesFromClassAttributes ($ content );
39+ $ content = $ this ->removeUnnecessaryWhitespacesForJsonLdSchemas ($ content );
40+ $ content = $ this ->removeCkeditorDataAttributesFromListItems ($ content );
41+ $ content = $ this ->removeWhitespacesAfterTagStartAndBeforeTagClose ($ content );
42+
43+ return (string )preg_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
44+ }
45+
46+ private function removeUnnecessaryTypeAttributesForStyleAndScriptTags (string $ content ): string
47+ {
48+ return str_replace (
4349 [
4450 ' type="text/css" ' ,
4551 ' type= \'text/css \'' ,
@@ -49,21 +55,54 @@ private function minify(string $content): string
4955 '' ,
5056 $ content
5157 );
58+ }
59+
60+ /**
61+ * @see https://forge.typo3.org/issues/109002
62+ * @see https://github.com/ckeditor/ckeditor5/issues/19006
63+ */
64+ private function removeCkeditorDataAttributesFromListItems (string $ content ): string
65+ {
66+ return (string )preg_replace (
67+ '/(<li)\s+data-list-item-id="[^"]*"/ ' ,
68+ '$1 ' ,
69+ $ content
70+ );
71+ }
72+
73+ private function removeWhitespacesAfterTagStartAndBeforeTagClose (string $ content ): string
74+ {
75+ return (string )preg_replace_callback (
76+ '/<(h[1-6]|p|li|td|th|dt|dd|button|label)[^>]*>\K\s+|\s+(?=<\/(h[1-6]|p|li|td|th|dt|dd|button|label)>)/ ' ,
77+ static fn () => '' ,
78+ $ content
79+ );
80+ }
5281
53- $ content = (string )preg_replace_callback (
82+ private function removeUnnecessaryWhitespacesFromClassAttributes (string $ content ): string
83+ {
84+ return (string )preg_replace_callback (
5485 '/class="([^"]+)"/ ' ,
5586 static function (array $ matches ) {
5687 $ cleanedClassList = trim ((string )preg_replace ('/\s+/ ' , ' ' , $ matches [1 ]));
5788 return 'class=" ' . $ cleanedClassList . '" ' ;
5889 },
5990 $ content
6091 );
92+ }
6193
62- $ content = (string )preg_replace_callback (
94+ private function removeUnnecessaryWhitespacesForJsonLdSchemas (string $ content ): string
95+ {
96+ return (string )preg_replace_callback (
6397 '/<script\s+type="application\/ld\+json">(.*?)<\/script>/s ' ,
6498 static function (array $ matches ) {
6599 $ json = trim ($ matches [1 ]);
66- $ minifiedJson = json_encode (json_decode ($ json , true ), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES );
100+
101+ try {
102+ $ minifiedJson = json_encode (json_decode ($ json , true ), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES );
103+ } catch (\JsonException ) {
104+ return '' ;
105+ }
67106
68107 if ('null ' === $ minifiedJson ) {
69108 return '' ;
@@ -73,7 +112,5 @@ static function (array $matches) {
73112 },
74113 $ content
75114 );
76-
77- return (string )preg_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
78115 }
79116}
0 commit comments