### Eclipse Workspace Patch 1.0
#P drupal-cvs
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.693
diff -u -r1.693 common.inc
--- includes/common.inc	2 Oct 2007 16:19:22 -0000	1.693
+++ includes/common.inc	3 Oct 2007 14:09:54 -0000
@@ -1674,6 +1674,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() {
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	3 Oct 2007 14:09:55 -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.24
diff -u -r1.24 color.module
--- modules/color/color.module	6 Aug 2007 12:54:39 -0000	1.24
+++ modules/color/color.module	3 Oct 2007 14:09:55 -0000
@@ -48,10 +48,21 @@
 function _color_page_alter(&$vars) {
   global $theme_key;
 
-  // Override stylesheet
-  $path = variable_get('color_'. $theme_key .'_stylesheet', NULL);
-  if ($path) {
-    $vars['css']['all']['theme'][$path] = TRUE;
+  // Override stylesheets
+  $files = variable_get('color_'. $theme_key .'_stylesheets', array());
+  if (!empty($files)) {
+    // Look into vars and add css files only that needs rewritten
+    foreach ($vars['css']['all']['theme'] as $theme_file => $theme_preprocess) {
+      $theme_base = basename($theme_file);
+      // Look if we have matching vars files in the color rewrited files array 
+      foreach ($files as $color_file) {
+        $color_base = basename($color_file);
+        if ($theme_base == $color_base) {
+          // Add rewritten stylesheet for color overwrite
+          $vars['css']['all']['theme'][$color_file] = TRUE;
+        }
+      }
+    }
     $vars['styles'] = drupal_get_css($vars['css']);
   }
 
@@ -229,7 +240,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');
@@ -246,12 +257,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
   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
@@ -266,20 +275,28 @@
   // 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 = _color_rewrite_load_stylesheet($paths['source'] . $file);
+      $style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style);
+      $base = basename($file);
+      $css[] = $paths['target'] . $base;
+      _color_save_stylesheet($paths['target'] . $base, $style, $paths);
+    }
+  }
 
   // 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;
@@ -292,12 +309,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,21 +355,62 @@
     $output = str_replace($before, $after, $output);
   }
 
-  // Write new stylesheet
-  $file = fopen($paths['stylesheet'], 'w+');
-  fwrite($file, $output);
-  fclose($file);
-  $paths['files'][] = $paths['stylesheet'];
+  return $output;
+}
 
-  // Set standard file permissions for webserver-generated files
-  @chmod($paths['stylesheet'], 0664);
+/**
+ * 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. This function keeps all inline comments intact.
+ *
+ * @param $file
+ *   Name of the stylesheet to be processed.
+ * @return
+ *   Contents of the stylesheet including the imported stylesheets.
+ */
+function _color_rewrite_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\()?["\']?([^"\')]+)["\']?\)?;/', '__color_rewrite_load_stylesheet', $contents);
+  // Remove multiple charset declarations for standards compliance (and fixing Safari problems)
+  $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
+
+  // Change back.
+  chdir($cwd);
+  return $contents;
 }
 
 /**
- * Helper function for _color_rewrite_stylesheet.
+ * Helper function for _color_rewrite_load_stylesheet().
  */
-function _color_import_stylesheet($matches) {
-  return preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. dirname($matches[1]) .'/', file_get_contents($matches[1]));
+function __color_rewrite_load_stylesheet($matches) {
+  $filename = $matches[1];
+  // Load the imported stylesheet and replace @import commands in there as
+  // well.
+  $file = _color_rewrite_load_stylesheet($filename);
+  // Alter all url() paths.
+  return preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. dirname($filename) .'/', $file);
+}
+
+/**
+ * Save the rewritten stylesheet to disk.
+ */
+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);
 }
 
 /**
