refactor(UrlResolver): extract app url resolution into AppRootUrl

fixes #1732
This commit is contained in:
Victor Berchet
2015-06-23 11:28:06 +02:00
parent 8c993dca03
commit 06aaa0c50e
7 changed files with 43 additions and 29 deletions
@@ -0,0 +1,18 @@
import {Injectable} from 'angular2/di';
import {isBlank} from 'angular2/src/facade/lang';
import {DOM} from 'angular2/src/dom/dom_adapter';
@Injectable()
export class AppRootUrl {
private _value: string;
get value() {
if (isBlank(this._value)) {
var a = DOM.createElement('a');
DOM.resolveAndSetHref(a, './', null);
this._value = DOM.getHref(a);
}
return this._value;
}
}
+1 -16
View File
@@ -13,17 +13,7 @@ export class UrlResolver {
}
/**
* Resolves the `url` given the `baseUrl`.
*
* ## When the `baseUrl` is null
*
* `url` is resolved in the context of the current document.
* If the document location is 'http://www.foo.com/base' and the `url` is 'path/to/here', the
* resolved url will be
* 'http://www.foo.com/base/path/to/here'
*
* ## When the `baseUrl` is not null
*
* Resolves the `url` given the `baseUrl`:
* - when the `url` is null, the `baseUrl` is returned,
* - due to a limitation in the process used to resolve urls (a HTMLLinkElement), `url` must not
* start with a `/`,
@@ -37,11 +27,6 @@ export class UrlResolver {
* @returns {string} the resolved URL
*/
resolve(baseUrl: string, url: string): string {
if (isBlank(baseUrl)) {
DOM.resolveAndSetHref(UrlResolver.a, url, null);
return DOM.getHref(UrlResolver.a);
}
if (isBlank(url) || url == '') return baseUrl;
if (url[0] == '/') {