Commit 5643757e authored by alexpott's avatar alexpott

Issue #2573635 by Wim Leers, Mac_Weber, lluvigne, dawehner, dimaro,...

Issue #2573635 by Wim Leers, Mac_Weber, lluvigne, dawehner, dimaro, ConfuciusDa1st, rootwork, BR0kEN, unstatu: Url::fromUri() should accept protocol-relative URLs
parent b8a61f20
......@@ -272,7 +272,11 @@ public static function fromUri($uri, $options = []) {
if ($uri_parts === FALSE) {
throw new \InvalidArgumentException("The URI '$uri' is malformed.");
}
if (empty($uri_parts['scheme'])) {
// We support protocol-relative URLs.
if (strpos($uri, '//') === 0) {
$uri_parts['scheme'] = '';
}
elseif (empty($uri_parts['scheme'])) {
throw new \InvalidArgumentException("The URI '$uri' is invalid. You must use a valid URI scheme.");
}
$uri_parts += ['path' => ''];
......
......@@ -82,6 +82,8 @@ public function providerFromUri() {
// [$uri, $is_external]
// An external URI.
['https://www.drupal.org', TRUE],
// A protocol-relative URL.
['//www.drupal.org', TRUE],
// An internal, unrouted, base-relative URI.
['base:robots.txt', FALSE],
// Base-relative URIs with special characters.
......@@ -114,7 +116,6 @@ public function providerFromInvalidUri() {
// Schemeless paths.
['test'],
['/test'],
['//test'],
// Schemeless path with a query string.
['foo?bar'],
// Only a query string.
......
......@@ -335,6 +335,18 @@ public function testGetUriForExternalUrl() {
$this->assertEquals('http://example.com/test', $url->getUri());
}
/**
* Tests the getUri() and isExternal() methods for protocol-relative URLs.
*
* @covers ::getUri
* @covers ::isExternal
*/
public function testGetUriForProtocolRelativeUrl() {
$url = Url::fromUri('//example.com/test');
$this->assertEquals('//example.com/test', $url->getUri());
$this->assertTrue($url->isExternal());
}
/**
* Tests the getInternalPath method().
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment