You are here

public function AmpCssCollectionRenderer::rewriteFileURI in Accelerated Mobile Pages (AMP) 8.3

Prefixes all paths within a CSS file for processFile().

Copied from \Drupal\Core\Asset\CssOptimizer. We can't use that service because some modules, like the CDN module, decorate or alter it, and this method is not guaranteed by the interface, so we can't rely on it.

Parameters

array $matches: An array of matches by a preg_replace_callback() call that scans for url() references in CSS files, except for external or absolute ones.

Return value

string The rewritten file path.

See also

https://www.drupal.org/project/amp/issues/3094944

File

src/Asset/AmpCssCollectionRenderer.php, line 338

Class

AmpCssCollectionRenderer
Renders CSS assets.

Namespace

Drupal\amp\Asset

Code

public function rewriteFileURI($matches) {

  // Prefix with base and remove '../' segments where possible.
  $path = $this->rewriteFileURIBasePath . $matches[1];
  $last = '';
  while ($path != $last) {
    $last = $path;
    $path = preg_replace('`(^|/)(?!\\.\\./)([^/]+)/\\.\\./`', '$1', $path);
  }
  return 'url(' . file_url_transform_relative(file_create_url($path)) . ')';
}