Skip to content

Commit

Permalink
fix(url_resolver): in Dart make package urls relative to AppRootUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
yjbanov committed Jul 24, 2015
1 parent c2bbda0 commit 469afda
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 33 deletions.
9 changes: 8 additions & 1 deletion modules/angular2/src/services/url_resolver.dart
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions modules/angular2/src/services/url_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions modules/angular2/test/core/compiler/compiler_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -718,4 +718,4 @@ function collectEmbeddedPvs(pv: AppProtoView, target: AppProtoView[] = null): Ap
}
});
return target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down
25 changes: 23 additions & 2 deletions modules/angular2/test/services/url_resolver_spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
}
});
}
2 changes: 1 addition & 1 deletion modules/benchmarks/src/compiler/compiler_benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion modules/examples/src/material/button/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/checkbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
14 changes: 2 additions & 12 deletions modules/examples/src/material/demo_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/demo_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function commonDemoSetup(): void {
@Injectable()
export class DemoUrlResolver extends UrlResolver {
constructor() {
super();
super(null);
}

resolve(baseUrl: string, url: string): string {
Expand Down
2 changes: 1 addition & 1 deletion modules/examples/src/material/dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ class SimpleDialogComponent {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/grid_list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/progress-linear/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/radio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/switcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class DemoApp {

export function main() {
commonDemoSetup();
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
bootstrap(DemoApp, [bind(UrlResolver).toClass(DemoUrlResolver)]);
}

0 comments on commit 469afda

Please sign in to comment.