You are here

function drupal_load_stylesheet in Drupal 6

Same name and namespace in other branches
  1. 7 includes/common.inc \drupal_load_stylesheet()

Loads the stylesheet and resolves all @import commands.

Loads a stylesheet and replaces @import commands with the contents of the imported file. Use this instead of file_get_contents when processing stylesheets.

The returned contents are compressed removing white space and comments only when CSS aggregation is enabled. This optimization will not apply for color.module enabled themes with CSS aggregation turned off.

Parameters

$file: Name of the stylesheet to be processed.

$optimize: Defines if CSS contents should be compressed or not.

Return value

Contents of the stylesheet including the imported stylesheets.

3 calls to drupal_load_stylesheet()
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
drupal_build_css_cache in includes/common.inc
Aggregate and optimize CSS files, putting them in the files directory.
_drupal_load_stylesheet in includes/common.inc
Loads stylesheets recursively and returns contents with corrected paths.

File

includes/common.inc, line 2076
Common functions that many Drupal modules will need to reference.

Code

function drupal_load_stylesheet($file, $optimize = NULL) {
  static $_optimize;

  // Store optimization parameter for preg_replace_callback with nested @import loops.
  if (isset($optimize)) {
    $_optimize = $optimize;
  }
  $contents = '';
  if (file_exists($file)) {

    // Load the local CSS stylesheet.
    $contents = file_get_contents($file);

    // Change to the current stylesheet's directory.
    $cwd = getcwd();
    chdir(dirname($file));

    // Replaces @import commands with the actual stylesheet content.
    // This happens recursively but omits external files.
    $contents = preg_replace_callback('/@import\\s*(?:url\\()?[\'"]?(?![a-z]+:)([^\'"\\()]+)[\'"]?\\)?;/', '_drupal_load_stylesheet', $contents);

    // Remove multiple charset declarations for standards compliance (and fixing Safari problems).
    $contents = preg_replace('/^@charset\\s+[\'"](\\S*)\\b[\'"];/i', '', $contents);
    if ($_optimize) {

      // Perform some safe CSS optimizations.
      // Regexp to match comment blocks.
      $comment = '/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/';

      // Regexp to match double quoted strings.
      $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';

      // Regexp to match single quoted strings.
      $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
      $contents = preg_replace_callback("<{$double_quot}|{$single_quot}|{$comment}>Ss", "_process_comment", $contents);

      // and feed them to _process_comment().
      $contents = preg_replace('<\\s*([@{}:;,]|\\)\\s|\\s\\()\\s*>S', '\\1', $contents);

      // but keep space around parentheses.
      // End the file with a new line.
      $contents .= "\n";
    }

    // Change back directory.
    chdir($cwd);
  }
  return $contents;
}