Skip to content

Commit

Permalink
[BUGFIX lts] Prevent <base target="_parent"> from erroring in Histo…
Browse files Browse the repository at this point in the history
…ryLocation

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`.
  • Loading branch information
rwjblue committed Jul 29, 2020
1 parent 1b8e122 commit 46b7aad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class HistoryLocation extends EmberObject implements EmberLocatio

let base = document.querySelector('base');
let baseURL: string | null = '';
if (base) {
if (base !== null && base.hasAttribute('href')) {
baseURL = base.getAttribute('href');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ moduleFor(
location.initState();
}

['@test <base> with href sets `baseURL`'](assert) {
assert.expect(1);

let base = document.createElement('base');
base.setAttribute('href', '/foo/');

document.head.appendChild(base);

try {
createLocation();
location.initState();

assert.strictEqual(location.get('baseURL'), '/foo/');
} finally {
document.head.removeChild(base);
}
}

['@test <base> without href is ignored'](assert) {
assert.expect(1);

let base = document.createElement('base');
base.setAttribute('target', '_parent');

document.head.appendChild(base);

try {
createLocation();
location.initState();

assert.strictEqual(location.get('baseURL'), '');
} finally {
document.head.removeChild(base);
}
}

['@test base URL is removed when retrieving the current pathname'](assert) {
assert.expect(1);

Expand Down

0 comments on commit 46b7aad

Please sign in to comment.