From df2cf40d2cd197ed5f1960b026b31aad32b5d930 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 26 Oct 2008 18:06:39 +0000 Subject: [PATCH] - Patch #266358 by Rob Loach, mfer: use array in drupal_add_css(). --- includes/common.inc | 83 +++++++++++++++++++--------- includes/locale.inc | 2 +- includes/theme.inc | 2 +- includes/theme.maintenance.inc | 10 ++-- modules/block/block.admin.inc | 2 +- modules/color/color.module | 4 +- modules/dblog/dblog.module | 2 +- modules/help/help.admin.inc | 2 +- modules/openid/openid.module | 2 +- modules/openid/openid.pages.inc | 2 +- modules/search/search.module | 2 +- modules/simpletest/simpletest.module | 2 +- modules/simpletest/tests/common.test | 50 +++++++++++++++++ modules/system/system.module | 8 +-- modules/tracker/tracker.pages.inc | 2 +- modules/user/user.module | 2 +- 16 files changed, 128 insertions(+), 49 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index 0c6fd0ab796..33e83d1de59 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1731,55 +1731,84 @@ function drupal_add_link($attributes) { * 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 $type - * (optional) The type of stylesheet that is being added. Types are: module - * or theme. - * @param $media - * (optional) The media type for the stylesheet, e.g., all, print, screen. - * @param $preprocess - * (optional) Should this CSS file be aggregated and compressed if this - * feature has been turned on under the performance section? + * @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'. + * - '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. * - * 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. + * 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. * - * The reason for merging the CSS files is outlined quite thoroughly here: - * http://www.die.net/musings/page_load_time/ - * "Load fewer external objects. Due to request overhead, one bigger file - * just loads faster than two smaller ones half its size." + * The reason for merging the CSS files is outlined quite thoroughly here: + * http://www.die.net/musings/page_load_time/ + * "Load fewer external objects. Due to request overhead, one bigger file + * just loads faster than two smaller ones half its size." * - * However, you should *not* preprocess every file as this can lead to - * redundant caches. You should set $preprocess = FALSE when: + * However, you should *not* preprocess every file as this can lead to + * redundant caches. You should set $preprocess = FALSE when your styles + * are only used rarely on the site. This could be a special admin page, + * the homepage, or a handful of pages that does not represent the + * majority of the pages on your site. * - * - Your styles are only used rarely on the site. This could be a special - * admin page, the homepage, or a handful of pages that does not represent - * the majority of the pages on your site. - * - * Typical candidates for caching are for example styles for nodes across - * the site, or used in the theme. + * Typical candidates for caching are for example styles for nodes across + * the site, or used in the theme. + * @param $reset + * (optional) Resets the currently loaded cascading stylesheets. * @return * An array of CSS files. */ -function drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE) { +function drupal_add_css($path = NULL, $options = NULL, $reset = FALSE) { static $css = array(); global $language; + // Request made to reset the CSS added so far. + if ($reset) { + $css = array(); + } + // 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)) { + // Construct the options, taking the defaults into consideration. + if (isset($options)) { + if (!is_array($options)) { + $options = array('type' => $options); + } + } + else { + $options = array(); + } + $options += array( + 'type' => 'module', + 'media' => 'all', + '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][$type][$path] = $preprocess; + $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] = $preprocess; + $css[$media][$type][$rtl_path] = $options['preprocess']; } } } diff --git a/includes/locale.inc b/includes/locale.inc index 37b634c0262..00db655f7f9 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -2215,7 +2215,7 @@ function _locale_rebuild_js($langcode = NULL) { */ function _locale_translate_language_list($translation, $limit_language) { // Add CSS - drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css', array('preprocess' => FALSE)); $languages = language_list(); unset($languages['en']); diff --git a/includes/theme.inc b/includes/theme.inc index b90cf224d2b..ac6f955806a 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -133,7 +133,7 @@ function _init_theme($theme, $base_theme = array(), $registry_callback = '_theme // And now add the stylesheets properly foreach ($final_stylesheets as $media => $stylesheets) { foreach ($stylesheets as $stylesheet) { - drupal_add_css($stylesheet, 'theme', $media); + drupal_add_css($stylesheet, array('type' => 'theme', 'media' => $media)); } } diff --git a/includes/theme.maintenance.inc b/includes/theme.maintenance.inc index 713320cf656..f64fd424552 100644 --- a/includes/theme.maintenance.inc +++ b/includes/theme.maintenance.inc @@ -62,11 +62,11 @@ function _drupal_maintenance_theme() { // These are usually added from system_init() -except maintenance.css. // When the database is inactive it's not called so we add it here. - drupal_add_css(drupal_get_path('module', 'system') . '/defaults.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/system.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/maintenance.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/admin.css', 'module'); + drupal_add_css(drupal_get_path('module', 'system') . '/defaults.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/system.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/maintenance.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/admin.css'); } /** diff --git a/modules/block/block.admin.inc b/modules/block/block.admin.inc index a8f515d031e..9e16f8af62a 100644 --- a/modules/block/block.admin.inc +++ b/modules/block/block.admin.inc @@ -28,7 +28,7 @@ function block_admin_display($theme = NULL) { function block_admin_display_form(&$form_state, $blocks, $theme = NULL) { global $theme_key, $custom_theme; - drupal_add_css(drupal_get_path('module', 'block') . '/block.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'block') . '/block.css', array('preprocess' => FALSE)); // If non-default theme configuration has been selected, set the custom theme. $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland'); diff --git a/modules/color/color.module b/modules/color/color.module index e99f907fc9d..51c894e514d 100644 --- a/modules/color/color.module +++ b/modules/color/color.module @@ -153,11 +153,11 @@ function color_scheme_form(&$form_state, $theme) { $info = color_get_info($theme); // Add Farbtastic color picker. - drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE); + drupal_add_css('misc/farbtastic/farbtastic.css', array('preprocess' => FALSE)); drupal_add_js('misc/farbtastic/farbtastic.js'); // Add custom CSS and JS. - drupal_add_css($base . '/color.css', 'module', 'all', FALSE); + drupal_add_css($base . '/color.css', array('preprocess' => FALSE)); drupal_add_js($base . '/color.js'); drupal_add_js(array('color' => array( 'reference' => color_get_palette($theme, TRUE) diff --git a/modules/dblog/dblog.module b/modules/dblog/dblog.module index 7fbee34bc49..5f3623e5312 100644 --- a/modules/dblog/dblog.module +++ b/modules/dblog/dblog.module @@ -83,7 +83,7 @@ function dblog_menu() { function dblog_init() { if (arg(0) == 'admin' && arg(1) == 'reports') { // Add the CSS for this module - drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'dblog') . '/dblog.css', array('preprocess' => FALSE)); } } diff --git a/modules/help/help.admin.inc b/modules/help/help.admin.inc index 701f8fabc78..5b038c729f4 100644 --- a/modules/help/help.admin.inc +++ b/modules/help/help.admin.inc @@ -11,7 +11,7 @@ */ function help_main() { // Add CSS - drupal_add_css(drupal_get_path('module', 'help') . '/help.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'help') . '/help.css', array('preprocess' => FALSE)); $output = '

