function drupal_load_stylesheet_content

7.x common.inc drupal_load_stylesheet_content($contents, $optimize = FALSE)

Processes the contents of a stylesheet for aggregation.

Parameters

$contents: The contents of the stylesheet.

$optimize: (optional) Boolean whether CSS contents should be minified. Defaults to FALSE.

Return value

Contents of the stylesheet including the imported stylesheets.

4 calls to drupal_load_stylesheet_content()
CascadingStylesheetsTestCase::testRenderInlinePreprocess in drupal-7.x/modules/simpletest/tests/common.test
Tests rendering inline stylesheets with preprocessing on.
CascadingStylesheetsTestCase::testRenderRemoveCharsetPreprocess in drupal-7.x/modules/simpletest/tests/common.test
Tests removing charset when rendering stylesheets with preprocessing on.
drupal_aggregate_css in drupal-7.x/includes/common.inc
Default callback to aggregate CSS files and inline content.
drupal_load_stylesheet in drupal-7.x/includes/common.inc
Loads the stylesheet and resolves all @import commands.

File

drupal-7.x/includes/common.inc, line 3707
Common functions that many Drupal modules will need to reference.

Code

function drupal_load_stylesheet_content($contents, $optimize = FALSE) {
  // 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 = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
    // Strip all comment blocks, but keep double/single quoted strings.
    $contents = preg_replace(
    "<($double_quot|$single_quot)|$comment>Ss", 
    "$1", 
    $contents
    );
    // Remove certain whitespace.
    // There are different conditions for removing leading and trailing
    // whitespace.
    // @see http://php.net/manual/regexp.reference.subpatterns.php
    $contents = preg_replace('<
      # Strip leading and trailing whitespace.
        \s*([@{};,])\s*
      # Strip only leading whitespace from:
      # - Closing parenthesis: Retain "@media (bar) and foo".
      | \s+([\)])
      # Strip only trailing whitespace from:
      # - Opening parenthesis: Retain "@media (bar) and foo".
      # - Colon: Retain :pseudo-selectors.
      | ([\(:])\s+
    >xS', 
    // Only one of the three capturing groups will match, so its reference
    // will contain the wanted value and the references for the
    // two non-matching groups will be replaced with empty strings.
    '$1$2$3', 
    $contents
    );
    // End the file with a new line.
    $contents = trim($contents);
    $contents .= "\n";
  }

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