From 922be70f545b839c26edc41e6d76f5ad3d4e5bd2 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Thu, 27 Dec 2018 12:44:52 +0800 Subject: [PATCH] do not add origin before data uri image --- src/snapshot.ts | 4 ++++ test/snapshot.test.ts | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/snapshot.ts b/src/snapshot.ts index cf058345..cc470957 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -44,11 +44,15 @@ function extractOrigin(url: string): string { const URL_IN_CSS_REF = /url\((['"]|)([^'"]*)\1\)/gm; 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 { return cssText.replace(URL_IN_CSS_REF, (_1, _2, filePath) => { if (!RELATIVE_PATH.test(filePath)) { return `url('${filePath}')`; } + if (DATA_URI.test(filePath)) { + return `url(${filePath})`; + } if (filePath[0] === '/') { return `url('${extractOrigin(href) + filePath}')`; } diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 12542897..106779e9 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -46,4 +46,10 @@ describe('absolute url to stylesheet', () => { `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)'); + }); });