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
@@ -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();
@@ -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,
@@ -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);
@@ -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
@@ -718,4 +718,4 @@ function collectEmbeddedPvs(pv: AppProtoView, target: AppProtoView[] = null): Ap
}
});
return target;
}
}
@@ -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 = `
@@ -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);
@@ -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');
});
});
}
});
}