function CssOptimizer::processCss

Same name in other branches
  1. 9 core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::processCss()
  2. 8.9.x core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::processCss()
  3. 11.x core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::processCss()

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

string Contents of the stylesheet including the imported stylesheets.

1 call to CssOptimizer::processCss()
CssOptimizer::loadFile in core/lib/Drupal/Core/Asset/CssOptimizer.php
Loads the stylesheet and resolves all @import commands.

File

core/lib/Drupal/Core/Asset/CssOptimizer.php, line 215

Class

CssOptimizer
Optimizes a CSS asset.

Namespace

Drupal\Core\Asset

Code

protected function processCss($contents, $optimize = FALSE) {
    // Remove unwanted CSS code that cause issues.
    $contents = $this->clean($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('<
        # 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";
    }
    // Replaces @import commands with the actual stylesheet content.
    // This happens recursively but omits external files and local files
    // with supports- or media-query qualifiers, as those are conditionally
    // loaded depending on the user agent.
    $contents = preg_replace_callback('/@import\\s*(?:url\\(\\s*)?[\'"]?(?![a-z]+:)(?!\\/\\/)([^\'"\\()]+)[\'"]?\\s*\\)?\\s*;/', [
        $this,
        'loadNestedFile',
    ], $contents);
    return $contents;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.