Skip to content

Commit

Permalink
Fix url of @import not being rewritten (getgrav#3750)
Browse files Browse the repository at this point in the history
Looks good.  thanks.
  • Loading branch information
pamtbaau authored Oct 2, 2023
1 parent a8042a6 commit 3cdbc58
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions system/src/Grav/Common/Assets/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Pipeline extends PropertyObject
protected const JS_MODULE_ASSET = 3;

/** @const Regex to match CSS urls */
protected const CSS_URL_REGEX = '{url\(([\'\"]?)(.*?)\1\)}';
protected const CSS_URL_REGEX = '{url\(([\'\"]?)(.*?)\1\)|(@import)\s+([\'\"])(.*?)\4}';

/** @const Regex to match JS imports */
protected const JS_IMPORT_REGEX = '{import.+from\s?[\'|\"](.+?)[\'|\"]}';
Expand Down Expand Up @@ -257,9 +257,14 @@ protected function cssRewrite($file, $dir, $local)
// Find any css url() elements, grab the URLs and calculate an absolute path
// Then replace the old url with the new one
$file = (string)preg_replace_callback(self::CSS_URL_REGEX, function ($matches) use ($dir, $local) {
$isImport = count($matches) > 3 && $matches[3] === '@import';

$old_url = $matches[2];

if ($isImport) {
$old_url = $matches[5];
} else {
$old_url = $matches[2];
}

// Ensure link is not rooted to web server, a data URL, or to a remote host
if (preg_match(self::FIRST_FORWARDSLASH_REGEX, $old_url) || Utils::startsWith($old_url, 'data:') || $this->isRemoteLink($old_url)) {
return $matches[0];
Expand All @@ -273,7 +278,11 @@ protected function cssRewrite($file, $dir, $local)

$new_url = ($local ? $this->base_url : '') . $old_url;

return str_replace($matches[2], $new_url, $matches[0]);
if ($isImport) {
return str_replace($matches[5], $new_url, $matches[0]);
} else {
return str_replace($matches[2], $new_url, $matches[0]);
}
}, $file);

return $file;
Expand Down

0 comments on commit 3cdbc58

Please sign in to comment.