fix(router): respect LocationStrategy when constructing hrefs in links
Note that this introduces more behavior for LocationStrategy which needs yet more refactoring to test. See #4935. Closes #4333
This commit is contained in:
@ -21,10 +21,15 @@ export class SpyLocation implements Location {
|
||||
|
||||
simulateUrlPop(pathname: string) { ObservableWrapper.callNext(this._subject, {'url': pathname}); }
|
||||
|
||||
normalizeAbsolutely(url: string): string { return this._baseHref + url; }
|
||||
prepareExternalUrl(url: string): string {
|
||||
if (url.length > 0 && !url.startsWith('/')) {
|
||||
url = '/' + url;
|
||||
}
|
||||
return this._baseHref + url;
|
||||
}
|
||||
|
||||
go(path: string, query: string = '') {
|
||||
path = this.normalizeAbsolutely(path);
|
||||
path = this.prepareExternalUrl(path);
|
||||
if (this._path == path && this._query == query) {
|
||||
return;
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ export class MockLocationStrategy extends LocationStrategy {
|
||||
|
||||
path(): string { return this.internalPath; }
|
||||
|
||||
prepareExternalUrl(internal: string): string { return internal; }
|
||||
|
||||
simulateUrlPop(pathname: string): void {
|
||||
ObservableWrapper.callNext(this._subject, {'url': pathname});
|
||||
}
|
||||
|
@ -65,12 +65,16 @@ export class HashLocationStrategy extends LocationStrategy {
|
||||
normalizeQueryParams(this._location.search);
|
||||
}
|
||||
|
||||
prepareExternalUrl(internal: string): string {
|
||||
return internal.length > 0 ? ('#' + internal) : internal;
|
||||
}
|
||||
|
||||
pushState(state: any, title: string, path: string, queryParams: string) {
|
||||
var url = path + normalizeQueryParams(queryParams);
|
||||
if (url.length == 0) {
|
||||
url = this._location.pathname;
|
||||
} else {
|
||||
url = '#' + url;
|
||||
url = this.prepareExternalUrl(url);
|
||||
}
|
||||
this._history.pushState(state, title, url);
|
||||
}
|
||||
|
@ -103,22 +103,25 @@ export class Location {
|
||||
path(): string { return this.normalize(this.platformStrategy.path()); }
|
||||
|
||||
/**
|
||||
* Given a string representing a URL, returns the normalized URL path.
|
||||
* Given a string representing a URL, returns the normalized URL path without leading or
|
||||
* trailing slashes
|
||||
*/
|
||||
normalize(url: string): string {
|
||||
return stripTrailingSlash(_stripBaseHref(this._baseHref, stripIndexHtml(url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a string representing a URL, returns the normalized URL path.
|
||||
* Given a string representing a URL, returns the platform-specific external URL path.
|
||||
* If the given URL doesn't begin with a leading slash (`'/'`), this method adds one
|
||||
* before normalizing.
|
||||
* before normalizing. This method will also add a hash if `HashLocationStrategy` is
|
||||
* used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use.
|
||||
*/
|
||||
normalizeAbsolutely(url: string): string {
|
||||
prepareExternalUrl(url: string): string {
|
||||
if (!url.startsWith('/')) {
|
||||
url = '/' + url;
|
||||
}
|
||||
return stripTrailingSlash(_addBaseHref(this._baseHref, url));
|
||||
return this.platformStrategy.prepareExternalUrl(
|
||||
stripTrailingSlash(_addBaseHref(this._baseHref, url)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,8 +129,7 @@ export class Location {
|
||||
* new item onto the platform's history.
|
||||
*/
|
||||
go(path: string, query: string = ''): void {
|
||||
var absolutePath = this.normalizeAbsolutely(path);
|
||||
this.platformStrategy.pushState(null, '', absolutePath, query);
|
||||
this.platformStrategy.pushState(null, '', path, query);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,6 +16,7 @@
|
||||
*/
|
||||
export abstract class LocationStrategy {
|
||||
abstract path(): string;
|
||||
abstract prepareExternalUrl(internal: string): string;
|
||||
abstract pushState(state: any, title: string, url: string, queryParams: string): void;
|
||||
abstract forward(): void;
|
||||
abstract back(): void;
|
||||
|
@ -67,6 +67,8 @@ export class PathLocationStrategy extends LocationStrategy {
|
||||
|
||||
getBaseHref(): string { return this._baseHref; }
|
||||
|
||||
prepareExternalUrl(internal: string): string { return this._baseHref + internal; }
|
||||
|
||||
path(): string { return this._location.pathname + normalizeQueryParams(this._location.search); }
|
||||
|
||||
pushState(state: any, title: string, url: string, queryParams: string) {
|
||||
|
@ -60,9 +60,8 @@ export class RouterLink {
|
||||
this._routeParams = changes;
|
||||
this._navigationInstruction = this._router.generate(this._routeParams);
|
||||
|
||||
// TODO: is this the right spot for this?
|
||||
var navigationHref = '/' + stringifyInstruction(this._navigationInstruction);
|
||||
this.visibleHref = this._location.normalizeAbsolutely(navigationHref);
|
||||
var navigationHref = stringifyInstruction(this._navigationInstruction);
|
||||
this.visibleHref = this._location.prepareExternalUrl(navigationHref);
|
||||
}
|
||||
|
||||
onClick(): boolean {
|
||||
|
Reference in New Issue
Block a user