Skip to content

Commit 746c91e

Browse files
committed
Preventing the base path or absolute URL from being prefixed incorrectly on an absolute URL
If the version strategy returns an absolute URL, that should be the final URL.
1 parent 5742958 commit 746c91e

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/Symfony/Component/Asset/PathPackage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public function getUrl($path)
5656
return $path;
5757
}
5858

59-
return $this->getBasePath().ltrim($this->getVersionStrategy()->applyVersion($path), '/');
59+
$versionedPath = $this->getVersionStrategy()->applyVersion($path);
60+
61+
if ($this->isAbsoluteUrl($versionedPath)) {
62+
return $versionedPath;
63+
}
64+
65+
return $this->getBasePath().ltrim($versionedPath, '/');
6066
}
6167

6268
/**

src/Symfony/Component/Asset/Tests/PathPackageTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public function getContextConfigs()
7575
);
7676
}
7777

78+
public function testVersionStrategyGivesAbsoluteURL()
79+
{
80+
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
81+
$versionStrategy->expects($this->any())
82+
->method('applyVersion')
83+
->willReturn('https://cdn.com/bar/main.css');
84+
$package = new PathPackage('/subdirectory', $versionStrategy, $this->getContext('/bar'));
85+
86+
$this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
87+
}
88+
7889
private function getContext($basePath)
7990
{
8091
$context = $this->getMockBuilder('Symfony\Component\Asset\Context\ContextInterface')->getMock();

src/Symfony/Component/Asset/Tests/UrlPackageTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public function getContextConfigs()
7777
);
7878
}
7979

80+
public function testVersionStrategyGivesAbsoluteURL()
81+
{
82+
$versionStrategy = $this->getMockBuilder('Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface')->getMock();
83+
$versionStrategy->expects($this->any())
84+
->method('applyVersion')
85+
->willReturn('https://cdn.com/bar/main.css');
86+
$package = new UrlPackage('https://example.com', $versionStrategy);
87+
88+
$this->assertEquals('https://cdn.com/bar/main.css', $package->getUrl('main.css'));
89+
}
90+
8091
/**
8192
* @expectedException \Symfony\Component\Asset\Exception\LogicException
8293
*/

src/Symfony/Component/Asset/UrlPackage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getUrl($path)
8181

8282
$url = $this->getVersionStrategy()->applyVersion($path);
8383

84+
if ($this->isAbsoluteUrl($url)) {
85+
return $url;
86+
}
87+
8488
if ($url && '/' != $url[0]) {
8589
$url = '/'.$url;
8690
}

0 commit comments

Comments
 (0)