2015-10-13 03:29:13 -04:00
|
|
|
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/testing_internal';
|
2015-11-05 17:07:57 -05:00
|
|
|
import {extractStyleUrls, isStyleUrlResolvable} from 'angular2/src/compiler/style_url_resolver';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-11-05 17:07:57 -05:00
|
|
|
import {UrlResolver} from 'angular2/src/compiler/url_resolver';
|
2015-08-25 18:36:02 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-10-14 12:39:40 -04:00
|
|
|
describe('extractStyleUrls', () => {
|
2015-09-02 18:07:31 -04:00
|
|
|
var urlResolver;
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
beforeEach(() => { urlResolver = new UrlResolver(); });
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-10-14 12:39:40 -04:00
|
|
|
it('should not resolve "url()" urls', () => {
|
2015-09-02 18:07:31 -04:00
|
|
|
var css = `
|
|
|
|
.foo {
|
|
|
|
background-image: url("double.jpg");
|
|
|
|
background-image: url('simple.jpg');
|
|
|
|
background-image: url(noquote.jpg);
|
|
|
|
}`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var resolvedCss = extractStyleUrls(urlResolver, 'http://ng.io', css).style;
|
|
|
|
expect(resolvedCss).toEqual(css);
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
it('should extract "@import" urls', () => {
|
|
|
|
var css = `
|
|
|
|
@import '1.css';
|
|
|
|
@import "2.css";
|
|
|
|
`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
2015-09-02 18:07:31 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual('');
|
|
|
|
expect(styleWithImports.styleUrls).toEqual(['http://ng.io/1.css', 'http://ng.io/2.css']);
|
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
it('should extract "@import url()" urls', () => {
|
|
|
|
var css = `
|
|
|
|
@import url('3.css');
|
|
|
|
@import url("4.css");
|
|
|
|
@import url(5.css);
|
|
|
|
`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
2015-09-02 18:07:31 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual('');
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(styleWithImports.styleUrls)
|
|
|
|
.toEqual(['http://ng.io/3.css', 'http://ng.io/4.css', 'http://ng.io/5.css']);
|
2015-09-02 18:07:31 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
it('should extract "@import urls and keep rules in the same line', () => {
|
|
|
|
var css = `@import url('some.css');div {color: red};`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
2015-09-02 18:07:31 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual('div {color: red};');
|
|
|
|
expect(styleWithImports.styleUrls).toEqual(['http://ng.io/some.css']);
|
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
|
2015-09-02 18:07:31 -04:00
|
|
|
it('should extract media query in "@import"', () => {
|
|
|
|
var css = `
|
|
|
|
@import 'print1.css' print;
|
|
|
|
@import url(print2.css) print;
|
|
|
|
`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
2015-09-02 18:07:31 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual('');
|
2016-04-12 12:40:37 -04:00
|
|
|
expect(styleWithImports.styleUrls)
|
|
|
|
.toEqual(['http://ng.io/print1.css', 'http://ng.io/print2.css']);
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
|
|
|
|
2015-10-07 16:37:56 -04:00
|
|
|
it('should leave absolute non-package @import urls intact', () => {
|
|
|
|
var css = `@import url('http://server.com/some.css');`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
2015-10-07 16:37:56 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual(`@import url('http://server.com/some.css');`);
|
|
|
|
expect(styleWithImports.styleUrls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve package @import urls', () => {
|
|
|
|
var css = `@import url('package:a/b/some.css');`;
|
2015-10-14 12:39:40 -04:00
|
|
|
var styleWithImports = extractStyleUrls(new FakeUrlResolver(), 'http://ng.io', css);
|
2015-10-07 16:37:56 -04:00
|
|
|
expect(styleWithImports.style.trim()).toEqual(``);
|
|
|
|
expect(styleWithImports.styleUrls).toEqual(['fake_resolved_url']);
|
|
|
|
});
|
|
|
|
|
2015-08-25 18:36:02 -04:00
|
|
|
});
|
2015-10-14 12:39:40 -04:00
|
|
|
|
|
|
|
describe('isStyleUrlResolvable', () => {
|
|
|
|
it('should resolve relative urls',
|
|
|
|
() => { expect(isStyleUrlResolvable('someUrl.css')).toBe(true); });
|
|
|
|
|
|
|
|
it('should resolve package: urls',
|
|
|
|
() => { expect(isStyleUrlResolvable('package:someUrl.css')).toBe(true); });
|
|
|
|
|
|
|
|
it('should resolve asset: urls',
|
2015-10-26 19:34:40 -04:00
|
|
|
() => { expect(isStyleUrlResolvable('asset:someUrl.css')).toBe(true); });
|
2015-10-14 12:39:40 -04:00
|
|
|
|
|
|
|
it('should not resolve empty urls', () => {
|
|
|
|
expect(isStyleUrlResolvable(null)).toBe(false);
|
|
|
|
expect(isStyleUrlResolvable('')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not resolve urls with other schema',
|
|
|
|
() => { expect(isStyleUrlResolvable('http://otherurl')).toBe(false); });
|
2015-10-15 13:17:14 -04:00
|
|
|
|
|
|
|
it('should not resolve urls with absolute paths', () => {
|
|
|
|
expect(isStyleUrlResolvable('/otherurl')).toBe(false);
|
|
|
|
expect(isStyleUrlResolvable('//otherurl')).toBe(false);
|
|
|
|
});
|
2015-10-14 12:39:40 -04:00
|
|
|
});
|
2015-08-25 18:36:02 -04:00
|
|
|
}
|
2015-10-07 16:37:56 -04:00
|
|
|
|
|
|
|
/// The real thing behaves differently between Dart and JS for package URIs.
|
|
|
|
class FakeUrlResolver extends UrlResolver {
|
|
|
|
constructor() { super(); }
|
|
|
|
|
|
|
|
resolve(baseUrl: string, url: string): string { return 'fake_resolved_url'; }
|
|
|
|
}
|