2015-08-25 15:36:02 -07:00
|
|
|
// Some of the code comes from WebComponents.JS
|
|
|
|
// https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {RegExpWrapper, StringWrapper, isBlank, isPresent} from '../src/facade/lang';
|
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
import {UrlResolver} from './url_resolver';
|
2015-08-25 15:36:02 -07:00
|
|
|
|
|
|
|
export class StyleWithImports {
|
|
|
|
constructor(public style: string, public styleUrls: string[]) {}
|
|
|
|
}
|
|
|
|
|
2015-10-14 09:39:40 -07:00
|
|
|
export function isStyleUrlResolvable(url: string): boolean {
|
2015-10-15 10:17:14 -07:00
|
|
|
if (isBlank(url) || url.length === 0 || url[0] == '/') return false;
|
2015-10-14 09:39:40 -07:00
|
|
|
var schemeMatch = RegExpWrapper.firstMatch(_urlWithSchemaRe, url);
|
2015-10-26 16:34:40 -07:00
|
|
|
return isBlank(schemeMatch) || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
|
2015-10-14 09:39:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewrites stylesheets by resolving and removing the @import urls that
|
|
|
|
* are either relative or don't have a `package:` scheme
|
|
|
|
*/
|
2016-06-08 16:38:52 -07:00
|
|
|
export function extractStyleUrls(
|
|
|
|
resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports {
|
2016-06-08 15:45:15 -07:00
|
|
|
var foundUrls: any[] /** TODO #9100 */ = [];
|
2016-06-08 16:38:52 -07:00
|
|
|
var modifiedCssText =
|
|
|
|
StringWrapper.replaceAllMapped(cssText, _cssImportRe, (m: any /** TODO #9100 */) => {
|
|
|
|
var url = isPresent(m[1]) ? m[1] : m[2];
|
|
|
|
if (!isStyleUrlResolvable(url)) {
|
|
|
|
// Do not attempt to resolve non-package absolute URLs with URI scheme
|
|
|
|
return m[0];
|
|
|
|
}
|
|
|
|
foundUrls.push(resolver.resolve(baseUrl, url));
|
|
|
|
return '';
|
|
|
|
});
|
2015-10-14 09:39:40 -07:00
|
|
|
return new StyleWithImports(modifiedCssText, foundUrls);
|
2015-09-02 15:07:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var _cssImportRe = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g;
|
2015-10-07 13:37:56 -07:00
|
|
|
// TODO: can't use /^[^:/?#.]+:/g due to clang-format bug:
|
|
|
|
// https://github.com/angular/angular/issues/4596
|
2015-10-15 10:17:14 -07:00
|
|
|
var _urlWithSchemaRe = /^([a-zA-Z\-\+\.]+):/g;
|