core/lib/Drupal/Core/Asset/CssOptimizer.php | 34 ++++++++++++++++++--------- core/modules/color/color.module | 3 +-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/lib/Drupal/Core/Asset/CssOptimizer.php b/core/lib/Drupal/Core/Asset/CssOptimizer.php index 8865623..2cf07e7 100644 --- a/core/lib/Drupal/Core/Asset/CssOptimizer.php +++ b/core/lib/Drupal/Core/Asset/CssOptimizer.php @@ -14,6 +14,13 @@ class CssOptimizer implements AssetOptimizerInterface { /** + * The base path used by rewriteFileURI(). + * + * @var string + */ + public $rewriteFileURIBasePath; + + /** * {@inheritdoc} */ public function optimize(array $css_asset) { @@ -39,8 +46,9 @@ protected function processFile($css_asset) { $contents = $this->loadFile($css_asset['data'], TRUE); // Get the parent directory of this file, relative to the Drupal root. - $css_base_url = substr($css_asset['data'], 0, strrpos($css_asset['data'], '/')); - $this->rewriteFileURI(NULL, $css_base_url . '/'); + $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', array($this, 'rewriteFileURI'), $contents); @@ -107,6 +115,12 @@ public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE) { * This function is used for recursive loading of stylesheets and * returns the stylesheet content with all url() paths corrected. * + * @param array $matches + * An array of matches by a preg_replace_callback() call that scans for + * @import-ed CSS files, except for external CSS files. + * @return + * The contents of the CSS file at $matches[1], with corrected paths. + * * @see Drupal\Core\Asset\AssetOptimizerInterface::loadFile() */ protected function loadNestedFile($matches) { @@ -191,21 +205,19 @@ protected function processCss($contents, $optimize = FALSE) { } /** - * Prefixes all paths within a CSS file for aggregateFile(). + * Prefixes all paths within a CSS file for processFile(). + * + * @param 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. * * Note: the only reason this method is public is so color.module can call it; * it is not on the AssetOptimizerInterface, so future refactorings can make * it protected. */ - public function rewriteFileURI($matches, $base = NULL) { - static $_base; - // Store base path for preg_replace_callback. - if (isset($base)) { - $_base = $base; - } - + public function rewriteFileURI($matches) { // Prefix with base and remove '../' segments where possible. - $path = $_base . $matches[1]; + $path = $this->rewriteFileURIBasePath . $matches[1]; $last = ''; while ($path != $last) { $last = $path; diff --git a/core/modules/color/color.module b/core/modules/color/color.module index e14a25d..7660d6b 100644 --- a/core/modules/color/color.module +++ b/core/modules/color/color.module @@ -388,8 +388,7 @@ function color_scheme_form_submit($form, &$form_state) { // Return the path to where this CSS file originated from, stripping // off the name of the file at the end of the path. - $base = base_path() . dirname($paths['source'] . $file) . '/'; - $css_optimizer->rewriteFileURI(NULL, $base); + $css_optimizer->rewriteFileURIBasePath = base_path() . dirname($paths['source'] . $file) . '/'; // Prefix all paths within this CSS file, ignoring absolute paths. $style = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', array($css_optimizer, 'rewriteFileURI'), $style);