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;
+    }
 }