From cdedca1716fa5902b375a7e1f5bd4199dda14f56 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] complately update relative path regexp --- package.json | 2 +- src/snapshot.ts | 3 ++- test/snapshot.test.ts | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4b1ac00b..acdea765 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "chai": "^4.1.2", "jest-snapshot": "^23.6.0", "mocha": "^5.2.0", - "puppeteer": "^1.9.0", + "puppeteer": "^1.10.0", "rollup": "^0.66.4", "rollup-plugin-terser": "^3.0.0", "rollup-plugin-typescript": "^1.0.0", diff --git a/src/snapshot.ts b/src/snapshot.ts index 1423f6d9..4b4708d8 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -43,9 +43,10 @@ function extractOrigin(url: string): string { } const URL_IN_CSS_REF = /url\((['"]|)([^'"]*)\1\)/gm; +const RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/; export function absoluteToStylesheet(cssText: string, href: string): string { return cssText.replace(URL_IN_CSS_REF, (_1, _2, filePath) => { - if (!/^[./]/.test(filePath)) { + if (!RELATIVE_PATH.test(filePath)) { return `url('${filePath}')`; } if (filePath[0] === '/') { diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 227535e9..12542897 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -5,6 +5,12 @@ import { absoluteToStylesheet } from '../src/snapshot'; describe('absolute url to stylesheet', () => { const href = 'http://localhost/css/style.css'; + it('cam handle relative path', () => { + expect(absoluteToStylesheet('url(a.jpg)', href)).to.equal( + `url('http://localhost/css/a.jpg')`, + ); + }); + it('can handle same level path', () => { expect(absoluteToStylesheet('url("./a.jpg")', href)).to.equal( `url('http://localhost/css/a.jpg')`,