Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.811
diff -u -r1.811 common.inc
--- includes/common.inc	26 Oct 2008 18:06:38 -0000	1.811
+++ includes/common.inc	27 Oct 2008 10:39:37 -0000
@@ -1712,32 +1712,35 @@
 }
 
 /**
- * Adds a CSS file to the stylesheet queue.
+ * Adds a cascading stylesheet to the stylesheet queue.
  *
- * @param $path
- *   (optional) The path to the CSS file relative to the base_path(), e.g.,
- *   /modules/devel/devel.css.
- *
- *   Modules should always prefix the names of their CSS files with the module
- *   name, for example: system-menus.css rather than simply menus.css. Themes
- *   can override module-supplied CSS files based on their filenames, and this
- *   prefixing helps prevent confusing name collisions for theme developers.
- *   See drupal_get_css where the overrides are performed.
- *
- *   If the direction of the current language is right-to-left (Hebrew,
- *   Arabic, etc.), the function will also look for an RTL CSS file and append
- *   it to the list. The name of this file should have an '-rtl.css' suffix.
- *   For example a CSS file called 'name.css' will have a 'name-rtl.css'
- *   file added to the list, if exists in the same directory. This CSS file
- *   should contain overrides for properties which should be reversed or
- *   otherwise different in a right-to-left display.
+ * @param $data
+ *   (optional) If given, the value depends on the type passed through to the
+ *   $options parameter:
+ *   - 'module' or 'theme': The path to the CSS file relative to the base_path(),
+ *     e.g., /modules/devel/devel.css.
+ *
+ *     Modules should always prefix the names of their CSS files with the module
+ *     name, for example: system-menus.css rather than simply menus.css. Themes
+ *     can override module-supplied CSS files based on their filenames, and this
+ *     prefixing helps prevent confusing name collisions for theme developers.
+ *     See drupal_get_css where the overrides are performed.
+ *
+ *     If the direction of the current language is right-to-left (Hebrew,
+ *     Arabic, etc.), the function will also look for an RTL CSS file and append
+ *     it to the list. The name of this file should have an '-rtl.css' suffix.
+ *     For example a CSS file called 'name.css' will have a 'name-rtl.css'
+ *     file added to the list, if exists in the same directory. This CSS file
+ *     should contain overrides for properties which should be reversed or
+ *     otherwise different in a right-to-left display.
+ *   - 'inline': The CSS that should be placed in the given scope.
  * @param $options
  *   (optional) A string defining the type of CSS that is being added in the
  *   $path parameter ('module' or 'theme'), or an associative array of
  *   additional options, with the following keys:
  *     - 'type'
- *       The type of stylesheet that is being added. Types are: module or
- *       theme. Defaults to 'module'.
+ *       The type of stylesheet that is being added. Types are: 'module',
+ *       'theme', or 'inline'. Defaults to 'module'.
  *     - 'media'
  *       The media type for the stylesheet, e.g., all, print, screen. Defaults
  *       to 'all'.
@@ -1767,9 +1770,9 @@
  * @param $reset
  *   (optional) Resets the currently loaded cascading stylesheets.
  * @return
- *   An array of CSS files.
+ *   An array of queued CSS.
  */
-function drupal_add_css($path = NULL, $options = NULL, $reset = FALSE) {
+function drupal_add_css($data = NULL, $options = NULL, $reset = FALSE) {
   static $css = array();
   global $language;
 
@@ -1780,7 +1783,7 @@
 
   // Create an array of CSS files for each media type first, since each type needs to be served
   // to the browser differently.
-  if (isset($path)) {
+  if (isset($data)) {
     // Construct the options, taking the defaults into consideration.
     if (isset($options)) {
       if (!is_array($options)) {
@@ -1800,15 +1803,18 @@
 
     // This check is necessary to ensure proper cascading of styles and is faster than an asort().
     if (!isset($css[$media])) {
-      $css[$media] = array('module' => array(), 'theme' => array());
+      $css[$media] = array('module' => array(), 'theme' => array(), 'inline' => array());
     }
-    $css[$media][$type][$path] = $options['preprocess'];
 
-    // If the current language is RTL, add the CSS file with RTL overrides.
-    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
-      $rtl_path = str_replace('.css', '-rtl.css', $path);
-      if (file_exists($rtl_path)) {
-        $css[$media][$type][$rtl_path] = $options['preprocess'];
+    $css[$media][$type][$data] = $options['preprocess'];
+
+    if ($type != 'inline') {
+      // If the current language is RTL, add the CSS file with RTL overrides.
+      if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
+        $rtl_path = str_replace('.css', '-rtl.css', $data);
+        if (file_exists($rtl_path)) {
+          $css[$media][$type][$rtl_path] = $options['preprocess'];
+        }
       }
     }
   }
@@ -1845,6 +1851,7 @@
   }
   $no_module_preprocess = '';
   $no_theme_preprocess = '';
+  $no_inline_preprocess = '';
 
   $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
   $directory = file_directory_path();
@@ -1859,7 +1866,7 @@
   foreach ($css as $media => $types) {
     // If CSS preprocessing is off, we still need to output the styles.
     // Additionally, go through any remaining styles if CSS preprocessing is on and output the non-cached ones.
-    foreach ($types as $type => $files) {
+    foreach ($types as $type => $data) {
       if ($type == 'module') {
         // Setup theme overrides for module styles.
         $theme_styles = array();
@@ -1867,29 +1874,35 @@
           $theme_styles[] = basename($theme_style);
         }
       }
-      foreach ($types[$type] as $file => $preprocess) {
+      foreach ($types[$type] as $data => $preprocess) {
         // If the theme supplies its own style using the name of the module style, skip its inclusion.
         // This includes any RTL styles associated with its main LTR counterpart.
-        if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
+        if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($data)), $theme_styles)) {
           // Unset the file to prevent its inclusion when CSS aggregation is enabled.
-          unset($types[$type][$file]);
+          unset($types[$type][$data]);
           continue;
         }
+        // Include inline stylesheets
+        if ($type == 'inline') {
+          if (!$preprocess || !($is_writable && $preprocess_css)) {
+            $no_inline_preprocess .= $data;
+          }
+        }
         // Only include the stylesheet if it exists.
-        if (file_exists($file)) {
+        elseif (file_exists($data)) {
           if (!$preprocess || !($is_writable && $preprocess_css)) {
             // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
             // regardless of whether preprocessing is on or off.
             if (!$preprocess && $type == 'module') {
-              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
+              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
             }
             // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
             // regardless of whether preprocessing is on or off.
             elseif (!$preprocess && $type == 'theme') {
-              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
+              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
             }
             else {
-              $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
+              $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
             }
           }
         }
@@ -1902,8 +1915,10 @@
       $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
     }
   }
-
-  return $no_module_preprocess . $output . $no_theme_preprocess;
+  if (!empty($no_inline_preprocess)) {
+    $no_inline_preprocess = '<style type="text/css">' . $no_inline_preprocess . '</style>';
+  }
+  return $no_module_preprocess . $output . $no_theme_preprocess . $no_inline_preprocess;
 }
 
 /**
@@ -1926,15 +1941,20 @@
 
   if (!file_exists($csspath . '/' . $filename)) {
     // Build aggregate CSS file.
-    foreach ($types as $type) {
-      foreach ($type as $file => $cache) {
+    foreach ($types as $type => $css) {
+      foreach ($css as $stylesheet => $cache) {
         if ($cache) {
-          $contents = drupal_load_stylesheet($file, TRUE);
-          // Return the path to where this CSS file originated from.
-          $base = base_path() . dirname($file) . '/';
-          _drupal_build_css_path(NULL, $base);
-          // Prefix all paths within this CSS file, ignoring external and absolute paths.
-          $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents);
+          if ($type == 'inline') {
+            $data .= drupal_load_stylesheet_content($stylesheet, TRUE);
+          }
+          else {
+            $contents = drupal_load_stylesheet($stylesheet, TRUE);
+            // Return the path to where this CSS file originated from.
+            $base = base_path() . dirname($stylesheet) . '/';
+            _drupal_build_css_path(NULL, $base);
+            // Prefix all paths within this CSS file, ignoring external and absolute paths.
+            $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents);
+          }
         }
       }
     }
@@ -2003,30 +2023,39 @@
   if (file_exists($file)) {
     // Load the local CSS stylesheet.
     $contents = file_get_contents($file);
+    
+    // Process the stylesheet.
+    $contents = drupal_load_stylesheet_content($contents, $_optimize);
+  }
 
-    // Change to the current stylesheet's directory.
-    $cwd = getcwd();
-    chdir(dirname($file));
-
-    // 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);
-    // 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);
-    }
+  return $contents;
+}
 
-    // Change back directory.
-    chdir($cwd);
+/**
+ * Processes the content of a stylesheet for optimization.
+ *
+ * @param $contents
+ *   The content of the stylesheet.
+ * @param $optimize
+ *   Defines if CSS contents should be compressed or not.
+ * @return
+ *   Contents of the stylesheet including the imported stylesheets.
+ */
+function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
+  // 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);
+  // 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);
   }
-
   return $contents;
 }
 