' . t('Help topics') . '

' . t('Help is available on the following items:') . '

' . help_links_as_list(); return $output; } diff --git a/modules/openid/openid.module b/modules/openid/openid.module index 3af31b0d6fe..d02c60b23b5 100644 --- a/modules/openid/openid.module +++ b/modules/openid/openid.module @@ -75,7 +75,7 @@ function openid_user_insert(&$edit, &$account, $category = NULL) { */ function openid_form_alter(&$form, $form_state, $form_id) { if ($form_id == 'user_login_block' || $form_id == 'user_login') { - drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css', 'module'); + drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css'); drupal_add_js(drupal_get_path('module', 'openid') . '/openid.js'); if (!empty($form_state['post']['openid_identifier'])) { $form['name']['#required'] = FALSE; diff --git a/modules/openid/openid.pages.inc b/modules/openid/openid.pages.inc index efd7684eee2..28c8f947b91 100644 --- a/modules/openid/openid.pages.inc +++ b/modules/openid/openid.pages.inc @@ -29,7 +29,7 @@ function openid_authentication_page() { */ function openid_user_identities($account) { drupal_set_title($account->name); - drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css', 'module'); + drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css'); // Check to see if we got a response $result = openid_complete(); diff --git a/modules/search/search.module b/modules/search/search.module index 6846f8f6561..c96dfbfa71b 100644 --- a/modules/search/search.module +++ b/modules/search/search.module @@ -1031,7 +1031,7 @@ function search_get_keys() { function search_form(&$form_state, $action = '', $keys = '', $type = NULL, $prompt = NULL) { // Add CSS - drupal_add_css(drupal_get_path('module', 'search') . '/search.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'search') . '/search.css', array('preprocess' => FALSE)); if (!$action) { $action = url('search/' . $type); diff --git a/modules/simpletest/simpletest.module b/modules/simpletest/simpletest.module index 372ad1f3f21..c1752b84f65 100644 --- a/modules/simpletest/simpletest.module +++ b/modules/simpletest/simpletest.module @@ -199,7 +199,7 @@ function simpletest_test_form() { } function theme_simpletest_test_table($table) { - drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css', 'module'); + drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css'); drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', 'module'); // Create header for test selection table. diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 74e053cf98e..6a82018db58 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -114,6 +114,56 @@ class DrupalTagsHandlingTestCase extends DrupalWebTestCase { } } +/** + * Test the Drupal CSS system. + */ +class CascadingStylesheetsTestCase extends DrupalWebTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Cascading stylesheets'), + 'description' => t('Tests adding various cascading stylesheets to the page.'), + 'group' => t('System') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp(); + // Reset drupal_add_css() before each test. + drupal_add_css(NULL, NULL, TRUE); + } + + /** + * Check default stylesheets as empty. + */ + function testDefault() { + $this->assertEqual(array(), drupal_add_css(), t('Default CSS is empty.')); + } + + /** + * Tests adding a file stylesheet. + */ + function testAddFile() { + $path = drupal_get_path('module', 'simpletest') . '/simpletest.css'; + $css = drupal_add_css($path); + $this->assertEqual($css['all']['module'][$path], TRUE, t('Adding a CSS file caches it properly.')); + } + + /** + * Tests rendering the stylesheets. + */ + function testRenderFile() { + $css = drupal_get_path('module', 'simpletest') . '/simpletest.css'; + drupal_add_css($css); + $this->assertTrue(strpos(drupal_get_css(), $css) > 0, t('Rendered CSS includes the added stylesheet.')); + } +} + /** * Test drupal_http_request(). */ diff --git a/modules/system/system.module b/modules/system/system.module index 633623a4bb6..ed9dda45b7a 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -706,13 +706,13 @@ function system_init() { if (arg(0) == 'admin' || (variable_get('node_admin_theme', '0') && arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit'))) { global $custom_theme; $custom_theme = variable_get('admin_theme', '0'); - drupal_add_css(drupal_get_path('module', 'system') . '/admin.css', 'module'); + drupal_add_css(drupal_get_path('module', 'system') . '/admin.css'); } // Add the CSS for this module. - drupal_add_css(drupal_get_path('module', 'system') . '/defaults.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/system.css', 'module'); - drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css', 'module'); + drupal_add_css(drupal_get_path('module', 'system') . '/defaults.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/system.css'); + drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css'); } /** diff --git a/modules/tracker/tracker.pages.inc b/modules/tracker/tracker.pages.inc index 027b3941928..5a7fd3f3c7f 100644 --- a/modules/tracker/tracker.pages.inc +++ b/modules/tracker/tracker.pages.inc @@ -12,7 +12,7 @@ */ function tracker_page($account = NULL, $set_title = FALSE) { // Add CSS - drupal_add_css(drupal_get_path('module', 'tracker') . '/tracker.css', 'module', 'all', FALSE); + drupal_add_css(drupal_get_path('module', 'tracker') . '/tracker.css', array('preprocess' => FALSE)); if ($account) { if ($set_title) { diff --git a/modules/user/user.module b/modules/user/user.module index a8c9de8be43..351a7fe9073 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -1120,7 +1120,7 @@ function user_menu() { } function user_init() { - drupal_add_css(drupal_get_path('module', 'user') . '/user.css', 'module'); + drupal_add_css(drupal_get_path('module', 'user') . '/user.css'); } function user_uid_optional_load($arg) {