Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(urlUtils): make IPv6 URL's hostname wrapped in square brackets in…
Browse files Browse the repository at this point in the history
… IE/Edge

IE 9-11 and Edge 16-17 (fixed in 18 Preview) incorrectly don't wrap IPv6
addresses' hostnames in square brackets when parsed out of an anchor element.

Fixes #16692
Closes #16715
  • Loading branch information
mgol committed Oct 18, 2018
1 parent b27080d commit 0e1bd78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ng/urlUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ var urlParsingNode = window.document.createElement('a');
var originUrl = urlResolve(window.location.href);
var baseUrlParsingNode;

urlParsingNode.href = 'http://[::1]';

// Support: IE 9-11 only, Edge 16-17 only (fixed in 18 Preview)
// IE/Edge don't wrap IPv6 addresses' hostnames in square brackets
// when parsed out of an anchor element.
var ipv6InBrackets = urlParsingNode.hostname === '[::1]';

/**
*
Expand Down Expand Up @@ -72,13 +78,19 @@ function urlResolve(url) {

urlParsingNode.setAttribute('href', href);

var hostname = urlParsingNode.hostname;

if (!ipv6InBrackets && hostname.indexOf(':') > -1) {
hostname = '[' + hostname + ']';
}

return {
href: urlParsingNode.href,
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
host: urlParsingNode.host,
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
hostname: urlParsingNode.hostname,
hostname: hostname,
port: urlParsingNode.port,
pathname: (urlParsingNode.pathname.charAt(0) === '/')
? urlParsingNode.pathname
Expand Down
13 changes: 13 additions & 0 deletions test/ng/urlUtilsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ describe('urlUtils', function() {
var parsed = urlResolve('/');
expect(parsed.pathname).toBe('/');
});

it('should return an IPv6 hostname wrapped in brackets', function() {
// Support: IE 9-11 only, Edge 16-17 only (fixed in 18 Preview)
// IE/Edge don't wrap IPv6 addresses' hostnames in square brackets
// when parsed out of an anchor element.
var parsed = urlResolve('http://[::1]/');
expect(parsed.hostname).toBe('[::1]');
});

it('should not put the domain in brackets for the hostname field', function() {
var parsed = urlResolve('https://google.com/');
expect(parsed.hostname).toBe('google.com');
});
});


Expand Down

0 comments on commit 0e1bd78

Please sign in to comment.