From feed38b35ac86c22e1e73e00e42b00d90ff4d033 Mon Sep 17 00:00:00 2001 From: maul75 Date: Wed, 29 Nov 2023 13:44:51 +0100 Subject: [PATCH 1/2] Update TinyHtmlMinifier.php Functional extension to enable HTML tags in the script area --- src/TinyHtmlMinifier.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/TinyHtmlMinifier.php b/src/TinyHtmlMinifier.php index d628c06..08a3cdf 100644 --- a/src/TinyHtmlMinifier.php +++ b/src/TinyHtmlMinifier.php @@ -71,7 +71,7 @@ public function minify(string $html) : string $html = $this->removeComments($html); } - $rest = $html; + $rest = $this->replaceJSPlaceholders($html); while (!empty($rest)) { $parts = explode('<', $rest, 2); @@ -79,7 +79,7 @@ public function minify(string $html) : string $rest = (isset($parts[1])) ? $parts[1] : ''; } - return $this->output; + return $this->restoreJSPlaceholders($this->output); } // Walk trough html @@ -283,4 +283,23 @@ private function minifyKeepSpaces($element) { return preg_replace('!\s+!', ' ', $element); } + + // Change opening and closing tags to placeholder in script tags + private function replaceJSPlaceholders($string) + { + $pattern = '/]*>(.*?)<\/script>/s'; + $string = preg_replace_callback($pattern, function($matches) { + $content = str_replace('<', '__OPEN_TAG__', $matches[1]); + $content = str_replace('>', '__CLOSE_TAG__', $content); + return ""; + }, $string); + + return $string; + } + + // Restore / revert opening and closing tags in script tags + private function restoreJSPlaceholders($string) + { + return str_replace(array('__OPEN_TAG__', '__CLOSE_TAG__'), array('<', '>'), $string); + } } From f4435091a6ec741e7374bdea2a789c961158f10a Mon Sep 17 00:00:00 2001 From: maul75 Date: Sun, 3 Dec 2023 15:56:08 +0100 Subject: [PATCH 2/2] Update TinyHtmlMinifier.php Updated the replaceJSPlaceholders function. --- src/TinyHtmlMinifier.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/TinyHtmlMinifier.php b/src/TinyHtmlMinifier.php index 08a3cdf..5cbaf2e 100644 --- a/src/TinyHtmlMinifier.php +++ b/src/TinyHtmlMinifier.php @@ -285,17 +285,18 @@ private function minifyKeepSpaces($element) } // Change opening and closing tags to placeholder in script tags - private function replaceJSPlaceholders($string) - { - $pattern = '/]*>(.*?)<\/script>/s'; - $string = preg_replace_callback($pattern, function($matches) { - $content = str_replace('<', '__OPEN_TAG__', $matches[1]); - $content = str_replace('>', '__CLOSE_TAG__', $content); - return ""; - }, $string); - - return $string; - } + function replaceJSPlaceholders($string) +{ + $pattern = '/]*)>(.*?)<\/script>/s'; + $string = preg_replace_callback($pattern, function($matches) { + $attributes = $matches[1]; + $content = str_replace('<', '__OPEN_TAG__', $matches[2]); + $content = str_replace('>', '__CLOSE_TAG__', $content); + return "{$content}"; + }, $string); + + return $string; +} // Restore / revert opening and closing tags in script tags private function restoreJSPlaceholders($string)