do not add origin before data uri image
This commit is contained in:
@@ -44,11 +44,15 @@ function extractOrigin(url: string): string {
|
|||||||
|
|
||||||
const URL_IN_CSS_REF = /url\((['"]|)([^'"]*)\1\)/gm;
|
const URL_IN_CSS_REF = /url\((['"]|)([^'"]*)\1\)/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).*,(.*)/gi;
|
||||||
export function absoluteToStylesheet(cssText: string, href: string): string {
|
export function absoluteToStylesheet(cssText: string, href: string): string {
|
||||||
return cssText.replace(URL_IN_CSS_REF, (_1, _2, filePath) => {
|
return cssText.replace(URL_IN_CSS_REF, (_1, _2, filePath) => {
|
||||||
if (!RELATIVE_PATH.test(filePath)) {
|
if (!RELATIVE_PATH.test(filePath)) {
|
||||||
return `url('${filePath}')`;
|
return `url('${filePath}')`;
|
||||||
}
|
}
|
||||||
|
if (DATA_URI.test(filePath)) {
|
||||||
|
return `url(${filePath})`;
|
||||||
|
}
|
||||||
if (filePath[0] === '/') {
|
if (filePath[0] === '/') {
|
||||||
return `url('${extractOrigin(href) + filePath}')`;
|
return `url('${extractOrigin(href) + filePath}')`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,4 +46,10 @@ describe('absolute url to stylesheet', () => {
|
|||||||
`url('http://localhost/css/a.jpg')`,
|
`url('http://localhost/css/a.jpg')`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can handle data url image', () => {
|
||||||
|
expect(
|
||||||
|
absoluteToStylesheet('url(data:image/gif;base64,ABC)', href),
|
||||||
|
).to.equal('url(data:image/gif;base64,ABC)');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user