Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): require explicit base href #3122

Merged
merged 2 commits into from Jul 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions modules/angular2/src/dom/browser_adapter.dart
Expand Up @@ -342,8 +342,11 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
return window.location;
}
getBaseHref() {
var uri = document.baseUri;
var baseUri = Uri.parse(uri);
var href = getBaseElementHref();
if (href == null) {
return null;
}
var baseUri = Uri.parse(href);
return baseUri.path;
}
String getUserAgent() {
Expand All @@ -360,3 +363,15 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
js.context[name] = value;
}
}


var baseElement = null;
String getBaseElementHref() {
if (baseElement == null) {
baseElement = document.querySelector('base');
if (baseElement == null) {
return null;
}
}
return baseElement.getAttribute('href');
}
20 changes: 19 additions & 1 deletion modules/angular2/src/dom/browser_adapter.ts
Expand Up @@ -263,14 +263,32 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
getHistory(): History { return window.history; }
getLocation(): Location { return window.location; }
getBaseHref(): string { return relativePath(document.baseURI); }
getBaseHref(): string {
var href = getBaseElementHref();
if (isBlank(href)) {
return null;
}
return relativePath(href);
}
getUserAgent(): string { return window.navigator.userAgent; }
setData(element, name: string, value: string) { element.dataset[name] = value; }
getData(element, name: string): string { return element.dataset[name]; }
// TODO(tbosch): move this into a separate environment class once we have it
setGlobalVar(name: string, value: any) { global[name] = value; }
}


var baseElement = null;
function getBaseElementHref(): string {
if (isBlank(baseElement)) {
baseElement = document.querySelector('base');
if (isBlank(baseElement)) {
return null;
}
}
return baseElement.attr('href');
}

// based on urlUtils.js in AngularJS 1
var urlParsingNode = null;
function relativePath(url): string {
Expand Down
11 changes: 9 additions & 2 deletions modules/angular2/src/router/location.ts
@@ -1,6 +1,7 @@
import {LocationStrategy} from './location_strategy';
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {BaseException, isBlank} from 'angular2/src/facade/lang';
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/di';

export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locationHrefToken'));
Expand All @@ -22,8 +23,14 @@ export class Location {

constructor(public _platformStrategy: LocationStrategy,
@Optional() @Inject(appBaseHrefToken) href?: string) {
this._baseHref = stripTrailingSlash(
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
var browserBaseHref = isPresent(href) ? href : this._platformStrategy.getBaseHref();

if (isBlank(browserBaseHref)) {
throw new BaseException(
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
}

this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
this._platformStrategy.onPopState((_) => this._onPopState(_));
}

Expand Down
8 changes: 8 additions & 0 deletions modules/angular2/test/router/location_spec.ts
Expand Up @@ -75,5 +75,13 @@ export function main() {
location.go('user/btford');
expect(locationStrategy.path()).toEqual('/my/custom/href/user/btford');
});

it('should throw when no base href is provided', () => {
var locationStrategy = new MockLocationStrategy();
locationStrategy.internalBaseHref = null;
expect(() => new Location(locationStrategy))
.toThrowError(
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
});
});
}