Skip to content

Commit

Permalink
MDL-21252 home-grown CSS minimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 5, 2010
1 parent df06c1c commit e10f304
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions theme/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@
$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;
Expand Down Expand Up @@ -129,9 +131,7 @@ function send_ie_css($themename, $rev) {
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;
Expand All @@ -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;
}

0 comments on commit e10f304

Please sign in to comment.