From 67027d8be62156484f9e7bdcafdc6072856e8b4c Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] handle empty url path --- src/snapshot.ts | 5 ++++- test/snapshot.test.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index ec23e7ba..52831dfe 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -46,8 +46,11 @@ const URL_IN_CSS_REF = /url\((?:'([^']*)'|"([^"]*)"|([^)]*))\)/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, path1, path2, path3) => { + return cssText.replace(URL_IN_CSS_REF, (origin, path1, path2, path3) => { const filePath = path1 || path2 || path3; + if (!filePath) { + return origin; + } if (!RELATIVE_PATH.test(filePath)) { return `url('${filePath}')`; } diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 197fb7ad..ffb61742 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -54,7 +54,7 @@ describe('absolute url to stylesheet', () => { href, ), ).to.equal( - `background-image: url('http://localhost/css/images/b.jpg');` + + `background-image: url('http://localhost/css/images/b.jpg'); ` + `background: #aabbcc url('http://localhost/css/images/a.jpg') 50% 50% repeat;`, ); }); @@ -64,4 +64,8 @@ describe('absolute url to stylesheet', () => { absoluteToStylesheet('url(data:image/gif;base64,ABC)', href), ).to.equal('url(data:image/gif;base64,ABC)'); }); + + it('can handle empty path', () => { + expect(absoluteToStylesheet(`url('')`, href)).to.equal(`url('')`); + }); });