From: Petr Skoda Date: Tue, 5 Jan 2010 21:45:51 +0000 (+0000) Subject: MDL-21252 home-grown CSS minimisation X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e10f304d19581c8475a0cfb124a2b918e56bb3da;p=moodle.git MDL-21252 home-grown CSS minimisation --- diff --git a/theme/styles.php b/theme/styles.php index 39a7b05233..9ff8a9d1da 100644 --- a/theme/styles.php +++ b/theme/styles.php @@ -82,9 +82,11 @@ if ($type === 'editor') { $sheet = ''; foreach($value as $val) { if (is_array($val)) { - $sheet .= "\n\n".implode("\n\n", $val); + foreach ($val as $k=>$v) { + $sheet .= compress_css($v)."\n"; + } } else { - $sheet .= "\n\n".$val; + $sheet .= compress_css($val)."\n"; } } $css[$key] = $sheet; @@ -129,9 +131,7 @@ EOF; header('Pragma: '); header('Accept-Ranges: none'); header('Content-Type: text/css'); - if (!min_enable_zlib_compression()) { - header('Content-Length: '.strlen($css)); - } + header('Content-Length: '.strlen($css)); echo $css; die; @@ -153,3 +153,22 @@ function send_cached_css($csspath, $rev) { readfile($csspath); die; } + +function compress_css($css) { + // remove comments - credit for this regex goes to "reinhold weber" + $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); + + // replace newlines, tabs, etc. + $css = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $css); + + // replace repeated spaces + $css = preg_replace('/ +/', ' ', $css); + + // fix whitespace around separators + $css = preg_replace('/ ([;,:\{\}])/', '$1', $css); + $css = preg_replace('/([;,:\{\}]) /', '$1', $css); + + $css = str_replace('url (', 'url(', $css); + + return $css; +} \ No newline at end of file