You are here

function advagg_load_stylesheet_content in Advanced CSS/JS Aggregation 7.2

Processes the contents of a stylesheet for aggregation.

Parameters

string $contents: The contents of the stylesheet.

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

Return value

string Contents of the stylesheet including the imported stylesheets.

See also

drupal_load_stylesheet_content()

4 calls to advagg_load_stylesheet_content()
AdvAggCascadingStylesheetsTestCase::testRenderFile in tests/advagg.test
Tests rendering the stylesheets.
advagg_load_stylesheet in ./advagg.module
Loads the stylesheet and resolves all @import commands.
advagg_mod_admin_test_css_files in advagg_mod/advagg_mod.admin.inc
Test all CSS files seeing if any string translations do anything.
_advagg_aggregate_css in ./advagg.module
Default callback to aggregate CSS files and inline content.

File

./advagg.module, line 5220
Advanced CSS/JS aggregation module.

Code

function advagg_load_stylesheet_content($contents, $optimize = FALSE) {

  // If a BOM is found, convert the file to UTF-8. Used for inline CSS here.
  $encoding = advagg_get_encoding_from_bom($contents);
  if (!empty($encoding)) {
    $contents = advagg_convert_to_utf8($contents, $encoding);
  }
  if ($optimize) {

    // Perform some safe CSS optimizations.
    // Regexp to match comment blocks.
    // Regexp to match double quoted strings.
    // Regexp to match single quoted strings.
    $comment = '/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/';
    $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
    $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('<
      # Do not strip any space from within single or double quotes
      (' . $double_quot . '|' . $single_quot . ')
      # 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+
    >xSs', '$1$2$3$4', $contents);

    // End the file with a new line.
    $contents = trim($contents);
    $contents .= "\n";
  }

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

  // 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*+\\)?+\\s*+;%i', '_advagg_load_stylesheet', $contents);
  return $contents;
}