Same name in other branches
  1. 6.0.x advagg_css_minify/src/Asset/CssMinifier.php \Drupal\advagg_css_minify\Asset\CssMinifier::minifyCore()
  2. 8.x-3.x advagg_css_minify/src/Asset/CssMinifier.php \Drupal\advagg_css_minify\Asset\CssMinifier::minifyCore()
  3. 8.x-4.x advagg_css_minify/src/Asset/CssMinifier.php \Drupal\advagg_css_minify\Asset\CssMinifier::minifyCore()

Minify a css string with the core css minification algorithm.

Parameters

string $contents: The contents of the stylesheet.

Return value

string Minified css by the core minification method.

See also

\Drupal\Core\Asset\CssOptimizer::processCss()

1 call to CssMinifier::minifyCore()
CssMinifier::optimize in advagg_css_minify/src/Asset/CssMinifier.php
Optimize the asset's content.

File

advagg_css_minify/src/Asset/CssMinifier.php, line 133

Class

CssMinifier
Optimizes a JavaScript asset.

Namespace

Drupal\advagg_css_minify\Asset

Code

protected function minifyCore($contents) {
    // 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);
    return $contents;
}