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);