### Eclipse Workspace Patch 1.0
#P drupal-cvs
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.700
diff -u -r1.700 common.inc
--- includes/common.inc	12 Oct 2007 14:10:17 -0000	1.700
+++ includes/common.inc	12 Oct 2007 23:57:38 -0000
@@ -1697,34 +1697,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);
   }
@@ -1732,6 +1715,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() {
Index: themes/garland/color/color.inc
===================================================================
RCS file: /cvs/drupal/drupal/themes/garland/color/color.inc,v
retrieving revision 1.3
diff -u -r1.3 color.inc
--- themes/garland/color/color.inc	2 Oct 2007 16:31:50 -0000	1.3
+++ themes/garland/color/color.inc	12 Oct 2007 23:57:39 -0000
@@ -30,6 +30,11 @@
     'images/menu-leaf.gif',
   ),
 
+  // CSS files (excluding @import) to rewrite with new color scheme
+  'css' => array(
+    'style.css',
+  ),
+
   // Coordinates of gradient (x, y, width, height)
   'gradient' => array(0, 37, 760, 121),
 
Index: modules/color/color.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/color/color.module,v
retrieving revision 1.27
diff -u -r1.27 color.module
--- modules/color/color.module	11 Oct 2007 10:51:06 -0000	1.27
+++ modules/color/color.module	12 Oct 2007 23:57:39 -0000
@@ -58,12 +58,43 @@
  * Callback for the theme to alter the resources used.
  */
 function _color_page_alter(&$vars) {
-  global $theme_key;
+  global $language, $theme_key;
 
-  // Override stylesheet
-  $path = variable_get('color_'. $theme_key .'_stylesheet', NULL);
-  if ($path) {
-    $vars['css']['all']['theme'][$path] = TRUE;
+  // Override stylesheets
+  $color_paths = variable_get('color_'. $theme_key .'_stylesheets', array());
+  if (!empty($color_paths)) {
+    // Loop over themes CSS files and try to rebuild CSS array with rewritten
+    // stylesheets. Additional keep the orginal order for CSS cascading intact.
+    $new_theme_css = array();
+
+    foreach ($vars['css']['all']['theme'] as $old_path => $old_preprocess) {
+      // Add the non-colored stylesheet as we cannot use if/else in following loop.
+      $new_theme_css[$old_path] = $old_preprocess;
+
+      // Loop over for matching paths in the color.module rewritten paths array. 
+      foreach ($color_paths as $color_path) {
+        // Color module currently requires unique file names to be used,
+        // what allows to compare different filepaths.
+        if (basename($old_path) == basename($color_path)) {
+          // Pull out the non-colored and replace with rewritten stylesheet.
+          unset($new_theme_css[$old_path]);
+          $new_theme_css[$color_path] = $old_preprocess;
+
+          // If the current language is RTL and the CSS file had an RTL variant,
+          // pull out the non-colored and replace with rewritten RTL stylesheet.
+          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
+            $rtl_old_path = str_replace('.css', '-rtl.css', $old_path);
+            $rtl_color_path = str_replace('.css', '-rtl.css', $color_path);
+            if (file_exists($rtl_color_path)) {
+              unset($new_theme_css[$rtl_old_path]);
+              $new_theme_css[$rtl_color_path] = $old_preprocess;
+            }
+          }
+          break;
+        }
+      }
+    }
+    $vars['css']['all']['theme'] = $new_theme_css;
     $vars['styles'] = drupal_get_css($vars['css']);
   }
 
@@ -241,7 +272,7 @@
   if (implode(',', color_get_palette($theme, true)) == implode(',', $palette)
     || $form_state['values']['op'] == t('Reset to defaults')) {
     variable_del('color_'. $theme .'_palette');
-    variable_del('color_'. $theme .'_stylesheet');
+    variable_del('color_'. $theme .'_stylesheets');
     variable_del('color_'. $theme .'_logo');
     variable_del('color_'. $theme .'_files');
     variable_del('color_'. $theme .'_screenshot');
@@ -258,12 +289,10 @@
   $paths['target'] = $paths['target'] .'/';
   $paths['id'] = $id;
   $paths['source'] = drupal_get_path('theme', $theme) .'/';
-  $paths['stylesheet'] = $paths['target'] .'style.css';
   $paths['files'] = $paths['map'] = array();
 
-  // Save palette and stylesheet location
+  // Save palette and logo location
   variable_set('color_'. $theme .'_palette', $palette);
-  variable_set('color_'. $theme .'_stylesheet', $paths['stylesheet']);
   variable_set('color_'. $theme .'_logo', $paths['target'] .'logo.png');
 
   // Copy over neutral images
@@ -278,20 +307,51 @@
   // Render new images
   _color_render_images($theme, $info, $paths, $palette);
 
-  // Rewrite stylesheet
-  _color_rewrite_stylesheet($theme, $info, $paths, $palette);
+  // Rewrite theme stylesheets
+  $css = array();
+  foreach ($info['css'] as $file) {
+    if (file_exists($paths['source'] . $file)) {
+      $style = drupal_load_stylesheet($paths['source'] . $file, FALSE);
+      // Return the path to where this CSS file originated from, stripping
+      // off the name of the file at the end of the path.
+      $base = base_path() . dirname($paths['source'] . $file) .'/';
+      _drupal_build_css_cache(NULL, $base);
+      // Prefix all paths within this CSS file, ignoring absolute paths.
+      $style = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_cache', $style);
+      // Rewrite stylesheet
+      $style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style);
+      $base_file = basename($file);
+      $css[] = $paths['target'] . $base_file;
+      _color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
+
+      $rtl_file = str_replace('.css', '-rtl.css', $file);
+      if (file_exists($paths['source'] . $rtl_file)) {
+        $style = drupal_load_stylesheet($paths['source'] . $rtl_file);
+        // Return the path to where this CSS file originated from, stripping
+        // off the name of the file at the end of the path.
+        $base = base_path() . dirname($paths['source'] . $file) .'/';
+        _drupal_build_css_cache(NULL, $base);
+        // Prefix all paths within this CSS file, ignoring absolute paths.
+        $style = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_cache', $style);
+        // Rewrite stylesheet
+        $style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style);
+        $base_file = basename($rtl_file);
+        $css[] = $paths['target'] . $base_file;
+        _color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
+      }
+    }
+  }
 
