- Patch #266358 by Rob Loach, mfer: use array in drupal_add_css().

merge-requests/26/head
Dries Buytaert 2008-10-26 18:06:39 +00:00
parent 617fe51e62
commit df2cf40d2c
16 changed files with 128 additions and 49 deletions

View File

@ -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'];
}
}
}

View File

@ -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']);

View File

@ -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));
}
}

View File

@ -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');
}
/**

View File

@ -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');

View File

@ -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)

View File

@ -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));
}
}

View File

@ -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 = '<h2>' . t('Help topics') . '</h2><p>' . t('Help is available on the following items:') . '</p>' . help_links_as_list();
return $output;
}

View File

@ -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;

View File

@ -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();

View File

@ -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);

View File

@ -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.

View File

@ -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().
*/

View File

@ -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');
}
/**

View File

@ -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) {

View File

@ -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) {