public function minifyHtml($strHtml)
{
// The feature has been disabled
if (!\Config::get('minifyMarkup') || \Config::get('debugMode')) {
return $strHtml;
}
// Split the markup based on the tags that shall be preserved
$arrChunks = preg_split('@(</?pre[^>]*>)|(</?script[^>]*>)|(</?style[^>]*>)|( ?</?textarea[^>]*>)@i', $strHtml, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$strHtml = '';
$blnPreserveNext = false;
$blnOptimizeNext = false;
$strType = null;
// Check for valid JavaScript types (see #7927)
$isJavaScript = function ($strChunk) {
$typeMatch = array();
if (preg_match('/\\stype\\s*=\\s*(?:(?J)(["\'])\\s*(?<type>.*?)\\s*\\1|(?<type>[^\\s>]+))/i', $strChunk, $typeMatch) && !in_array(strtolower($typeMatch['type']), static::$validJavaScriptTypes)) {
return false;
}
if (preg_match('/\\slanguage\\s*=\\s*(?:(?J)(["\'])\\s*(?<type>.*?)\\s*\\1|(?<type>[^\\s>]+))/i', $strChunk, $typeMatch) && !in_array('text/' . strtolower($typeMatch['type']), static::$validJavaScriptTypes)) {
return false;
}
return true;
};
// Recombine the markup
foreach ($arrChunks as $strChunk) {
if (strncasecmp($strChunk, '<pre', 4) === 0 || strncasecmp(ltrim($strChunk), '<textarea', 9) === 0) {
$blnPreserveNext = true;
} elseif (strncasecmp($strChunk, '<script', 7) === 0) {
if ($isJavaScript($strChunk)) {
$blnOptimizeNext = true;
$strType = 'js';
} else {
$blnPreserveNext = true;
}
} elseif (strncasecmp($strChunk, '<style', 6) === 0) {
$blnOptimizeNext = true;
$strType = 'css';
} elseif ($blnPreserveNext) {
$blnPreserveNext = false;
} elseif ($blnOptimizeNext) {
$blnOptimizeNext = false;
// Minify inline scripts
if ($strType == 'js') {
$objMinify = new Minify\JS();
$objMinify->add($strChunk);
$strChunk = $objMinify->minify();
} elseif ($strType == 'css') {
$objMinify = new Minify\CSS();
$objMinify->add($strChunk);
$strChunk = $objMinify->minify();
}
} else {
// Remove line indentations and trailing spaces
$strChunk = str_replace("\r", '', $strChunk);
$strChunk = preg_replace(array('/^[\\t ]+/m', '/[\\t ]+$/m', '/\\n\\n+/'), array('', '', "\n"), $strChunk);
}
$strHtml .= $strChunk;
}
return trim($strHtml);
}