You are here

protected function CssMinifier::minifyCore in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.3 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;
}