-  // Maintain list of files
+  // Maintain list of files.
+  variable_set('color_'. $theme .'_stylesheets', $css);
   variable_set('color_'. $theme .'_files', $paths['files']);
 }
 
 /**
  * Rewrite the stylesheet to match the colors in the palette.
  */
-function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette) {
-  // Load stylesheet
+function _color_rewrite_stylesheet($theme, &$info, &$paths, $palette, $style) {
   $themes = list_themes();
-  $style = file_get_contents($themes[$theme]->stylesheets['all']['style.css']);
 
   // Prepare color conversion table
   $conversion = $palette;
@@ -304,12 +364,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;
@@ -353,24 +407,24 @@
 
   // Replace paths to images
   foreach ($paths['map'] as $before => $after) {
+    $before = base_path() . $paths['source'] . $before;
     $output = str_replace($before, $after, $output);
   }
 
-  // Write new stylesheet
-  $file = fopen($paths['stylesheet'], 'w+');
-  fwrite($file, $output);
-  fclose($file);
-  $paths['files'][] = $paths['stylesheet'];
-
-  // Set standard file permissions for webserver-generated files
-  @chmod($paths['stylesheet'], 0664);
+  return $output;
 }
 
 /**
- * Helper function for _color_rewrite_stylesheet.
+ * Save the rewritten stylesheet to disk.
  */
-function _color_import_stylesheet($matches) {
-  return preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. dirname($matches[1]) .'/', file_get_contents($matches[1]));
+function _color_save_stylesheet($file, $style, &$paths) {
+
+  // Write new stylesheet
+  file_save_data($style, $file, FILE_EXISTS_REPLACE);
+  $paths['files'][] = $file;
+
+  // Set standard file permissions for webserver-generated files
+  @chmod($file, 0664);
 }
 
 /**
