You are here

protected function CssOptimizer::processFile in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::processFile()
  2. 9 core/lib/Drupal/Core/Asset/CssOptimizer.php \Drupal\Core\Asset\CssOptimizer::processFile()

Processes CSS file and adds base URLs to any relative resource paths.

Parameters

array $css_asset: A CSS asset. The array should contain the `data` key where the value should be the path to the CSS file relative to the Drupal root. This is an example of the `data` key's value, "core/assets/vendor/normalize-css/normalize.css".

Return value

string The asset's cleaned/optimized contents.

File

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

Class

CssOptimizer
Optimizes a CSS asset.

Namespace

Drupal\Core\Asset

Code

protected function processFile($css_asset) {
  $contents = $this
    ->loadFile($css_asset['data'], TRUE);
  if ($css_asset['media'] !== 'print' && $css_asset['media'] !== 'all') {
    $contents = '@media ' . $css_asset['media'] . '{' . $contents . '}' . "\n";
  }
  $contents = $this
    ->clean($contents);

  // Get the parent directory of this file, relative to the Drupal root.
  $css_base_path = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/'));

  // Store base path.
  $this->rewriteFileURIBasePath = $css_base_path . '/';

  // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
  return preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', [
    $this,
    'rewriteFileURI',
  ], $contents);
}