### Eclipse Workspace Patch 1.0
#P drupal-cvs
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.715
diff -u -r1.715 common.inc
--- includes/common.inc	16 Nov 2007 15:35:24 -0000	1.715
+++ includes/common.inc	19 Nov 2007 20:11:56 -0000
@@ -1712,34 +1712,17 @@
     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, TRUE);
           // 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);
         }
       }
     }
 
-    // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
-    // @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);
   }
@@ -1747,6 +1730,88 @@
 }
 
 /**
+ * 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.
+ * @param $optimize
+ *   Defines if CSS contents should be compressed or not.
+ * @return
+ *   Contents of the stylesheet including the imported stylesheets.
+ */
+function drupal_load_stylesheet($file, $optimize = NULL) {
+  static $_optimize;
+  // Store optimization parameter.
+  if (isset($optimize)) {
+    $_optimize = $optimize;
+  }
+
+  $contents = '';
+  if (file_exists($file)) {
+    // Load the local CSS stylesheet.
+    $contents = file_get_contents($file);
+
+    // Change to the current stylesheet's directory.
+    $cwd = getcwd();
+    chdir(dirname($file));
+
+    // Replace @import commands with the actual stylesheet content.
+    $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);
+
+    if ($_optimize) {
+      // 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 directory.
+    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() {
