Skip to content

Commit 5acd269

Browse files
committed
feat: add ckeditor data attribute removal for list items and remove unnecessary whitespaces after opening and before closing tags
1 parent 6ac5748 commit 5acd269

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

Classes/EventListener/ContentMinifierEventListener.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ public function __invoke(AfterCacheableContentIsGeneratedEvent $event): void
1616
}
1717

1818
/**
19-
* remove javascript inline comments
19+
* remove JavaScript inline comments
2020
* convert linebreaks to spaces
2121
* convert tabs to spaces
2222
* convert multiple spaces to one single space
2323
* remove spaces between tags, but ignore on some inline-tags
2424
* replace non-HTML5 conform closing tags
25-
* remove type attributes for styles and javascript
26-
* remove unnecessary whitespaces from class attributes
27-
* remove unnecessary whitespaces from JSON-LD
2825
*/
2926
private function minify(string $content): string
3027
{
@@ -37,7 +34,18 @@ private function minify(string $content): string
3734
'/" \/>/' => '">',
3835
];
3936

40-
$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(
4149
[
4250
' type="text/css"',
4351
' type=\'text/css\'',
@@ -47,17 +55,45 @@ private function minify(string $content): string
4755
'',
4856
$content
4957
);
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+
}
5081

51-
$content = (string)preg_replace_callback(
82+
private function removeUnnecessaryWhitespacesFromClassAttributes(string $content): string
83+
{
84+
return (string)preg_replace_callback(
5285
'/class="([^"]+)"/',
5386
static function (array $matches) {
5487
$cleanedClassList = trim((string)preg_replace('/\s+/', ' ', $matches[1]));
5588
return 'class="' . $cleanedClassList . '"';
5689
},
5790
$content
5891
);
92+
}
5993

60-
$content = (string)preg_replace_callback(
94+
private function removeUnnecessaryWhitespacesForJsonLdSchemas(string $content): string
95+
{
96+
return (string)preg_replace_callback(
6197
'/<script\s+type="application\/ld\+json">(.*?)<\/script>/s',
6298
static function (array $matches) {
6399
$json = trim($matches[1]);
@@ -76,7 +112,5 @@ static function (array $matches) {
76112
},
77113
$content
78114
);
79-
80-
return (string)preg_replace(array_keys($replacements), array_values($replacements), $content);
81115
}
82116
}

0 commit comments

Comments
 (0)