Skip to content

Commit b3acf66

Browse files
authoredJul 22, 2021
fix: remove IE8 url parsing workaround (#7334)
1 parent bba6e17 commit b3acf66

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed
 

‎src/js/utils/url.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,25 @@ import window from 'global/window';
4141
* An object of url details
4242
*/
4343
export const parseUrl = function(url) {
44+
// This entire method can be replace with URL once we are able to drop IE11
45+
4446
const props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
4547

4648
// add the url to an anchor and let the browser parse the URL
47-
let a = document.createElement('a');
49+
const a = document.createElement('a');
4850

4951
a.href = url;
5052

51-
// IE8 (and 9?) Fix
52-
// ie8 doesn't parse the URL correctly until the anchor is actually
53-
// added to the body, and an innerHTML is needed to trigger the parsing
54-
const addToBody = (a.host === '' && a.protocol !== 'file:');
55-
let div;
56-
57-
if (addToBody) {
58-
div = document.createElement('div');
59-
div.innerHTML = `<a href="${url}"></a>`;
60-
a = div.firstChild;
61-
// prevent the div from affecting layout
62-
div.setAttribute('style', 'display:none; position:absolute;');
63-
document.body.appendChild(div);
64-
}
65-
6653
// Copy the specific URL properties to a new object
67-
// This is also needed for IE8 because the anchor loses its
54+
// This is also needed for IE because the anchor loses its
6855
// properties when it's removed from the dom
6956
const details = {};
7057

7158
for (let i = 0; i < props.length; i++) {
7259
details[props[i]] = a[props[i]];
7360
}
7461

75-
// IE9 adds the port to the host property unlike everyone else. If
62+
// IE adds the port to the host property unlike everyone else. If
7663
// a port identifier is added for standard ports, strip it.
7764
if (details.protocol === 'http:') {
7865
details.host = details.host.replace(/:80$/, '');
@@ -86,8 +73,9 @@ export const parseUrl = function(url) {
8673
details.protocol = window.location.protocol;
8774
}
8875

89-
if (addToBody) {
90-
document.body.removeChild(div);
76+
/* istanbul ignore if */
77+
if (!details.host) {
78+
details.host = window.location.host;
9179
}
9280

9381
return details;

‎test/unit/utils/url.test.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ import * as Url from '../../../src/js/utils/url.js';
55

66
QUnit.module('url');
77
QUnit.test('should parse the details of a url correctly', function(assert) {
8-
assert.equal(
9-
Url.parseUrl('#').protocol,
10-
window.location.protocol,
11-
'parsed relative url protocol'
12-
);
8+
assert.equal(Url.parseUrl('#').protocol, window.location.protocol, 'parsed relative url protocol');
139
assert.equal(Url.parseUrl('#').host, window.location.host, 'parsed relative url host');
10+
assert.equal(Url.parseUrl('#foo').hash, '#foo', 'parsed relative url hash');
1411

1512
assert.equal(Url.parseUrl('http://example.com').protocol, 'http:', 'parsed example url protocol');
1613
assert.equal(Url.parseUrl('http://example.com').hostname, 'example.com', 'parsed example url hostname');

0 commit comments

Comments
 (0)
Please sign in to comment.