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 = []) { ...@@ -272,7 +272,11 @@ public static function fromUri($uri, $options = []) {
if ($uri_parts === FALSE) { if ($uri_parts === FALSE) {
throw new \InvalidArgumentException("The URI '$uri' is malformed."); 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."); throw new \InvalidArgumentException("The URI '$uri' is invalid. You must use a valid URI scheme.");
} }
$uri_parts += ['path' => '']; $uri_parts += ['path' => ''];
......
...@@ -82,6 +82,8 @@ public function providerFromUri() { ...@@ -82,6 +82,8 @@ public function providerFromUri() {
// [$uri, $is_external] // [$uri, $is_external]
// An external URI. // An external URI.
['https://www.drupal.org', TRUE], ['https://www.drupal.org', TRUE],
// A protocol-relative URL.
['//www.drupal.org', TRUE],
// An internal, unrouted, base-relative URI. // An internal, unrouted, base-relative URI.
['base:robots.txt', FALSE], ['base:robots.txt', FALSE],
// Base-relative URIs with special characters. // Base-relative URIs with special characters.
...@@ -114,7 +116,6 @@ public function providerFromInvalidUri() { ...@@ -114,7 +116,6 @@ public function providerFromInvalidUri() {
// Schemeless paths. // Schemeless paths.
['test'], ['test'],
['/test'], ['/test'],
['//test'],
// Schemeless path with a query string. // Schemeless path with a query string.
['foo?bar'], ['foo?bar'],
// Only a query string. // Only a query string.
......
...@@ -335,6 +335,18 @@ public function testGetUriForExternalUrl() { ...@@ -335,6 +335,18 @@ public function testGetUriForExternalUrl() {
$this->assertEquals('http://example.com/test', $url->getUri()); $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(). * 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