From 46b7aad9276f389982418b4a3dde527bb7b6608c Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 29 Jul 2020 09:45:26 -0400 Subject: [PATCH] [BUGFIX lts] Prevent `` from erroring in HistoryLocation It is perfectly valid to have a `` element without an `href` attribute but the code previously assumed that if a `` was present that it **must** contain an `href`. Specifically, prior to this change if you had a `` like: ```html ``` 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 `` without an `href`. --- .../routing/lib/location/history_location.ts | 2 +- .../tests/location/history_location_test.js | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/@ember/-internals/routing/lib/location/history_location.ts b/packages/@ember/-internals/routing/lib/location/history_location.ts index 9fd5206766d..a368f13b1b7 100644 --- a/packages/@ember/-internals/routing/lib/location/history_location.ts +++ b/packages/@ember/-internals/routing/lib/location/history_location.ts @@ -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'); } diff --git a/packages/@ember/-internals/routing/tests/location/history_location_test.js b/packages/@ember/-internals/routing/tests/location/history_location_test.js index 988e4b3660f..6efad8dd768 100644 --- a/packages/@ember/-internals/routing/tests/location/history_location_test.js +++ b/packages/@ember/-internals/routing/tests/location/history_location_test.js @@ -97,6 +97,42 @@ moduleFor( location.initState(); } + ['@test 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 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);