/** * @param $output * @param string $type * @return mixed */ private function minify($output, $type = 'text/html') { $elapsed = $this->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end'); $memory = round(memory_get_usage() / 1024 / 1024, 2) . 'MB'; $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output); if ($this->config->item('minify_output') === TRUE) { $this->load->library('Jsmin', array('')); $size_ini = strlen($output); if ($size_ini === 0) { return ''; } switch ($type) { case 'text/html': // 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($s, 'text/css'), $output); } // Minify the javascript in <script> tags. foreach ($javascript_clean[0] as $s) { $javascript_mini[] = $this->minify($s, 'text/javascript'); } // 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|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($codes_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); } break; case 'text/css': // Remove CSS comments $output = preg_replace('!/\\*[^*]*\\*+([^/][^*]*\\*+)*/!', '', $output); // Remove spaces around curly brackets, colons, semi-colons, parenthesis, commas $output = preg_replace('!\\s*(:|;|,|}|{|\\(|\\))\\s*!', '$1', $output); break; case 'text/javascript': $output = Jsmin::minify($output); break; default: break; } $size_end = $size_ini - strlen($output); log_message('debug', 'Minifier shaved ' . $size_end / 1000 . 'KB (' . round($size_end / $size_ini * 100) . '%) off final HTML output.'); } return $output; }
/** * Minify Javascript * * @uses __construct() * @uses min() * @param string $js Javascript to be minified * @return string */ public static function minify($js) { $jsmin = new Jsmin($js); return $jsmin->min(); }