From 7cb33bfe809c0a7d720e904c8aaffeeaa6f93fff Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] fix css url regexp --- src/snapshot.ts | 2 +- test/snapshot.test.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index fb772813..3acbdb5b 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -42,7 +42,7 @@ function extractOrigin(url: string): string { return origin; } -const URL_IN_CSS_REF = /url\((['"])([^'"]*)\1\)/gm; +const URL_IN_CSS_REF = /url\((['"]|)([^'"]*)\1\)/gm; export function absoluteToStylesheet(cssText: string, href: string): string { return cssText.replace(URL_IN_CSS_REF, (_1, _2, filePath) => { if (!/^[./]/.test(filePath)) { diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 0720f120..227535e9 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -28,4 +28,16 @@ describe('absolute url to stylesheet', () => { absoluteToStylesheet('url("http://localhost/a.jpg")', href), ).to.equal(`url('http://localhost/a.jpg')`); }); + + it('can handle single quote path', () => { + expect(absoluteToStylesheet(`url('./a.jpg')`, href)).to.equal( + `url('http://localhost/css/a.jpg')`, + ); + }); + + it('can handle no quote path', () => { + expect(absoluteToStylesheet('url(./a.jpg)', href)).to.equal( + `url('http://localhost/css/a.jpg')`, + ); + }); });