Skip to content

Commit 632d1a8

Browse files
author
Robert Jackson
committed
[BUGFIX lts] Prevent <base target="_parent"> from erroring in HistoryLocation
It is perfectly valid to have a `<base>` element without an `href` attribute but the code previously assumed that if a `<base>` was present that it **must** contain an `href`. Specifically, prior to this change if you had a `<base>` like: ```html <base target="_parent"> ``` You would get the following error: ``` Uncaught TypeError: Cannot read property 'replace' of null ``` See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base for more details on usage of `<base>` without an `href`. (cherry picked from commit 46b7aad)
1 parent 435e84e commit 632d1a8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/@ember/-internals/routing/lib/location/history_location.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class HistoryLocation extends EmberObject implements EmberLocatio
7777

7878
let base = document.querySelector('base');
7979
let baseURL: string | null = '';
80-
if (base) {
80+
if (base !== null && base.hasAttribute('href')) {
8181
baseURL = base.getAttribute('href');
8282
}
8383

packages/@ember/-internals/routing/tests/location/history_location_test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,42 @@ moduleFor(
9797
location.initState();
9898
}
9999

100+
['@test <base> with href sets `baseURL`'](assert) {
101+
assert.expect(1);
102+
103+
let base = document.createElement('base');
104+
base.setAttribute('href', '/foo/');
105+
106+
document.head.appendChild(base);
107+
108+
try {
109+
createLocation();
110+
location.initState();
111+
112+
assert.strictEqual(location.get('baseURL'), '/foo/');
113+
} finally {
114+
document.head.removeChild(base);
115+
}
116+
}
117+
118+
['@test <base> without href is ignored'](assert) {
119+
assert.expect(1);
120+
121+
let base = document.createElement('base');
122+
base.setAttribute('target', '_parent');
123+
124+
document.head.appendChild(base);
125+
126+
try {
127+
createLocation();
128+
location.initState();
129+
130+
assert.strictEqual(location.get('baseURL'), '');
131+
} finally {
132+
document.head.removeChild(base);
133+
}
134+
}
135+
100136
['@test base URL is removed when retrieving the current pathname'](assert) {
101137
assert.expect(1);
102138

0 commit comments

Comments
 (0)