Skip to content

Commit

Permalink
Update HtmlMinify.php
Browse files Browse the repository at this point in the history
  • Loading branch information
veneliniliev authored Jan 18, 2017
1 parent 6dca30e commit 2d87c49
Showing 1 changed file with 123 additions and 14 deletions.
137 changes: 123 additions & 14 deletions app/Http/Middleware/HtmlMinify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<pre>') !== false) {
$replace = array(
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\r/" => '',
"/>\n</" => '><',
"/>\s+\n</" => '><',
"/>\n\s+</" => '><'
);
} else {
$replace = array(
'/<!--[^\[](.*?)[^\]]-->/s' => '',
"/<\?php/" => '<?php ',
"/\n([\S])/" => '$1',
"/\r/" => '',
"/\n/" => '',
"/\t/" => '',
"/ +/" => ' '
);
}

// Remove htmlcomment;
$additionaly = array(
// strip whitespaces after tags, except space
'/\>[^\S ]+/s' => '>',
// strip whitespaces before tags, except space
'/[^\S ]+\</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" . '<!--
Expand Down Expand Up @@ -111,4 +137,87 @@ protected function isAnHtmlResponse(Response $response)
$type = $response->headers->get('Content-Type');
return strtolower(strtok($type, ';')) === 'text/html';
}

function compress($buffer)
{
/**
* To remove useless whitespace from generated HTML, except for Javascript.
* [Regex Source]
* https://github.com/bcit-ci/codeigniter/wiki/compress-html-output
* http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter
*/
$regexRemoveWhiteSpace = '%# Collapse ws everywhere but in blacklisted elements.
(?> # Match all whitespaces other than single space.
[^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws,
| \s{2,} # or two or more consecutive-any-whitespace.
) # Note: The remaining regex consumes no text at all...
(?= # Ensure we are not in a blacklist tag.
(?: # Begin (unnecessary) group.
(?: # Zero or more of...
[^<]++ # Either one or more non-"<"
| < # or a < starting a non-blacklist tag.
(?!/?(?:textarea|pre)\b)
)*+ # (This could be "unroll-the-loop"ified.)
) # End (unnecessary) group.
(?: # Begin alternation group.
< # Either a blacklist start tag.
(?>textarea|pre)\b
| \z # or end of file.
) # End alternation group.
) # If we made it here, we are not in a blacklist tag.
%ix';

$regexRemoveWhiteSpace = '%(?>[^\S ]\s*| \s{2,})(?=(?:(?:[^<]++| <(?!/?(?:textarea|pre)\b))*+)(?:<(?>textarea|pre)\b|\z))%ix';
$re = '%# Collapse whitespace everywhere but in blacklisted elements.
(?> # Match all whitespans other than single space.
[^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws,
| \s{2,} # or two or more consecutive-any-whitespace.
) # Note: The remaining regex consumes no text at all...
(?= # Ensure we are not in a blacklist tag.
[^<]*+ # Either zero or more non-"<" {normal*}
(?: # Begin {(special normal*)*} construct
< # or a < starting a non-blacklist tag.
(?!/?(?:textarea|pre|script)\b)
[^<]*+ # more non-"<" {normal*}
)*+ # Finish "unrolling-the-loop"
(?: # Begin alternation group.
< # Either a blacklist start tag.
(?>textarea|pre|script)\b
| \z # or end of file.
) # End alternation group.
) # If we made it here, we are not in a blacklist tag.
%Six';

// $new_buffer = preg_replace('/<!--(.*|\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 ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'!/\*.*?\*/!s', // Remove htmlcomment
'/\n\s*\n/'
); // Remove htmlcomment

$replace = array(
'>',
'<',
'\\1',
'',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
}

0 comments on commit 2d87c49

Please sign in to comment.