### Eclipse Workspace Patch 1.0
#P drupal-5
Index: modules/color/color.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/color/color.module,v
retrieving revision 1.13.2.2
diff -u -r1.13.2.2 color.module
--- modules/color/color.module	9 Jul 2007 05:02:32 -0000	1.13.2.2
+++ modules/color/color.module	1 Oct 2007 17:10:11 -0000
@@ -267,8 +267,9 @@
  * Rewrite the stylesheet to match the colors in the palette.
  */
 function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette) {
-  // Load stylesheet
-  $style = file_get_contents($paths['source'] .'style.css');
+  // Load stylesheet and replace @import commands with the contents of the
+  // imported stylesheet.
+  $style = drupal_load_stylesheet($paths['source'] .'style.css');
 
   // Prepare color conversion table
   $conversion = $palette;
@@ -281,12 +282,6 @@
   // Split off the "Don't touch" section of the stylesheet.
   list($style, $fixed) = explode("Color Module: Don't touch", $style);
 
-  // Look for @import commands and insert the referenced stylesheets.
-  $cwd = getcwd();
-  chdir(drupal_get_path('theme', $theme));
-  $style = preg_replace_callback('/@import\s*["\']([^"\']+)["\'];/', '_color_import_stylesheet', $style);
-  chdir($cwd);
-
   // Find all colors in the stylesheet and the chunks in between.
   $style = preg_split('/(#[0-9a-f]{6}|#[0-9a-f]{3})/i', $style, -1, PREG_SPLIT_DELIM_CAPTURE);
   $is_color = false;
@@ -344,13 +339,6 @@
 }
 
 /**
- * Helper function for _color_rewrite_stylesheet.
- */
-function _color_import_stylesheet($matches) {
-  return preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. dirname($matches[1]) .'/', file_get_contents($matches[1]));
-}
-
-/**
  * Render images that match a given palette.
  */
 function _color_render_images($theme, &$info, &$paths, $palette) {
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.611.2.10
diff -u -r1.611.2.10 common.inc
--- includes/common.inc	12 Sep 2007 07:29:57 -0000	1.611.2.10
+++ includes/common.inc	1 Oct 2007 17:10:10 -0000
@@ -1510,32 +1510,16 @@
     foreach ($types as $type) {
       foreach ($type as $file => $cache) {
         if ($cache) {
-          $contents = file_get_contents($file);
-          // Remove multiple charset declarations for standards compliance (and fixing Safari problems)
-          $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
+          $contents = drupal_load_stylesheet($file);
           // Return the path to where this CSS file originated from, stripping off the name of the file at the end of the path.
-          $path = base_path() . substr($file, 0, strrpos($file, '/')) .'/';
-          // Wraps all @import arguments in url().
-          $contents = preg_replace('/@import\s+(?!url)[\'"]?(\S*)\b[\'"]?/i', '@import url("\1")', $contents);
-          // Fix all paths within this CSS file, ignoring absolute paths.
-          $data .= preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. $path . '\2', $contents);
+          $base = base_path() . dirname($file) .'/';
+          _drupal_build_css_cache(NULL, $base);
+          // Prefix all paths within this CSS file, ignoring absolute paths.
+          $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_cache', $contents);
         }
       }
     }
 
-    // @import rules must proceed any other style, so we move those to the top.
-    $regexp = '/@import[^;]+;/i';
-    preg_match_all($regexp, $data, $matches);
-    $data = preg_replace($regexp, '', $data);
-    $data = implode('', $matches[0]) . $data;
-
-    // Perform some safe CSS optimizations.
-    $data = preg_replace('<
-      \s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
-      /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
-      [\n\r]                        # Remove line breaks.
-      >x', '\1', $data);
-
     // Create the CSS file.
     file_save_data($data, $csspath .'/'. $filename, FILE_EXISTS_REPLACE);
   }
@@ -1543,6 +1527,74 @@
 }
 
 /**
+ * Helper function for drupal_build_css_cache().
+ */
+function _drupal_build_css_cache($matches, $base = NULL) {
+  static $_base;
+  // Store base path.
+  if (isset($base)) {
+    $_base = $base;
+  }
+
+  // Prefix with base and remove '../' segments where possible.
+  $path = $_base . $matches[1];
+  $last = '';
+  while ($path != $last) {
+    $last = $path;
+    $path = preg_replace('`(^|/)(?!../)([^/]+)/../`', '$1', $path);
+  }
+  return 'url('. $path .')';
+}
+
+/**
+ * Loads the stylesheet and resolves all @import commands.
+ *
+ * Loads a stylesheet and replaces @import commands with the contents of the
+ * imported file. Use this instead of file_get_contents if you processing
+ * stylesheets.
+ *
+ * @param $file
+ *   Name of the stylesheet to be processed.
+ * @return
+ *   Contents of the stylesheet including the imported stylesheets.
+ */
+function drupal_load_stylesheet($file) {
+  // Load the CSS.
+  $contents = file_get_contents($file);
+
+  // Change to the stylesheet's directory.
+  $cwd = getcwd();
+  chdir(dirname($file));
+
+  // Replace @import commands with the actual stylesheet.
+  $contents = preg_replace_callback('/@import\s*(?:url\()?["\']?([^"\')]+)["\']?\)?;/', '_drupal_load_stylesheet', $contents);
+  // Remove multiple charset declarations for standards compliance (and fixing Safari problems)
+  $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
+  // Perform some safe CSS optimizations.
+  $contents = preg_replace('<
+    \s*([@{}:;,]|\)\s|\s\()\s* |  # Remove whitespace around separators, but keep space around parentheses.
+    /\*([^*\\\\]|\*(?!/))+\*/ |   # Remove comments that are not CSS hacks.
+    [\n\r]                        # Remove line breaks.
+    >x', '\1', $contents);
+
+  // Change back.
+  chdir($cwd);
+  return $contents;
+}
+
+/**
+ * Helper function for drupal_load_stylesheet().
+ */
+function _drupal_load_stylesheet($matches) {
+  $filename = $matches[1];
+  // Load the imported stylesheet and replace @import commands in there as
+  // well.
+  $file = drupal_load_stylesheet($filename);
+  // Alter all url() paths.
+  return preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. dirname($filename) .'/', $file);
+}
+
+/**
  * Delete all cached CSS files.
  */
 function drupal_clear_css_cache() {
