Skip to content

Commit

Permalink
Fixed pipeline + remote url rewrites #2216
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Oct 8, 2018
1 parent 1d6cdd4 commit b96e264
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fixed asset manager to not add empty assets when they don't exist in the filesystem
* Regression: Fixed asset manager methods with default legacy attributes
* Update `script` and `style` Twig tags to use the new `Assets` classes
* Fixed asset pipeline to rewrite remote URLs as well as local [#2216](https://github.com/getgrav/grav/issues/2216)

# v1.6.0-beta.1
## 10/01/2018
Expand Down
19 changes: 10 additions & 9 deletions system/src/Grav/Common/Assets/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function renderCss($assets, $group, $attributes = [], &$no_pipeline = [])
}

// Concatenate files
$buffer = $this->gatherLinks($assets, self::CSS_ASSET);
$buffer = $this->gatherLinks($assets, self::CSS_ASSET, $no_pipeline);

// Minify if required
if ($this->shouldMinify('css')) {
Expand Down Expand Up @@ -160,7 +160,8 @@ public function renderCss($assets, $group, $attributes = [], &$no_pipeline = [])
*
* @param array $assets
* @param string $group
* @param array $attributes
* @param array $attributes
* @param array $no_pipeline
*
* @return bool|string URL or generated content if available, else false
*/
Expand Down Expand Up @@ -202,7 +203,7 @@ public function renderJs($assets, $group, $attributes = [], &$no_pipeline = [])
}

// Concatenate files
$buffer = $this->gatherLinks($assets, self::JS_ASSET);
$buffer = $this->gatherLinks($assets, self::JS_ASSET, $no_pipeline);

// Minify if required
if ($this->shouldMinify('js')) {
Expand All @@ -228,23 +229,23 @@ public function renderJs($assets, $group, $attributes = [], &$no_pipeline = [])
}



/**
* Finds relative CSS urls() and rewrites the URL with an absolute one
*
* @param string $file the css source file
* @param string $relative_path relative path to the css file
* @param string $file the css source file
* @param string $dir , $local relative path to the css file
* @param boolean $local is this a local or remote asset
*
* @return mixed
*/
protected function cssRewrite($file, $relative_path)
protected function cssRewrite($file, $dir, $local)
{
// Strip any sourcemap comments
$file = preg_replace(self::CSS_SOURCEMAP_REGEX, '', $file);

// 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 ($relative_path) {
$file = (string)preg_replace_callback(self::CSS_URL_REGEX, function ($matches) use ($dir, $local) {

$old_url = $matches[2];

Expand All @@ -253,7 +254,7 @@ protected function cssRewrite($file, $relative_path)
return $matches[0];
}

$new_url = $this->base_url . ltrim(Utils::normalizePath($relative_path . '/' . $old_url), '/');
$new_url = ($local ? $this->base_url: '') . ltrim(Utils::normalizePath($dir . '/' . $old_url), '/');

return str_replace($old_url, $new_url, $matches[0]);
}, $file);
Expand Down
20 changes: 12 additions & 8 deletions system/src/Grav/Common/Assets/Traits/AssetUtilsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ public static function isRemoteLink($link)
/**
* Download and concatenate the content of several links.
*
* @param array $links
* @param bool $css
* @param array $assets
* @param bool $css
* @param array $no_pipeline
*
* @return string
*/
protected function gatherLinks(array $links, $css = true)
protected function gatherLinks(array $assets, $css = true, &$no_pipeline = [])
{
$buffer = '';


foreach ($links as $asset) {
$relative_dir = '';
foreach ($assets as $id => $asset) {
$local = true;

$link = $asset->getAsset();
Expand All @@ -57,9 +57,10 @@ protected function gatherLinks(array $links, $css = true)
if (0 === strpos($link, '//')) {
$link = 'http:' . $link;
}
$relative_dir = dirname($relative_path);
} else {
// Fix to remove relative dir if grav is in one
if (($this->base_url !== '/') && strpos($this->base_url, $link) === 0) {
if (($this->base_url !== '/') && Utils::startsWith($relative_path, $this->base_url)) {
$base_url = '#' . preg_quote($this->base_url, '#') . '#';
$relative_path = ltrim(preg_replace($base_url, '/', $link, 1), '/');
}
Expand All @@ -72,6 +73,9 @@ protected function gatherLinks(array $links, $css = true)

// No file found, skip it...
if ($file === false) {
if (!$local) { // Assume we coudln't download this file for some reason assume it's not pipeline compatible
$no_pipeline[$id] = $asset;
}
continue;
}

Expand All @@ -81,8 +85,8 @@ protected function gatherLinks(array $links, $css = true)
}

// If this is CSS + the file is local + rewrite enabled
if ($css && $local && $this->css_rewrite) {
$file = $this->cssRewrite($file, $relative_dir);
if ($css && $this->css_rewrite) {
$file = $this->cssRewrite($file, $relative_dir, $local);
}

$file = rtrim($file) . PHP_EOL;
Expand Down

0 comments on commit b96e264

Please sign in to comment.