--- includes/_common.inc.original	2008-07-10 19:16:53.000000000 +0200
+++ includes/common.inc	2008-07-10 19:33:56.000000000 +0200
@@ -1463,29 +1463,51 @@ function drupal_get_css($css = NULL) {
   if (!isset($css)) {
     $css = drupal_add_css();
   }
+  $no_module_preprocess = '';
+  $no_theme_preprocess = '';
 
   $preprocess_css = variable_get('preprocess_css', FALSE);
   $directory = file_directory_path();
   $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
 
+  $group_css_files = variable_get('group_css_files', FALSE);
+  $group_css_limit = 30;
+
   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.
+    if ($group_css_files) {
+      $no_module_preprocess_media = array();
+      $no_theme_preprocess_media = array();
+      $output_media = array();
+    }
     foreach ($types as $type => $files) {
       foreach ($types[$type] as $file => $preprocess) {
         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 .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
+            if ($group_css_files) {
+              $no_module_preprocess_media[] = '@import "'. base_path() . $file .'";';
+            } else {
+              $no_module_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\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.
           else if (!$preprocess && $type == 'theme') {
-            $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
+            if ($group_css_files) {
+              $no_theme_preprocess_media[] = '@import "'. base_path() . $file .'";';
+            } else {
+              $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
+            }
           }
           else {
-            $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
+              if ($group_css_files) {
+                $output_media[] = '@import "'. base_path() . $file .'";';
+              } else {
+                $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $file .'";</style>' ."\n";
+              }
           }
         }
       }
@@ -1494,7 +1516,77 @@ function drupal_get_css($css = NULL) {
     if ($is_writable && $preprocess_css) {
       $filename = md5(serialize($types)) .'.css';
       $preprocess_file = drupal_build_css_cache($types, $filename);
-      $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'";</style>'. "\n";
+      if ($group_css_files) {
+        $output_media[] = '@import "'. base_path() . $preprocess_file .'";';
+      } else {
+        $output .= '<style type="text/css" media="'. $media .'">@import "'. base_path() . $preprocess_file .'";</style>'. "\n";
+      }
+    }
+
+    if ($group_css_files && $no_module_preprocess_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($no_module_preprocess_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $no_module_preprocess .= '<style type="text/css" media="'. $media .'">'. join("\n",$no_module_preprocess_media) .'</style>'. "\n";
+      }
+    }
+
+    if ($group_css_files && $no_theme_preprocess_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($no_theme_preprocess_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $no_theme_preprocess .= '<style type="text/css" media="'. $media .'">'. join("\n",$no_theme_preprocess_media) .'</style>'. "\n";
+      }
+    }
+
+    if ($group_css_files && $output_media) {
+      if ($group_css_limit > 0 ) {
+        $i = 1;
+        $importgroup = '';
+        foreach ($output_media as $importfile) {
+          $importgroup .= $importfile ."\n";
+          if ($i < $group_css_limit) {
+            $i++;
+          } else {
+            $output .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+            $importgroup = '';
+            $i = 1;
+          }
+        }
+        if (!empty($importgroup)) {
+          $output .= '<style type="text/css" media="'. $media .'">'. $importgroup .'</style>'. "\n";
+        }
+      } else {
+        $output .= '<style type="text/css" media="'. $media .'">'. join("\n",$output_media) .'</style>'. "\n";
+      }
     }
   }
 
--- modules/system/_system.module.original	2008-07-10 19:17:15.000000000 +0200
+++ modules/system/system.module	2008-07-10 19:19:10.000000000 +0200
@@ -703,6 +703,13 @@ function system_performance_settings() {
     '#options' => array(t('Disabled'), t('Enabled')),
     '#description' => t("Some Drupal modules include their own CSS files. When these modules are enabled, each module's CSS file adds an additional HTTP request to the page, which can increase the load time of each page. These HTTP requests can also slightly increase server load. It is recommended to only turn this option on when your site is in production, as it can interfere with theme development. This option is disabled if you have not set up your files directory, or if your download method is set to private."),
   );
+  $form['bandwidth_optimizations']['group_css_files'] = array(
+    '#type' => 'radios',
+    '#title' => t('Group CSS files'),
+    '#default_value' => intval(variable_get('group_css_files', FALSE)),
+    '#options' => array(t('Disabled'), t('Enabled')),
+    '#description' => '<p>'. t('Internet Explorer 6 and 7 ignore CSS stylesheets after 30 &lt;style&gt; or &lt;link&gt; tags (see <a href="@url_kb" title="@title_kb" rel=\"nofollow\">Microsoft Knowledge Base 262161</a> and <a href="@url_issue" title="@title_issue">issue 228818</a> on drupal.org). By enabling this option up to 30 CSS files get grouped into single &lt;style&gt; tags per media type. It is recommended to enable this option in sites using more than 30 CSS stylesheets and if download method is set to <em>private</em>. <em>Group CSS files</em> is not needed and enabling this option will not have any effect if <em>Optimize CSS files</em> is enabled. <strong><em>But be aware that enabling this option will prevent stylesheets to get saved when "saving as a page" feature is used (see <a href="@url_issue2" title="@title_issue2">issue 145218</a> on drupal.org).</em></strong>', array('@url_kb' => 'http://support.microsoft.com/kb/262161/en-us', '@title_kb' => 'All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer', '@url_issue' => 'http://drupal.org/node/228818', '@title_issue' => 'IE: Stylesheets ignored after 30 link/style tags', '@url_issue2' => 'http://drupal.org/node/145218', '@title_issue2' => 'Use href instead of @import for CSS')) .'</p>',
+  );
 
   $form['#submit']['system_settings_form_submit'] = array();
   $form['#submit']['drupal_clear_css_cache'] = array();
