=== modified file 'includes/common.inc' --- includes/common.inc 2006-11-21 19:40:09 +0000 +++ includes/common.inc 2006-11-25 05:41:19 +0000 @@ -1286,19 +1286,27 @@ function drupal_add_link($attributes) { * Adds a CSS file to the stylesheet queue. * * @param $path - * The path to the CSS file relative to the base_path(), e.g., /modules/devel/devel.css. + * (optional) The path to the CSS file relative to the base_path(), e.g., /modules/devel/devel.css. * @param $type - * The type of stylesheet that is being added. Types are: core, module, and theme. + * (optional) The type of stylesheet that is being added. Types are: core, module, and theme. * @param $media * (optional) The media type for the stylesheet, e.g., all, print, screen. + * @param $cache + * (optional) Should this CSS file be cached if cache CSS setting is turned on? * @return * An array of CSS files. */ -function drupal_add_css($path = NULL, $type = 'module', $media = 'all') { - static $css = array('core' => array(), 'module' => array(), 'theme' => array()); +function drupal_add_css($path = NULL, $type = 'module', $media = 'all', $cache = TRUE) { + static $css = array(); - if (!is_null($path)) { - $css[$type][$path] = array('path' => $path, 'media' => $media); + // 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)) { + // This check is necessary to ensure proper cascading of styles and is faster than an asort() + if (!isset($css[$media])) { + $css[$media] = array('core' => array(), 'module' => array(), 'theme' => array()); + } + $css[$media][$type][$path] = $cache; } return $css; @@ -1316,13 +1324,34 @@ function drupal_add_css($path = NULL, $t */ function drupal_get_css($css = NULL) { $output = ''; - if (is_null($css)) { + if (!isset($css)) { $css = drupal_add_css(); } - foreach ($css as $type) { - foreach ($type as $file) { - $output .= '\n"; + $cache_css = variable_get('cache_css', FALSE); + $directory_path = file_directory_path(); + $is_writable = is_writable($directory_path); + + foreach ($css as $media => $types) { + if ($is_writable && $cache_css) { + $filename = md5(serialize($types)) .'.css'; + $path = $directory_path .'/css/'; + + if (!file_exists($path . $filename)) { + $filename = drupal_build_css_cache($types); + } + + $output .= ''. "\n"; + } + + // If CSS caching is off, we still need to ouput the styles + // Additionally, go through any remaining styles if CSS caching is on and output the non-cached ones + foreach ($types as $type) { + foreach ($type as $file => $cache) { + if (!$cache || !$cache_css) { + $output .= '' ."\n"; + } + } } } @@ -1330,6 +1359,72 @@ function drupal_get_css($css = NULL) { } /** + * Aggregate and optimize CSS files, putting them in the files directory. + * + * @param $css + * An array of CSS files to aggregate and compress into one file. + * @return + * The name of the CSS file. + */ +function drupal_build_css_cache($css) { + $data = ''; + $filename = md5(serialize($css)) .'.css'; + + // Create the css/ within the files folder + file_check_directory(file_create_path('css'), FILE_CREATE_DIRECTORY); + + // Build aggregate CSS file + foreach ($css as $type) { + foreach ($type as $file => $cache) { + if ($cache) { + $contents = file_get_contents($file); + // 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, '/')) .'/'; + // Fix all paths within this CSS file, ignoring absolute paths + $contents = preg_replace('/url\(([\'"]?)(?![a-z]+:)/i', 'url(\1'. $path . '\2', $contents); + // Fix any @import that don't use url() and is not absoslute + $data .= preg_replace('/@import\s*([\'"]?)(?![a-z]+:)/i', '@import \1'. $path . '\2', $contents); + } + } + } + + // @import rules must proceed any either 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; + + // If any modules implement hook_compress_css, let them handle compressing + // the css, otherwise just strip out the whitespace, which can be done safely. + if (module_invoke_all('hook_compress_css')) { + foreach (module_implements('compress_css') as $module) { + $function = $module .'_compress_css'; + $function($contents); + } + } + else { + // Perform some safe CSS optimizations + $data = preg_replace('< + \s*([@{}:;\)])\s* | # Remove whitespace around separators. + /\*([^*\\\\]|\*(?!/))+\*/ | # Remove comments that are not CSS hacks + [\n\r] # Remove line breaks + >x', '\1', $data); + } + + // Create the CSS file + file_save_data($data, file_create_path('css/') .'/'. $filename, FILE_EXISTS_REPLACE); + + return $filename; +} + +/** + * Delete all cached CSS files + */ +function drupal_flush_css_cache() { + file_scan_directory(file_create_path('css'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE); +} + +/** * Add a JavaScript file, setting or inline code to the page. * * The behavior of this function depends on the parameters it is called with. === modified file 'modules/color/color.module' --- modules/color/color.module 2006-11-10 19:59:30 +0000 +++ modules/color/color.module 2006-11-25 05:38:13 +0000 @@ -41,7 +41,7 @@ function _color_page_alter(&$vars) { // Override stylesheet $path = variable_get('color_'. $theme_key .'_stylesheet', NULL); if ($path) { - $vars['css']['theme'] = array($path => array('path' => $path, 'media' => 'all')); + $vars['css']['all']['theme'][$path] = TRUE; $vars['styles'] = drupal_get_css($vars['css']); } === modified file 'modules/system/system.module' --- modules/system/system.module 2006-11-24 10:57:20 +0000 +++ modules/system/system.module 2006-11-25 05:38:13 +0000 @@ -675,6 +675,14 @@ function system_page_caching_settings() '#description' => t('On high-traffic sites it can become necessary to enforce a minimum cache lifetime. The minimum cache lifetime is the minimum amount of time that will go by before the cache is emptied and recreated. A larger minimum cache lifetime offers better performance, but users will not see new content for a longer period of time.') ); + $form['cache_css'] = array( + '#type' => 'select', + '#title' => t('Cache and compress CSS files'), + '#default_value' => variable_get('cache_css', FALSE), + '#options' => array(t('No'), t('Yes')), + '#description' => t("A lot of 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 slow down the initial page load for a user browsing the site. Additionally, this additional HTTP request can increase the server load. It is recommended to only turn this option on when your site is in production, as leaving it on can interfere with theming development."), + ); + return system_settings_form($form); } @@ -1102,6 +1110,12 @@ function system_settings_form_submit($fo else { drupal_set_message(t('The configuration options have been saved.')); } + + // Only clear the CSS cache if the settings on the caching paged are saved + if ($form_id == 'system_page_caching_settings') { + drupal_flush_css_cache(); + } + menu_rebuild(); } @@ -1109,6 +1123,8 @@ function system_settings_form_submit($fo * Menu callback; displays a listing of all themes. */ function system_themes() { + // Clear cached CSS files + drupal_flush_css_cache(); $themes = system_theme_data(); ksort($themes); @@ -1481,6 +1497,10 @@ function system_modules_submit($form_id, if ($dependencies && !isset($form_values['confirm'])) { return FALSE; } + + // Clear the CSS cache + drupal_flush_css_cache(); + return 'admin/build/modules'; } === modified file 'update.php' --- update.php 2006-10-02 11:36:17 +0000 +++ update.php 2006-11-25 05:38:13 +0000 @@ -785,6 +785,9 @@ if (($access_check == FALSE) || ($user-> update_fix_watchdog(); update_fix_sessions(); + // Clear any cached CSS files, in case any module updates its CSS as well + drupal_flush_css_cache(); + $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; switch ($op) { case 'Update':