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

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
];
}

View File

@ -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;
}

View File

@ -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;
}
}

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] == '/') {

View File

@ -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,

View File

@ -34,6 +34,7 @@ import {
import {ProtoViewFactory} from 'angular2/src/core/compiler/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';
// TODO(tbosch): Spys don't support named modules...
import {RenderCompiler} from 'angular2/src/render/api';
@ -68,7 +69,7 @@ export function main() {
protoViewFactory = new FakeProtoViewFactory(protoViewFactoryResults);
return new Compiler(directiveResolver, new CompilerCache(), tplResolver, cmpUrlMapper,
urlResolver, renderCompiler, protoViewFactory);
urlResolver, renderCompiler, protoViewFactory, new FakeAppRootUrl());
}
describe('serialize template', () => {
@ -567,13 +568,11 @@ class SpyRenderCompiler extends SpyObject {
class FakeUrlResolver extends UrlResolver {
constructor() { super(); }
resolve(baseUrl: string, url: string): string {
if (baseUrl === null && url == './') {
return 'http://www.app.com';
}
resolve(baseUrl: string, url: string): string { return baseUrl + url; }
}
return baseUrl + url;
}
class FakeAppRootUrl extends AppRootUrl {
get value() { return 'http://www.app.com'; }
}

View File

@ -18,6 +18,8 @@ import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';
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 {ComponentUrlMapper} from 'angular2/src/core/compiler/component_url_mapper';
import {reflector} from 'angular2/src/reflection/reflection';
@ -42,9 +44,9 @@ export function main() {
var shadowDomStrategy = new NativeShadowDomStrategy(styleInliner, styleUrlResolver);
var renderCompiler = new rc.DefaultDomCompiler(new Parser(new Lexer()), shadowDomStrategy,
new TemplateLoader(null, urlResolver));
var compiler =
new Compiler(reader, cache, templateResolver, new ComponentUrlMapper(), urlResolver,
renderCompiler, new ProtoViewFactory(new DynamicChangeDetection(null)));
var compiler = new Compiler(
reader, cache, templateResolver, new ComponentUrlMapper(), urlResolver, renderCompiler,
new ProtoViewFactory(new DynamicChangeDetection(null)), new FakeAppRootUrl());
function measureWrapper(func, desc) {
return function() {
@ -164,3 +166,7 @@ class BenchmarkComponentNoBindings {
})
class BenchmarkComponentWithBindings {
}
class FakeAppRootUrl extends AppRootUrl {
get value() { return ''; }
}