From 2d87c496e37257fb2174373c6b12ec1b22458b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B5=D0=BD=D0=B5=D0=BB=D0=B8=D0=BD=20=D0=98=D0=BB?= =?UTF-8?q?=D0=B8=D0=B5=D0=B2?= Date: Wed, 18 Jan 2017 18:37:20 +0200 Subject: [PATCH] Update HtmlMinify.php --- app/Http/Middleware/HtmlMinify.php | 137 ++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 14 deletions(-) diff --git a/app/Http/Middleware/HtmlMinify.php b/app/Http/Middleware/HtmlMinify.php index 76f7b47..a78ddd4 100644 --- a/app/Http/Middleware/HtmlMinify.php +++ b/app/Http/Middleware/HtmlMinify.php @@ -48,20 +48,46 @@ public function handle($request, Closure $next) if ($this->isAResponseObject($response) && $this->isAnHtmlResponse($response) && !Administration::routeInAdministration()) { $output = $response->getContent(); - /* remove comments */ - $output = preg_replace('%/\*(?:(?!\*/|/\*).)*+(?:/\*(?:(?!\*/|/\*).)*+\*/(?:(?!\*/|/\*).)*+)*+.*?\*/%s', '', $output); - $output = preg_replace('//simx', '', $output); - /* remove tabs, spaces, newlines, etc. */ - $output = str_replace(array( - "\r\n", - "\r", - "\n", - "\t", - ' ', - ' ' - ), '', $output); - - $output = trim($output); + //convert encoding to utf8 + $output = mb_convert_encoding($output, 'utf8'); + + if (strpos($output, '
') !== false) {
+                $replace = array(
+                    '//s' => '',
+                    "/<\?php/" => ' '',
+                    "/>\n '><',
+                    "/>\s+\n '><',
+                    "/>\n\s+ '><'
+                );
+            } else {
+                $replace = array(
+                    '//s' => '',
+                    "/<\?php/" => ' '$1',
+                    "/\r/" => '',
+                    "/\n/" => '',
+                    "/\t/" => '',
+                    "/ +/" => ' '
+                );
+            }
+
+            // Remove htmlcomment;
+            $additionaly = array(
+                // strip whitespaces after tags, except space
+                '/\>[^\S ]+/s' => '>',
+                // strip whitespaces before tags, except space
+                '/[^\S ]+\ '<',
+                // shorten multiple whitespace sequences
+                '/(\s)+/s' => '\\1',
+                // Remove htmlcomment
+                '!/\*.*?\*/!s' => '',
+                '/\n\s*\n/' => ''
+            );
+
+            $output = preg_replace(array_keys($replace), array_values($replace), $output);
+            $output = preg_replace(array_keys($additionaly), array_values($additionaly), $output);
+            $output = $output = $this->compress($output);
 
             $output .= "\n\n" . '/Uis', " ", sanitize_output($buffer));
+        // $new_buffer = preg_replace('/\s+/', " ", sanitize_output($new_buffer));
+        $new_buffer = mb_ereg_replace($regexRemoveWhiteSpace, " ", $this->sanitize_output($buffer));
+
+        // We are going to check if processing has working
+        if ($new_buffer === null) {
+            $new_buffer = $buffer;
+        }
+
+        return $new_buffer;
+    }
+
+    function sanitize_output($buffer)
+    {
+        $search = array(
+            '/\>[^\S ]+/s', // strip whitespaces after tags, except space
+            '/[^\S ]+\',
+            '<',
+            '\\1',
+            '',
+            ''
+        );
+        $buffer = preg_replace($search, $replace, $buffer);
+        return $buffer;
+    }
 }