Skip to content

Commit

Permalink
fix: remove IE8 url parsing workaround (#7334)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatsev committed Jul 22, 2021
1 parent bba6e17 commit b3acf66
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
28 changes: 8 additions & 20 deletions src/js/utils/url.js
Expand Up @@ -41,38 +41,25 @@ 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 href="${url}"></a>`;
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 = {};

for (let i = 0; i < props.length; i++) {
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$/, '');
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions test/unit/utils/url.test.js
Expand Up @@ -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');
Expand Down

0 comments on commit b3acf66

Please sign in to comment.