Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.1113
diff -u -p -r1.1113 common.inc
--- includes/common.inc	18 Feb 2010 06:28:08 -0000	1.1113
+++ includes/common.inc	20 Feb 2010 08:58:18 -0000
@@ -2954,16 +2954,70 @@ function drupal_load_stylesheet_content(
 
   if ($optimize) {
     // Perform some safe CSS optimizations.
-    $contents = preg_replace('{
-      (?<=\\\\\*/)([^/\*]+/\*)([^\*/]+\*/)  # Add a backslash also at the end ie-mac hack comment, so the next pass will not touch it.
-                                            # The added backslash does not affect the effectiveness of the hack.
-      }x', '\1\\\\\2', $contents);
-    $contents = preg_replace('<
-      \s*([@{}:;,]|\)\s|\s\()\s* |          # Remove whitespace around separators, but keep space around parentheses.
-      /\*[^*\\\\]*\*+([^/*][^*]*\*+)*/ |    # Remove comments that are not CSS hacks.
-      >x', '\1', $contents);
+    $contents = str_replace("\r", '', $contents);  // Strip any and all carriage returns.
+    // Match and process strings, comments and everything else, one chunk at a time.
+    // To understand this regex, read: "Mastering Regular Expressions 3rd Edition" chapter 6.
+    $contents = preg_replace_callback('%
+      # One-regex-to-rule-them-all! - version: 20100220_0100
+      # Group 1: Match a double quoted string.
+      ("[^"\\\\]*+(?:\\\\.[^"\\\\]*+)*+") |  # or...
+      # Group 2: Match a single quoted string.
+      (\'[^\'\\\\]*+(?:\\\\.[^\'\\\\]*+)*+\') |  # or...
+      # Group 3: Match a regular non-MacIE5-hack comment.
+      (/\*[^\\\\*]*+\*++(?:[^\\\\*/][^\\\\*]*+\*++)*+/) |  # or...
+      # Group 4: Match a MacIE5-type1 comment.
+      (/\*(?:[^*\\\\]*+\**+(?!/))*+\\\\[^*]*+\*++(?:[^*/][^*]*+\*++)*+/(?<!\\\\\*/)) |  # or...
+      # Group 5: Match a MacIE5-type2 comment.
+      (/\*[^*]*\*+(?:[^/*][^*]*\*+)*/(?<=\\\\\*/))  # folllowed by...
+      # Group 6: Match everything up to final closing regular comment
+      ([^/]*+(?:(?!\*)/[^/]*+)*?)
+      # Group 7: Match final closing regular comment
+      (/\*[^/]++(?:(?<!\*)/(?!\*)[^/]*+)*+/(?<=(?<!\\\\)\*/)) |  # or...
+      # Group 8: Match regular non-string, non-comment text.
+      ([^"\'/]*+(?:(?!/\*)/[^"\'/]*+)*+)
+      %Ssx', '_minify_css', $contents); // Do it!
+    $contents = preg_replace('/^\s++/', '', $contents);             // Strip leading whitespace.
+    $contents = preg_replace('/[ \t]*+\n\s*+/S', "\n", $contents);  // Consolidate multi-lines space.
+    $contents = preg_replace('/(?<!\s)\s*+$/S', "\n", $contents);   // Ensure file ends in newline.
   }
 
+/**
+ * preg_replace_callback function to consolidate CSS code
+ *
+ * This is the callback function for the preg_replace_callback() used in
+ * drupal_load_stylesheet_content(). Support for comment hacks is implemented here.
+ */
+function _minify_css($matches) {
+  if ($matches[1]) {      // Group 1: Double quoted string.
+    return $matches[1];   // Return the string unmodified.
+  }
+  elseif ($matches[2]) {  // Group 2: Single quoted string.
+    return $matches[2];   // Return the string unmodified.
+  }
+  elseif ($matches[3]) {  // Group 3: Regular non-MacIE5-hack comment.
+    return "\n";            // Return single space.
+  }
+  elseif ($matches[4]) {  // Group 4: MacIE5-hack-type-1 comment.
+    return "\n/*\\T1*/\n";  // Return minimal MacIE5-hack-type-1 comment.
+  }
+  elseif ($matches[5]) {  // Group 5,6,7: MacIE5-hack-type-2 comment
+    $matches[6] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[6]);  // Clean pre-punctuation.
+    $matches[6] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[6]); // Clean post-punctuation.
+    $matches[6] = preg_replace('/;?\}/S', "}\n", $matches[6]);            // Add a touch of formatting.
+    return "\n/*T2\\*/" . $matches[6] . "\n/*T2E*/\n"; // Minify and reassemble composite type2 comment.
+  }
+  elseif (isset($matches[8])) { // Group 8: Non-string, non-comment. Safe to clean whitespace here.
+    $matches[8] = preg_replace('/^\s++/', '', $matches[8]);               // Strip all leading whitespace.
+    $matches[8] = preg_replace('/\s++$/', '', $matches[8]);               // Strip all trailing whitespace.
+    $matches[8] = preg_replace('/\s{2,}+/', ' ', $matches[8]);            // Consolidate multiple whitespace.
+    $matches[8] = preg_replace('/\s++([+>{};,)])/S', '$1', $matches[8]);  // Clean pre-punctuation.
+    $matches[8] = preg_replace('/([+>{}:;,(])\s++/S', '$1', $matches[8]); // Clean post-punctuation.
+    $matches[8] = preg_replace('/;?\}/S', "}\n", $matches[8]);            // Add a touch of formatting.
+    return $matches[8];
+  }
+  return $matches[0] . "\n/* ERROR! Unexpected _proccess_css_minify() parameter */\n"; // never get here
+}
+
   // Replaces @import commands with the actual stylesheet content.
   // This happens recursively but omits external files.
   $contents = preg_replace_callback('/@import\s*(?:url\()?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\)?;/', '_drupal_load_stylesheet', $contents);
