function CssCollectionOptimizerLazy::optimizeGroup

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php \Drupal\Core\Asset\CssCollectionOptimizerLazy::optimizeGroup()

Overrides AssetCollectionGroupOptimizerInterface::optimizeGroup

File

core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php, line 148

Class

CssCollectionOptimizerLazy
Optimizes CSS assets.

Namespace

Drupal\Core\Asset

Code

public function optimizeGroup(array $group) : string {
    // Optimize each asset within the group.
    $data = '';
    $current_license = FALSE;
    foreach ($group['items'] as $css_asset) {
        // Ensure license information is available as a comment after
        // optimization.
        if ($css_asset['license'] !== $current_license) {
            $data .= "/* @license " . $css_asset['license']['name'] . " " . $css_asset['license']['url'] . " */\n";
        }
        $current_license = $css_asset['license'];
        $data .= $this->optimizer
            ->optimize($css_asset);
    }
    // Per the W3C specification at
    // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import rules must
    // precede any other style, so we move those to the top. The regular
    // expression is expressed in NOWDOC since it is detecting backslashes as
    // well as single and double quotes. It is difficult to read when
    // represented as a quoted string.
    $regexp = <<<'REGEXP'
/@import\s*(?:'(?:\\'|.)*'|"(?:\\"|.)*"|url\(\s*(?:\\[\)\'\"]|[^'")])*\s*\)|url\(\s*'(?:\'|.)*'\s*\)|url\(\s*"(?:\"|.)*"\s*\)).*;/iU
REGEXP;
    preg_match_all($regexp, $data, $matches);
    $data = preg_replace($regexp, '', $data);
    return implode('', $matches[0]) . (!empty($matches[0]) ? "\n" : '') . $data;
}

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