diff --git a/modules/angular2/src/core/application.ts b/modules/angular2/src/core/application.ts index fcf6c8d99c..12b827d127 100644 --- a/modules/angular2/src/core/application.ts +++ b/modules/angular2/src/core/application.ts @@ -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> { StyleUrlResolver, StyleInliner, DynamicComponentLoader, - Testability + Testability, + AppRootUrl ]; } diff --git a/modules/angular2/src/core/compiler/compiler.ts b/modules/angular2/src/core/compiler/compiler.ts index bd6ecafb3d..5b1096a85c 100644 --- a/modules/angular2/src/core/compiler/compiler.ts +++ b/modules/angular2/src/core/compiler/compiler.ts @@ -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; } diff --git a/modules/angular2/src/services/app_root_url.ts b/modules/angular2/src/services/app_root_url.ts new file mode 100644 index 0000000000..677e6d7333 --- /dev/null +++ b/modules/angular2/src/services/app_root_url.ts @@ -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; + } +} diff --git a/modules/angular2/src/services/url_resolver.ts b/modules/angular2/src/services/url_resolver.ts index d14dfdb34b..6457cb4ab7 100644 --- a/modules/angular2/src/services/url_resolver.ts +++ b/modules/angular2/src/services/url_resolver.ts @@ -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] == '/') { diff --git a/modules/angular2/src/test_lib/test_injector.ts b/modules/angular2/src/test_lib/test_injector.ts index 6657ca0e32..7b5efe247e 100644 --- a/modules/angular2/src/test_lib/test_injector.ts +++ b/modules/angular2/src/test_lib/test_injector.ts @@ -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, diff --git a/modules/angular2/test/core/compiler/compiler_spec.ts b/modules/angular2/test/core/compiler/compiler_spec.ts index fafc5c2c0f..293fdf2454 100644 --- a/modules/angular2/test/core/compiler/compiler_spec.ts +++ b/modules/angular2/test/core/compiler/compiler_spec.ts @@ -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'; } } diff --git a/modules/benchmarks/src/compiler/compiler_benchmark.ts b/modules/benchmarks/src/compiler/compiler_benchmark.ts index bc176a6a08..eb749dffaf 100644 --- a/modules/benchmarks/src/compiler/compiler_benchmark.ts +++ b/modules/benchmarks/src/compiler/compiler_benchmark.ts @@ -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 ''; } +}