fix: the other way of setting charset in html

This commit is contained in:
Yanlong Wang 2025-04-17 22:04:06 +08:00
parent ec9f0826ac
commit c795cdb7b3
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -827,7 +827,10 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
throw new AssertionFailureError(`Failed to access ${url}: file too large`); throw new AssertionFailureError(`Failed to access ${url}: file too large`);
} }
snapshot.html = await readFile(await file.filePath, encoding); snapshot.html = await readFile(await file.filePath, encoding);
const innerCharset = snapshot.html.slice(0, 1024).match(/<meta[^>]+text\/html;\s*?charset=([^>"]+)\"/i)?.[1]?.toLowerCase(); let innerCharset;
const peek = snapshot.html.slice(0, 1024);
innerCharset ??= peek.match(/<meta[^>]+text\/html;\s*?charset=([^>"]+)/i)?.[1]?.toLowerCase();
innerCharset ??= peek.match(/<meta[^>]+charset="([^>"]+)\"/i)?.[1]?.toLowerCase();
if (innerCharset && innerCharset !== encoding) { if (innerCharset && innerCharset !== encoding) {
snapshot.html = await readFile(await file.filePath, innerCharset); snapshot.html = await readFile(await file.filePath, innerCharset);
} }