Skip to content

Commit

Permalink
fix(browserLocation): Use location.pathname (not href) or '/' when no…
Browse files Browse the repository at this point in the history
… base tag found
  • Loading branch information
christopherthielen committed Dec 20, 2017
1 parent bfa5755 commit db461d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/vanilla/browserLocationConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class BrowserLocationConfig implements LocationConfig {

applyDocumentBaseHref() {
let baseTag: HTMLBaseElement = document.getElementsByTagName("base")[0];
return this._baseHref = baseTag ? baseTag.href.substr(location.origin.length) : location.href;
return this._baseHref = baseTag ? baseTag.href.substr(location.origin.length) : location.pathname || '/';
}

dispose() {}
Expand Down
17 changes: 9 additions & 8 deletions test/vanilla.browserLocationConfigSpec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { UIRouter } from "../src/router";
import { UrlService } from "../src/url/urlService";
import { stripLastPathElement } from '../src/common';
import { UIRouter } from '../src/router';
import { UrlService } from '../src/url/urlService';
import * as vanilla from "../src/vanilla";
import { UrlMatcherFactory } from "../src/url/urlMatcherFactory";
import { UrlMatcherFactory } from '../src/url/urlMatcherFactory';
import { BrowserLocationConfig } from '../src/vanilla';
import { resetBrowserUrl } from './_testUtils';

describe('BrowserLocationConfig implementation', () => {
afterAll(() => resetBrowserUrl())
afterAll(() => resetBrowserUrl());

let router: UIRouter;
let $url: UrlService;
Expand Down Expand Up @@ -62,14 +63,14 @@ describe('BrowserLocationConfig implementation', () => {
expect(router.urlService.config.html5Mode()).toBe(true);
let stub = spyOn(service._history, 'pushState');
router.urlRouter.push($umf.compile('/hello/:name'), { name: 'world' }, {});
expect(stub.calls.first().args[2]).toBe('/hello/world');
expect(stub.calls.first().args[2]).toBe(stripLastPathElement($url.config.baseHref()) + '/hello/world');
});

it('uses history.replaceState when setting a url with replace', () => {
let service = mockPushState(router);
let stub = spyOn(service._history, 'replaceState');
router.urlRouter.push($umf.compile('/hello/:name'), { name: 'world' }, { replace: true });
expect(stub.calls.first().args[2]).toBe('/hello/world');
expect(stub.calls.first().args[2]).toBe(stripLastPathElement($url.config.baseHref()) + '/hello/world');
});

it('returns the correct url query', async(done) => {
Expand Down Expand Up @@ -127,9 +128,9 @@ describe('BrowserLocationConfig implementation', () => {
expect(blc.baseHref()).toBe('/base');
});

it('uses location.href if <base> is not present', () => {
it('uses location.pathname if <base> is not present', () => {
const blc = new BrowserLocationConfig();
expect(blc.baseHref()).toBe(location.href);
expect(blc.baseHref()).toBe(location.pathname);
});
});

Expand Down

0 comments on commit db461d6

Please sign in to comment.