Commit 1a5c71e2 authored by Dries's avatar Dries

- Patch #92877 by mfer, Rob Loach, Damien Tournoud, et al: add numeric weight to drupal_add_css.

parent eb34d299
This diff is collapsed.
......@@ -132,7 +132,7 @@ function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callb
// And now add the stylesheets properly
foreach ($final_stylesheets as $media => $stylesheets) {
foreach ($stylesheets as $stylesheet) {
drupal_add_css($stylesheet, array('type' => 'theme', 'media' => $media));
drupal_add_css($stylesheet, array('weight' => CSS_THEME, 'media' => $media));
}
}
......
......@@ -583,6 +583,33 @@ function locale_js_alter(&$javascript) {
}
}
/*
* Implement hook_css_alter().
*
* This function checks all CSS files currently added via drupal_add_css() and
* and checks to see if a related right to left CSS file should be included.
*/
function locale_css_alter(&$css) {
global $language;
// If the current language is RTL, add the CSS file with the RTL overrides.
if ($language->direction == LANGUAGE_RTL) {
foreach ($css as $data => $item) {
// Only provide RTL overrides for files.
if ($item['type'] == 'file') {
$rtl_path = str_replace('.css', '-rtl.css', $item['data']);
if (file_exists($rtl_path) && !isset($css[$rtl_path])) {
// Replicate the same item, but with the RTL path and a little larger
// weight so that it appears directly after the original CSS file.
$item['data'] = $rtl_path;
$item['weight'] += 0.01;
$css[$rtl_path] = $item;
}
}
}
}
}
// ---------------------------------------------------------------------------------
// Language switcher block
......
......@@ -203,7 +203,7 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
}
function setUp() {
parent::setUp('php');
parent::setUp('php', 'locale');
// Reset drupal_add_css() before each test.
drupal_static_reset('drupal_add_css');
}
......@@ -221,7 +221,7 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
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.'));
$this->assertEqual($css[$path]['data'], $path, t('Adding a CSS file caches it properly.'));
}
/**
......@@ -249,7 +249,7 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
$css_preprocessed = '<style type="text/css">' . drupal_load_stylesheet_content($css, TRUE) . '</style>';
drupal_add_css($css, 'inline');
$css = drupal_get_css();
$this->assertEqual($css, $css_preprocessed, t('Rendering preprocessed inline CSS adds it to the page.'));
$this->assertEqual($css, "\n" . $css_preprocessed, t('Rendering preprocessed inline CSS adds it to the page.'));
}
/**
......@@ -280,6 +280,74 @@ class CascadingStylesheetsTestCase extends DrupalWebTestCase {
$this->drupalGet('node/' . $node->nid);
$this->assertRaw($expected, t('Inline stylesheets appear in the full page rendering.'));
}
/**
* Test CSS ordering.
*/
function testRenderOrder() {
// A module CSS file.
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
// A few system CSS files, ordered in a strange way.
$system_path = drupal_get_path('module', 'system');
drupal_add_css($system_path . '/defaults.css', array('weight' => CSS_SYSTEM));
drupal_add_css($system_path . '/system.css', array('weight' => CSS_SYSTEM - 10));
drupal_add_css($system_path . '/system-menus.css', array('weight' => CSS_SYSTEM));
$expected = array(
$system_path . '/system.css',
$system_path . '/defaults.css',
$system_path . '/system-menus.css',
drupal_get_path('module', 'simpletest') . '/simpletest.css',
);
$css = drupal_get_css();
if (preg_match_all('/href="' . preg_quote(base_path(), '/') . '([^?]+)\?/', $css, $matches)) {
$result = $matches[1];
}
else {
$result = array();
}
$this->assertIdentical($result, $expected, t('The CSS files are in the expected order.'));
}
/**
* Test CSS override.
*/
function testRenderOverride() {
drupal_add_css(drupal_get_path('module', 'system') . '/system.css');
drupal_add_css(drupal_get_path('module', 'simpletest') . '/tests/system.css');
// The dummy stylesheet should be the only one included.
$css = drupal_get_css();
$this->assert(strpos($css, drupal_get_path('module', 'simpletest') . '/tests/system.css') !== FALSE, t('The overriding CSS file is output.'));
$this->assert(strpos($css, drupal_get_path('module', 'system') . '/system.css') === FALSE, t('The overriden CSS file is not output.'));
drupal_add_css(drupal_get_path('module', 'simpletest') . '/tests/system.css');
drupal_add_css(drupal_get_path('module', 'system') . '/system.css');
// The standard stylesheet should be the only one included.
$css = drupal_get_css();
$this->assert(strpos($css, drupal_get_path('module', 'system') . '/system.css') !== FALSE, t('The overriding CSS file is output.'));
$this->assert(strpos($css, drupal_get_path('module', 'simpletest') . '/tests/system.css') === FALSE, t('The overriden CSS file is not output.'));
}
/**
* Tests Locale module's CSS Alter to include RTL overrides.
*/
function testAlter() {
// Switch the language to a right to left language and add system.css.
global $language;
$language->direction = LANGUAGE_RTL;
drupal_add_css(drupal_get_path('module', 'system') . '/system.css');
// Check to see if system-rtl.css was also added.
$css = drupal_get_css();
$this->assert(strpos($css, drupal_get_path('module', 'system') . '/system-rtl.css') !== FALSE, t('CSS is alterable as right to left overrides are added.'));
// Change the language back to left to right.
$language->direction = LANGUAGE_LTR;
}
}
/**
......
......@@ -280,6 +280,19 @@ function hook_library_alter(&$libraries, $module) {
}
}
/**
* Alter CSS files before they are output on the page.
*
* @param $css
* An array of all CSS items (files and inline CSS) being requested on the page.
* @see drupal_add_css()
* @see drupal_get_css()
*/
function hook_css_alter(&$css) {
// Remove defaults.css file.
unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
}
/**
* Perform alterations before a page is rendered.
*
......
......@@ -1327,13 +1327,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');
drupal_add_css(drupal_get_path('module', 'system') . '/admin.css', array('weight' => CSS_SYSTEM));
}
// Add the CSS for this 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') . '/defaults.css', array('weight' => CSS_SYSTEM));
drupal_add_css(drupal_get_path('module', 'system') . '/system.css', array('weight' => CSS_SYSTEM));
drupal_add_css(drupal_get_path('module', 'system') . '/system-menus.css', array('weight' => CSS_SYSTEM));
// Ignore slave database servers for this request.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment