diff options
author | Andrey Andreev <narf@devilix.net> | 2014-12-15 16:28:23 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-12-15 16:28:23 +0100 |
commit | 466e8ccb0ad647fd8e477e881dfddc14c6d7cbc8 (patch) | |
tree | b6d5228e553f821147bdae2a1f307bbcf3404f41 | |
parent | 8586d0d51efb9c8a756ebfdcd3b341aafaa7affc (diff) |
Remove output minifier
This feature has proven to be problematic and it's not nearly
as flexible as a dedicated minifier library like Minify
(http://www.minifier.org/, https://github.com/matthiasmullie/minify).
The same results in terms of saving traffic can also be achievied via
gzip compression (which should also be done on the httpd level, but we
also support anyway) and stuff like mod_pagespeed.
Reverts PR #965
Related issues as a track record proving how problematic this has been:
#2078 #1499 #2163 #2092 #2387 #2637 #2710 #2120 #2171 #2631 #2326 #2795
#2791 #2772
Additionally, the count of contributors suggesting that the only way
to fix the minifier problems is to remove it, is around the same as
the count of people suggesting the feature to be implemented in the
first place. It was experimental anyway ... the experiment failed.
-rw-r--r-- | application/config/config.php | 13 | ||||
-rw-r--r-- | system/core/Output.php | 208 |
2 files changed, 0 insertions, 221 deletions
diff --git a/application/config/config.php b/application/config/config.php index 4ee33358a..4958b467e 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -466,18 +466,6 @@ $config['compress_output'] = FALSE; /* |-------------------------------------------------------------------------- -| Minify -|-------------------------------------------------------------------------- -| -| Removes extra characters (usually unnecessary spaces) from your -| output for faster page load speeds. Makes your outputted HTML source -| code less readable. -| -*/ -$config['minify_output'] = FALSE; - -/* -|-------------------------------------------------------------------------- | Master Time Reference |-------------------------------------------------------------------------- | @@ -489,7 +477,6 @@ $config['minify_output'] = FALSE; */ $config['time_reference'] = 'local'; - /* |-------------------------------------------------------------------------- | Rewrite PHP Short Tags diff --git a/system/core/Output.php b/system/core/Output.php index e8f0b1590..09d251fe2 100644 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -426,14 +426,6 @@ class CI_Output { // -------------------------------------------------------------------- - // Is minify requested? - if ($CFG->item('minify_output') === TRUE) - { - $output = $this->minify($output, $this->mime_type); - } - - // -------------------------------------------------------------------- - // Do we need to write a cache file? Only if the controller does not have its // own _output() method and we are not dealing with a cache file, which we // can determine by the existence of the $CI object above @@ -784,206 +776,6 @@ class CI_Output { } } - // -------------------------------------------------------------------- - - /** - * Minify - * - * Reduce excessive size of HTML/CSS/JavaScript content. - * - * @param string $output Output to minify - * @param string $type Output content MIME type - * @return string Minified output - */ - public function minify($output, $type = 'text/html') - { - switch ($type) - { - case 'text/html': - - if (($size_before = strlen($output)) === 0) - { - return ''; - } - - // Find all the <pre>,<code>,<textarea>, and <javascript> tags - // We'll want to return them to this unprocessed state later. - preg_match_all('{<pre.+</pre>}msU', $output, $pres_clean); - preg_match_all('{<code.+</code>}msU', $output, $codes_clean); - preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_clean); - preg_match_all('{<script.+</script>}msU', $output, $javascript_clean); - - // Minify the CSS in all the <style> tags. - preg_match_all('{<style.+</style>}msU', $output, $style_clean); - foreach ($style_clean[0] as $s) - { - $output = str_replace($s, $this->_minify_js_css($s, 'css', TRUE), $output); - } - - // Minify the javascript in <script> tags. - foreach ($javascript_clean[0] as $s) - { - $javascript_mini[] = $this->_minify_js_css($s, 'js', TRUE); - } - - // Replace multiple spaces with a single space. - $output = preg_replace('!\s{2,}!', ' ', $output); - - // Remove comments (non-MSIE conditionals) - $output = preg_replace('{\s*<!--[^\[<>].*(?<!!)-->\s*}msU', '', $output); - - // Remove spaces around block-level elements. - $output = preg_replace('/\s*(<\/?(html|head|title|meta|script|link|style|body|table|thead|tbody|tfoot|tr|th|td|h[1-6]|div|p|br)[^>]*>)\s*/is', '$1', $output); - - // Replace mangled <pre> etc. tags with unprocessed ones. - - if ( ! empty($pres_clean)) - { - preg_match_all('{<pre.+</pre>}msU', $output, $pres_messed); - $output = str_replace($pres_messed[0], $pres_clean[0], $output); - } - - if ( ! empty($codes_clean)) - { - preg_match_all('{<code.+</code>}msU', $output, $codes_messed); - $output = str_replace($codes_messed[0], $codes_clean[0], $output); - } - - if ( ! empty($textareas_clean)) - { - preg_match_all('{<textarea.+</textarea>}msU', $output, $textareas_messed); - $output = str_replace($textareas_messed[0], $textareas_clean[0], $output); - } - - if (isset($javascript_mini)) - { - preg_match_all('{<script.+</script>}msU', $output, $javascript_messed); - $output = str_replace($javascript_messed[0], $javascript_mini, $output); - } - - $size_removed = $size_before - strlen($output); - $savings_percent = round(($size_removed / $size_before * 100)); - - log_message('debug', 'Minifier shaved '.($size_removed / 1000).'KB ('.$savings_percent.'%) off final HTML output.'); - - break; - - case 'text/css': - - return $this->_minify_js_css($output, 'css'); - - case 'text/javascript': - case 'application/javascript': - case 'application/x-javascript': - - return $this->_minify_js_css($output, 'js'); - - default: break; - } - - return $output; - } - - // -------------------------------------------------------------------- - - /** - * Minify JavaScript and CSS code - * - * Strips comments and excessive whitespace characters - * - * @param string $output - * @param string $type 'js' or 'css' - * @param bool $tags Whether $output contains the 'script' or 'style' tag - * @return string - */ - protected function _minify_js_css($output, $type, $tags = FALSE) - { - if ($tags === TRUE) - { - $tags = array('close' => strrchr($output, '<')); - - $open_length = strpos($output, '>') + 1; - $tags['open'] = substr($output, 0, $open_length); - - $output = substr($output, $open_length, -strlen($tags['close'])); - - // Strip spaces from the tags - $tags = preg_replace('#\s{2,}#', ' ', $tags); - } - - $output = trim($output); - - if ($type === 'js') - { - // Catch all string literals and comment blocks - if (preg_match_all('#((?:((?<!\\\)\'|")|(/\*)|(//)).*(?(2)(?<!\\\)\2|(?(3)\*/|\n)))#msuUS', $output, $match, PREG_OFFSET_CAPTURE)) - { - $js_literals = $js_code = array(); - for ($match = $match[0], $c = count($match), $i = $pos = $offset = 0; $i < $c; $i++) - { - $js_code[$pos++] = trim(substr($output, $offset, $match[$i][1] - $offset)); - $offset = $match[$i][1] + strlen($match[$i][0]); - - // Save only if we haven't matched a comment block - if ($match[$i][0][0] !== '/') - { - $js_literals[$pos++] = array_shift($match[$i]); - } - } - $js_code[$pos] = substr($output, $offset); - - // $match might be quite large, so free it up together with other vars that we no longer need - unset($match, $offset, $pos); - } - else - { - $js_code = array($output); - $js_literals = array(); - } - - $varname = 'js_code'; - } - else - { - $varname = 'output'; - } - - // Standartize new lines - $$varname = str_replace(array("\r\n", "\r"), "\n", $$varname); - - if ($type === 'js') - { - $patterns = array( - '#\s*([!\#%&()*+,\-./:;<=>?@\[\]^`{|}~])\s*#' => '$1', // Remove spaces following and preceeding JS-wise non-special & non-word characters - '#\s{2,}#' => ' ' // Reduce the remaining multiple whitespace characters to a single space - ); - } - else - { - $patterns = array( - '#/\*.*(?=\*/)\*/#s' => '', // Remove /* block comments */ - '#\n?//[^\n]*#' => '', // Remove // line comments - '#\s*([^\w.\#%])\s*#U' => '$1', // Remove spaces following and preceeding non-word characters, excluding dots, hashes and the percent sign - '#\s{2,}#' => ' ' // Reduce the remaining multiple space characters to a single space - ); - } - - $$varname = preg_replace(array_keys($patterns), array_values($patterns), $$varname); - - // Glue back JS quoted strings - if ($type === 'js') - { - $js_code += $js_literals; - ksort($js_code); - $output = implode($js_code); - unset($js_code, $js_literals, $varname, $patterns); - } - - return is_array($tags) - ? $tags['open'].$output.$tags['close'] - : $output; - } - } /* End of file Output.php */ |