fix(url_resolver): in Dart make package urls relative to AppRootUrl

This commit is contained in:
yjbanov 2015-07-24 14:49:16 -07:00
parent c2bbda02a1
commit 469afda53e
18 changed files with 58 additions and 33 deletions

View File

@ -1,9 +1,15 @@
library angular2.src.services.url_resolver;
import 'package:angular2/di.dart' show Injectable;
import 'package:angular2/src/services/app_root_url.dart' show AppRootUrl;
@Injectable()
class UrlResolver {
final AppRootUrl _appRootUrl;
UrlResolver(this._appRootUrl);
/**
* Resolves the `url` given the `baseUrl`:
* - when the `url` is null, the `baseUrl` is returned,
@ -20,7 +26,8 @@ class UrlResolver {
Uri uri = Uri.parse(url);
if (uri.scheme == 'package') {
return '/packages/${uri.path}';
var maybeSlash = _appRootUrl.value.endsWith('/') ? '' : '/';
return '${_appRootUrl.value}${maybeSlash}packages/${uri.path}';
}
if (uri.isAbsolute) return uri.toString();

View File

@ -7,6 +7,7 @@ import {
normalizeBlank
} from 'angular2/src/facade/lang';
import {ListWrapper} from 'angular2/src/facade/collection';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
/**
* Used by the {@link Compiler} when resolving HTML and CSS template URLs.
@ -17,6 +18,8 @@ import {ListWrapper} from 'angular2/src/facade/collection';
*/
@Injectable()
export class UrlResolver {
constructor(_: AppRootUrl) {}
/**
* Resolves the `url` given the `baseUrl`:
* - when the `url` is null, the `baseUrl` is returned,

View File

@ -12,6 +12,7 @@ import 'package:angular2/src/render/dom/compiler/style_url_resolver.dart';
import 'package:angular2/src/render/dom/compiler/view_loader.dart';
import 'package:angular2/src/render/xhr.dart' show XHR;
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/services/app_root_url.dart';
import 'package:angular2/src/services/url_resolver.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/xhr_impl.dart';
@ -85,7 +86,7 @@ class _TemplateExtractor {
_TemplateExtractor(XHR xhr)
: _factory = new CompileStepFactory(new ng.Parser(new ng.Lexer())) {
var urlResolver = new UrlResolver();
var urlResolver = new UrlResolver(new AppRootUrl(''));
var styleUrlResolver = new StyleUrlResolver(urlResolver);
var styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver);

View File

@ -48,7 +48,7 @@ export function main() {
function createCompiler(renderCompileResults:
List<renderApi.ProtoViewDto | Promise<renderApi.ProtoViewDto>>,
protoViewFactoryResults: List<AppProtoView>) {
var urlResolver = new UrlResolver();
var urlResolver = new UrlResolver(new AppRootUrl(""));
renderCompileRequests = [];
renderCompileResults = ListWrapper.clone(renderCompileResults);
renderCompiler.spy('compile').andCallFake((view) => {
@ -399,9 +399,9 @@ export function main() {
var reader: any = new SpyDirectiveResolver();
// create the compiler
var compiler =
new Compiler(reader, cache, tplResolver, cmpUrlMapper, new UrlResolver(),
renderCompiler, protoViewFactory, new AppRootUrl("http://www.app.com"));
var compiler = new Compiler(reader, cache, tplResolver, cmpUrlMapper,
new UrlResolver(new AppRootUrl("")), renderCompiler,
protoViewFactory, new AppRootUrl("http://www.app.com"));
compiler.compileInHost(MainComponent)
.then((protoViewRef) => {
// the test should have failed if the resolver was called, so we're good

View File

@ -2,12 +2,14 @@ import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular
import {StyleUrlResolver} from 'angular2/src/render/dom/compiler/style_url_resolver';
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
export function main() {
describe('StyleUrlResolver', () => {
let styleUrlResolver;
beforeEach(() => { styleUrlResolver = new StyleUrlResolver(new UrlResolver()); });
beforeEach(
() => { styleUrlResolver = new StyleUrlResolver(new UrlResolver(new AppRootUrl(""))); });
it('should resolve "url()" urls', () => {
var css = `

View File

@ -15,6 +15,7 @@ import {ViewLoader} from 'angular2/src/render/dom/compiler/view_loader';
import {StyleInliner} from 'angular2/src/render/dom/compiler/style_inliner';
import {StyleUrlResolver} from 'angular2/src/render/dom/compiler/style_url_resolver';
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
import {ViewDefinition} from 'angular2/src/render/api';
import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
@ -28,7 +29,7 @@ export function main() {
beforeEach(() => {
xhr = new MockXHR();
urlResolver = new UrlResolver();
urlResolver = new UrlResolver(new AppRootUrl(''));
styleUrlResolver = new StyleUrlResolver(urlResolver);
let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver);
loader = new ViewLoader(xhr, styleInliner, styleUrlResolver);

View File

@ -1,9 +1,21 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
import {
describe,
it,
expect,
beforeEach,
ddescribe,
iit,
xit,
el,
IS_DARTIUM
} from 'angular2/test_lib';
import {UrlResolver} from 'angular2/src/services/url_resolver';
import {AppRootUrl} from 'angular2/src/services/app_root_url';
export function main() {
describe('UrlResolver', () => {
var resolver = new UrlResolver();
var appRootUrl = new AppRootUrl('http://localhost/example/');
var resolver = new UrlResolver(appRootUrl);
describe('absolute base url', () => {
it('should add a relative path to the base url', () => {
@ -70,5 +82,14 @@ export function main() {
expect(resolver.resolve('foo/baz/', '/bar')).toEqual('/bar');
});
});
if (IS_DARTIUM) {
describe('package url', () => {
it('should be served relative to AppRootUrl', () => {
expect(resolver.resolve('foo', 'package:bar/baz.dart'))
.toEqual('http://localhost/example/packages/bar/baz.dart');
});
});
}
});
}

View File

@ -35,7 +35,7 @@ export function main() {
var cache = new CompilerCache();
var viewResolver = new MultipleViewResolver(
count, [BenchmarkComponentNoBindings, BenchmarkComponentWithBindings]);
var urlResolver = new UrlResolver();
var urlResolver = new UrlResolver(new AppRootUrl(""));
var shadowDomStrategy = new NativeShadowDomStrategy();
var renderCompiler = new rc.DefaultDomCompiler(new Parser(new Lexer()), shadowDomStrategy,
new ViewLoader(null, null, null));

View File

@ -37,5 +37,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -20,5 +20,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -2,22 +2,12 @@ library angular2_examples.material.demo_common;
import 'package:angular2/src/dom/browser_adapter.dart';
import 'package:angular2/src/services/url_resolver.dart';
import 'package:angular2/src/services/app_root_url.dart';
void commonDemoSetup() {
BrowserDomAdapter.makeCurrent();
}
class DemoUrlResolver extends UrlResolver {
@override
String resolve(String baseUrl, String url) {
const MATERIAL_PKG = 'package:angular2_material/';
// We run a proxy server in front of pub serve that prepends "example" to
// paths
if (url.startsWith(MATERIAL_PKG)) {
return '/examples/packages/angular2_material/' + url.substring(MATERIAL_PKG.length);
}
return super.resolve(baseUrl, url);
}
DemoUrlResolver(AppRootUrl appRootUrl) : super(appRootUrl);
}

View File

@ -19,7 +19,7 @@ export function commonDemoSetup(): void {
@Injectable()
export class DemoUrlResolver extends UrlResolver {
constructor() {
super();
super(null);
}
resolve(baseUrl: string, url: string): string {

View File

@ -92,5 +92,5 @@ class SimpleDialogComponent {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -18,5 +18,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -12,5 +12,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -20,5 +20,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -37,5 +37,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

View File

@ -20,5 +20,5 @@ class DemoApp {
export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}