From b3acf663641fca0f7a966525a72845af7ec5fab2 Mon Sep 17 00:00:00 2001 From: Gary Katsevman Date: Thu, 22 Jul 2021 13:14:26 -0400 Subject: [PATCH] fix: remove IE8 url parsing workaround (#7334) --- src/js/utils/url.js | 28 ++++++++-------------------- test/unit/utils/url.test.js | 7 ++----- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/js/utils/url.js b/src/js/utils/url.js index 78f30e89ab..06d9a40007 100644 --- a/src/js/utils/url.js +++ b/src/js/utils/url.js @@ -41,30 +41,17 @@ import window from 'global/window'; * An object of url details */ export const parseUrl = function(url) { + // This entire method can be replace with URL once we are able to drop IE11 + const props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host']; // add the url to an anchor and let the browser parse the URL - let a = document.createElement('a'); + const a = document.createElement('a'); a.href = url; - // IE8 (and 9?) Fix - // ie8 doesn't parse the URL correctly until the anchor is actually - // added to the body, and an innerHTML is needed to trigger the parsing - const addToBody = (a.host === '' && a.protocol !== 'file:'); - let div; - - if (addToBody) { - div = document.createElement('div'); - div.innerHTML = ``; - a = div.firstChild; - // prevent the div from affecting layout - div.setAttribute('style', 'display:none; position:absolute;'); - document.body.appendChild(div); - } - // Copy the specific URL properties to a new object - // This is also needed for IE8 because the anchor loses its + // This is also needed for IE because the anchor loses its // properties when it's removed from the dom const details = {}; @@ -72,7 +59,7 @@ export const parseUrl = function(url) { details[props[i]] = a[props[i]]; } - // IE9 adds the port to the host property unlike everyone else. If + // IE adds the port to the host property unlike everyone else. If // a port identifier is added for standard ports, strip it. if (details.protocol === 'http:') { details.host = details.host.replace(/:80$/, ''); @@ -86,8 +73,9 @@ export const parseUrl = function(url) { details.protocol = window.location.protocol; } - if (addToBody) { - document.body.removeChild(div); + /* istanbul ignore if */ + if (!details.host) { + details.host = window.location.host; } return details; diff --git a/test/unit/utils/url.test.js b/test/unit/utils/url.test.js index 13cf5b15dd..e9ea06897b 100644 --- a/test/unit/utils/url.test.js +++ b/test/unit/utils/url.test.js @@ -5,12 +5,9 @@ import * as Url from '../../../src/js/utils/url.js'; QUnit.module('url'); QUnit.test('should parse the details of a url correctly', function(assert) { - assert.equal( - Url.parseUrl('#').protocol, - window.location.protocol, - 'parsed relative url protocol' - ); + assert.equal(Url.parseUrl('#').protocol, window.location.protocol, 'parsed relative url protocol'); assert.equal(Url.parseUrl('#').host, window.location.host, 'parsed relative url host'); + assert.equal(Url.parseUrl('#foo').hash, '#foo', 'parsed relative url hash'); assert.equal(Url.parseUrl('http://example.com').protocol, 'http:', 'parsed example url protocol'); assert.equal(Url.parseUrl('http://example.com').hostname, 'example.com', 'parsed example url hostname');