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
+3 -1
View File
@@ -44,6 +44,7 @@ import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mappe
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
import {
ComponentRef,
DynamicComponentLoader
@@ -133,7 +134,8 @@ function _injectorBindings(appComponentType): List<Type | Binding | List<any>> {
StyleUrlResolver,
StyleInliner,
DynamicComponentLoader,
Testability
Testability,
AppRootUrl
];
}
@@ -23,6 +23,7 @@ import {View} from '../annotations_impl/view';
import {ComponentUrlMapper} from './component_url_mapper';
import {ProtoViewFactory} from './proto_view_factory';
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
import * as renderApi from 'angular2/src/render/api';
@@ -74,14 +75,15 @@ export class Compiler {
constructor(reader: DirectiveResolver, cache: CompilerCache, templateResolver: TemplateResolver,
componentUrlMapper: ComponentUrlMapper, urlResolver: UrlResolver,
render: renderApi.RenderCompiler, protoViewFactory: ProtoViewFactory) {
render: renderApi.RenderCompiler, protoViewFactory: ProtoViewFactory,
appUrl: AppRootUrl) {
this._reader = reader;
this._compilerCache = cache;
this._compiling = new Map();
this._templateResolver = templateResolver;
this._componentUrlMapper = componentUrlMapper;
this._urlResolver = urlResolver;
this._appUrl = urlResolver.resolve(null, './');
this._appUrl = appUrl.value;
this._render = render;
this._protoViewFactory = protoViewFactory;
}
@@ -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] == '/') {
@@ -22,6 +22,7 @@ import {
import {XHR} from 'angular2/src/render/xhr';
import {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
import {NgZone} from 'angular2/src/core/zone/ng_zone';
@@ -115,6 +116,7 @@ function _getAppBindings() {
bind(XHR).toClass(MockXHR),
ComponentUrlMapper,
UrlResolver,
AppRootUrl,
StyleUrlResolver,
StyleInliner,
TestBed,