From f11be4d72391ef74847278c998241afb5780f031 Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Mon, 28 Jan 2019 13:55:38 -0800 Subject: [PATCH] fix(vanilla): Fix baseHref parsing with chrome-extension:// urls Closes #304 --- src/vanilla/browserLocationConfig.ts | 2 +- test/vanilla.browserLocationConfigSpec.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/vanilla/browserLocationConfig.ts b/src/vanilla/browserLocationConfig.ts index 4fd1ed89..9be08d2d 100644 --- a/src/vanilla/browserLocationConfig.ts +++ b/src/vanilla/browserLocationConfig.ts @@ -44,7 +44,7 @@ export class BrowserLocationConfig implements LocationConfig { private getBaseHref() { const baseTag: HTMLBaseElement = document.getElementsByTagName('base')[0]; if (baseTag && baseTag.href) { - return baseTag.href.replace(/^(https?:)?\/\/[^/]*/, ''); + return baseTag.href.replace(/^([^/:]*:)?\/\/[^/]*/, ''); } return this._isHtml5 ? '/' : location.pathname || '/'; diff --git a/test/vanilla.browserLocationConfigSpec.ts b/test/vanilla.browserLocationConfigSpec.ts index 9d798e7a..76512351 100644 --- a/test/vanilla.browserLocationConfigSpec.ts +++ b/test/vanilla.browserLocationConfigSpec.ts @@ -128,6 +128,24 @@ describe('BrowserLocationConfig implementation', () => { expect(blc.baseHref()).toBe('/base'); }); + it('strips off the http origin', () => { + applyBaseTag('http://localhost/base'); + const blc = new BrowserLocationConfig(); + expect(blc.baseHref()).toBe('/base'); + }); + + it('strips off the https origin', () => { + applyBaseTag('https://localhost/base'); + const blc = new BrowserLocationConfig(); + expect(blc.baseHref()).toBe('/base'); + }); + + it('supports chrome-extension:// urls', () => { + applyBaseTag('chrome-extension://cckppebifaokhmbkciccdindfbmacjcj/baseHref/'); + const blc = new BrowserLocationConfig(); + expect(blc.baseHref()).toBe('/baseHref/'); + }); + it('uses location.pathname if is not present', () => { const blc = new BrowserLocationConfig(); expect(blc.baseHref()).toBe(location.pathname);