fix(compiler): ignore @import in comments (#13368)
* refactor(compiler): clean up style url resolver * fix(compiler): ignore @import in css comments Closes #12196
This commit is contained in:
parent
bcd37f52fb
commit
c0f750af4e
@ -9,8 +9,6 @@
|
|||||||
// Some of the code comes from WebComponents.JS
|
// Some of the code comes from WebComponents.JS
|
||||||
// https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
|
// https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
|
||||||
|
|
||||||
import {isBlank, isPresent} from './facade/lang';
|
|
||||||
|
|
||||||
import {UrlResolver} from './url_resolver';
|
import {UrlResolver} from './url_resolver';
|
||||||
|
|
||||||
export class StyleWithImports {
|
export class StyleWithImports {
|
||||||
@ -18,8 +16,8 @@ export class StyleWithImports {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isStyleUrlResolvable(url: string): boolean {
|
export function isStyleUrlResolvable(url: string): boolean {
|
||||||
if (isBlank(url) || url.length === 0 || url[0] == '/') return false;
|
if (url == null || url.length === 0 || url[0] == '/') return false;
|
||||||
const schemeMatch = url.match(_urlWithSchemaRe);
|
const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP);
|
||||||
return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
|
return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,17 +28,20 @@ export function isStyleUrlResolvable(url: string): boolean {
|
|||||||
export function extractStyleUrls(
|
export function extractStyleUrls(
|
||||||
resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports {
|
resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports {
|
||||||
const foundUrls: string[] = [];
|
const foundUrls: string[] = [];
|
||||||
const modifiedCssText = cssText.replace(_cssImportRe, function(...m: string[]) {
|
|
||||||
const url = m[1] || m[2];
|
const modifiedCssText =
|
||||||
if (!isStyleUrlResolvable(url)) {
|
cssText.replace(CSS_COMMENT_REGEXP, '').replace(CSS_IMPORT_REGEXP, (...m: string[]) => {
|
||||||
// Do not attempt to resolve non-package absolute URLs with URI scheme
|
const url = m[1] || m[2];
|
||||||
return m[0];
|
if (!isStyleUrlResolvable(url)) {
|
||||||
}
|
// Do not attempt to resolve non-package absolute URLs with URI scheme
|
||||||
foundUrls.push(resolver.resolve(baseUrl, url));
|
return m[0];
|
||||||
return '';
|
}
|
||||||
});
|
foundUrls.push(resolver.resolve(baseUrl, url));
|
||||||
|
return '';
|
||||||
|
});
|
||||||
return new StyleWithImports(modifiedCssText, foundUrls);
|
return new StyleWithImports(modifiedCssText, foundUrls);
|
||||||
}
|
}
|
||||||
|
|
||||||
const _cssImportRe = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g;
|
const CSS_IMPORT_REGEXP = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g;
|
||||||
const _urlWithSchemaRe = /^([^:/?#]+):/;
|
const CSS_COMMENT_REGEXP = /\/\*.+?\*\//g;
|
||||||
|
const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;
|
||||||
|
@ -36,6 +36,17 @@ export function main() {
|
|||||||
expect(styleWithImports.styleUrls).toEqual(['http://ng.io/1.css', 'http://ng.io/2.css']);
|
expect(styleWithImports.styleUrls).toEqual(['http://ng.io/1.css', 'http://ng.io/2.css']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should ignore "@import" in comments', () => {
|
||||||
|
const css = `
|
||||||
|
@import '1.css';
|
||||||
|
/*@import '2.css';*/
|
||||||
|
`;
|
||||||
|
const styleWithImports = extractStyleUrls(urlResolver, 'http://ng.io', css);
|
||||||
|
expect(styleWithImports.style.trim()).toEqual('');
|
||||||
|
expect(styleWithImports.styleUrls).toContain('http://ng.io/1.css');
|
||||||
|
expect(styleWithImports.styleUrls).not.toContain('http://ng.io/2.css');
|
||||||
|
});
|
||||||
|
|
||||||
it('should extract "@import url()" urls', () => {
|
it('should extract "@import url()" urls', () => {
|
||||||
const css = `
|
const css = `
|
||||||
@import url('3.css');
|
@import url('3.css');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user