close #24 css text can be null

This commit is contained in:
Yanzhen Yu
2020-01-26 20:08:52 +08:00
parent 7c01ec3c27
commit b5e9b40f4d

View File

@@ -68,35 +68,41 @@ function extractOrigin(url: string): string {
const URL_IN_CSS_REF = /url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/gm; const URL_IN_CSS_REF = /url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/gm;
const RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/; const RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/;
const DATA_URI = /^(data:)([\w\/\+\-]+);(charset=[\w-]+|base64).*,(.*)/i; const DATA_URI = /^(data:)([\w\/\+\-]+);(charset=[\w-]+|base64).*,(.*)/i;
export function absoluteToStylesheet(cssText: string, href: string): string { export function absoluteToStylesheet(
return cssText.replace(URL_IN_CSS_REF, (origin, path1, path2, path3) => { cssText: string | null,
const filePath = path1 || path2 || path3; href: string,
if (!filePath) { ): string {
return origin; return (cssText || '').replace(
} URL_IN_CSS_REF,
if (!RELATIVE_PATH.test(filePath)) { (origin, path1, path2, path3) => {
return `url('${filePath}')`; const filePath = path1 || path2 || path3;
} if (!filePath) {
if (DATA_URI.test(filePath)) { return origin;
return `url(${filePath})`;
}
if (filePath[0] === '/') {
return `url('${extractOrigin(href) + filePath}')`;
}
const stack = href.split('/');
const parts = filePath.split('/');
stack.pop();
for (const part of parts) {
if (part === '.') {
continue;
} else if (part === '..') {
stack.pop();
} else {
stack.push(part);
} }
} if (!RELATIVE_PATH.test(filePath)) {
return `url('${stack.join('/')}')`; return `url('${filePath}')`;
}); }
if (DATA_URI.test(filePath)) {
return `url(${filePath})`;
}
if (filePath[0] === '/') {
return `url('${extractOrigin(href) + filePath}')`;
}
const stack = href.split('/');
const parts = filePath.split('/');
stack.pop();
for (const part of parts) {
if (part === '.') {
continue;
} else if (part === '..') {
stack.pop();
} else {
stack.push(part);
}
}
return `url('${stack.join('/')}')`;
},
);
} }
function getAbsoluteSrcsetString(doc: Document, attributeValue: string) { function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {