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

fix(urlUtils): make IPv6 URL's hostname wrapped in square brackets in IE/Edge #16715

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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