From d1e5a65059b61dc8c6c492ab2fb5dbaa9e84c6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82e=CC=A8biowski-Owczarek?= Date: Thu, 11 Oct 2018 16:48:02 +0200 Subject: [PATCH] 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 --- src/ng/urlUtils.js | 14 +++++++++++++- test/ng/urlUtilsSpec.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ng/urlUtils.js b/src/ng/urlUtils.js index 149e14c707b3..7869fe361a48 100644 --- a/src/ng/urlUtils.js +++ b/src/ng/urlUtils.js @@ -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]'; /** * @@ -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 diff --git a/test/ng/urlUtilsSpec.js b/test/ng/urlUtilsSpec.js index ebd864076623..1da5605e27be 100644 --- a/test/ng/urlUtilsSpec.js +++ b/test/ng/urlUtilsSpec.js @@ -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'); + }); });