From f29c5abfbc64df9dea479a256c58f159047c136a Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] close #24 css text can be null --- src/snapshot.ts | 62 +++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index 69f0577c..fbffe2db 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -68,35 +68,41 @@ function extractOrigin(url: string): string { 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).*,(.*)/i; -export function absoluteToStylesheet(cssText: string, href: string): string { - 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}')`; - } - if (DATA_URI.test(filePath)) { - return `url(${filePath})`; - } - if (filePath[0] === '/') { - return `url('${extractOrigin(href) + filePath}')`; - } - const stack = href.split('/'); - const parts = filePath.split('/'); - stack.pop(); - for (const part of parts) { - if (part === '.') { - continue; - } else if (part === '..') { - stack.pop(); - } else { - stack.push(part); +export function absoluteToStylesheet( + cssText: string | null, + href: string, +): string { + return (cssText || '').replace( + URL_IN_CSS_REF, + (origin, path1, path2, path3) => { + const filePath = path1 || path2 || path3; + if (!filePath) { + return origin; } - } - return `url('${stack.join('/')}')`; - }); + if (!RELATIVE_PATH.test(filePath)) { + return `url('${filePath}')`; + } + if (DATA_URI.test(filePath)) { + return `url(${filePath})`; + } + if (filePath[0] === '/') { + return `url('${extractOrigin(href) + filePath}')`; + } + const stack = href.split('/'); + const parts = filePath.split('/'); + stack.pop(); + for (const part of parts) { + if (part === '.') { + continue; + } else if (part === '..') { + stack.pop(); + } else { + stack.push(part); + } + } + return `url('${stack.join('/')}')`; + }, + ); } function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {