Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.844
diff -u -r1.844 common.inc
--- includes/common.inc	10 Jan 2009 11:50:11 -0000	1.844
+++ includes/common.inc	11 Jan 2009 04:04:22 -0000
@@ -1956,44 +1956,48 @@
 }
 
 /**
- * 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'.
  *     - 'preprocess':
- *       Allow this CSS file to be aggregated and compressed if the Optimize
- *       CSS feature has been turned on under the performance section. Defaults
- *       to TRUE.
+ *       Allow the CSS to be aggregated and compressed if the Optimize CSS
+ *       feature has been turned on under the performance section. Defaults to
+ *       TRUE.
  *
  *       What does this actually mean?
  *       CSS preprocessing is the process of aggregating a bunch of separate CSS
  *       files into one file that is then compressed by removing all extraneous
- *       white space.
+ *       white space. Note that inline stylesheets will not be aggregated into
+ *       this single file.
  *
  *       The reason for merging the CSS files is outlined quite thoroughly here:
  *       http://www.die.net/musings/page_load_time/
@@ -2011,9 +2015,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;
 
@@ -2024,7 +2028,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)) {
@@ -2037,22 +2041,25 @@
     $options += array(
       'type' => 'module',
       'media' => 'all',
-      'preprocess' => TRUE
+      'preprocess' => TRUE,
     );
     $media = $options['media'];
     $type = $options['type'];
 
     // 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'];
+        }
       }
     }
   }
@@ -2089,6 +2096,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();
@@ -2103,7 +2111,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 => $information) {
       if ($type == 'module') {
         // Setup theme overrides for module styles.
         $theme_styles = array();
@@ -2111,29 +2119,33 @@
           $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') {
+          $no_inline_preprocess .= drupal_load_stylesheet_content($data, $preprocess);
+        }
         // 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";
             }
           }
         }
@@ -2146,8 +2158,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;
 }
 
 /**
@@ -2170,15 +2184,18 @@
 
   if (!file_exists($csspath . '/' . $filename)) {
     // Build aggregate CSS file.
-    foreach ($types as $type) {
-      foreach ($type as $file => $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);
+    foreach ($types as $type => $css) {
+      // Don't aggregate inline stylesheets.
+      if ($type != 'inline') {
+        foreach ($css as $stylesheet => $cache) {
+          if ($cache) {
+            $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);
+          }
         }
       }
     }
@@ -2252,20 +2269,8 @@
     $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);
-    }
+    // Process the stylesheet.
+    $contents = drupal_load_stylesheet_content($contents, $_optimize);
 
     // Change back directory.
     chdir($cwd);
@@ -2275,6 +2280,34 @@
 }
 
 /**
+ * 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;
+}
+
+/**
  * Loads stylesheets recursively and returns contents with corrected paths.
  *
  * This function is used for recursive loading of stylesheets and
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.20
diff -u -r1.20 common.test
--- modules/simpletest/tests/common.test	8 Jan 2009 19:09:49 -0000	1.20
+++ modules/simpletest/tests/common.test	11 Jan 2009 04:04:24 -0000
@@ -197,6 +197,25 @@
     drupal_add_css($css);
     $this->assertTrue(strpos(drupal_get_css(), $css) > 0, t('Rendered CSS includes the added stylesheet.'));
   }
+
+  /**
+   * Tests rendering inline stylesheets with preprocessing on.
+   */
+  function testRenderInlinePreprocess() {
+    $css = 'body { padding: 0px; }';
+    $css_preprocessed = drupal_load_stylesheet_content($css, TRUE);
+    drupal_add_css($css, 'inline');
+    $this->assertTrue(strpos(drupal_get_css(), $css_preprocessed) > 0, t('Rendering preprocessed inline CSS adds it to the page.'));
+  }
+
+  /**
+   * Tests rendering the stylesheets with preprocessing off.
+   */
+  function testRenderInlineNoPreprocess() {
+    $css = 'body { padding: 0px; }';
+    drupal_add_css($css, array('type' => 'inline', 'preprocess' => FALSE));
+    $this->assertTrue(strpos(drupal_get_css(), $css) > 0, t('Rendering non-preprocessed inline CSS adds it to the page.'));
+  }
 }
 
 /**
