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

Commit b4e409b

Browse files
committed
fix(urlUtils): make IPv6 URL's hostname wrapped in square brackets in 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
1 parent 3e38032 commit b4e409b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ng/urlUtils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ var urlParsingNode = window.document.createElement('a');
1010
var originUrl = urlResolve(window.location.href);
1111
var baseUrlParsingNode;
1212

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

1420
/**
1521
*
@@ -72,13 +78,19 @@ function urlResolve(url) {
7278

7379
urlParsingNode.setAttribute('href', href);
7480

81+
var hostname = urlParsingNode.hostname;
82+
83+
if (!ipv6InBrackets && hostname.indexOf(':') > -1) {
84+
hostname = '[' + hostname + ']';
85+
}
86+
7587
return {
7688
href: urlParsingNode.href,
7789
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
7890
host: urlParsingNode.host,
7991
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
8092
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
81-
hostname: urlParsingNode.hostname,
93+
hostname: hostname,
8294
port: urlParsingNode.port,
8395
pathname: (urlParsingNode.pathname.charAt(0) === '/')
8496
? urlParsingNode.pathname

test/ng/urlUtilsSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ describe('urlUtils', function() {
3131
var parsed = urlResolve('/');
3232
expect(parsed.pathname).toBe('/');
3333
});
34+
35+
it('should return an IPv6 hostname wrapped in brackets', function() {
36+
// Support: IE 9-11 only, Edge 16-17 only (fixed in 18 Preview)
37+
// IE/Edge don't wrap IPv6 addresses' hostnames in square brackets
38+
// when parsed out of an anchor element.
39+
var parsed = urlResolve('http://[::1]/');
40+
expect(parsed.hostname).toBe('[::1]');
41+
});
42+
43+
it('should not put the domain in brackets for the hostname field', function() {
44+
var parsed = urlResolve('https://google.com/');
45+
expect(parsed.hostname).toBe('google.com');
46+
});
3447
});
3548

3649

0 commit comments

Comments
 (0)