Skip to content

fix: read content encoding hints from <meta> tags#3780

Merged
barjin merged 3 commits into
v4from
fix/html-embedded-encoding
Jun 30, 2026
Merged

fix: read content encoding hints from <meta> tags#3780
barjin merged 3 commits into
v4from
fix/html-embedded-encoding

Conversation

@barjin

@barjin barjin commented Jun 24, 2026

Copy link
Copy Markdown
Member

Utilizes the byte-stream prescan (https://html.spec.whatwg.org/multipage/parsing.html#prescan-a-byte-stream-to-determine-its-encoding) to determine the HTML document encoding, if not specified in HTTP headers (or explicitly forced by user).

Closes #2317

@barjin barjin added this to the 4.0 milestone Jun 25, 2026
@barjin
barjin requested a review from janbuchar June 25, 2026 08:36
*/
export function extractCharsetFromHtmlBytes(bytes: Buffer): string | undefined {
// latin1 preserves byte values for ASCII-compatible encodings, making the meta tags readable
const prescan = bytes.subarray(0, 1024).toString('latin1');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that 1024 bytes will do it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 13.2.3.2 Determining the character encoding paragraph 5:

Optionally, prescan the byte stream to determine its encoding, with the end condition being when the user agent decides that scanning further bytes would not be efficient. User agents are encouraged to only prescan the first 1024 bytes. User agents may decide that scanning any bytes is not efficient, in which case these substeps are entirely skipped.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, lawyered. Approving.

@barjin
barjin merged commit 7acf908 into v4 Jun 30, 2026
7 checks passed
@barjin
barjin deleted the fix/html-embedded-encoding branch June 30, 2026 08:46
barjin added a commit that referenced this pull request Jul 20, 2026
Utilizes the byte-stream prescan
(https://html.spec.whatwg.org/multipage/parsing.html#prescan-a-byte-stream-to-determine-its-encoding)
to determine the HTML document encoding, if not specified in HTTP
headers (or explicitly forced by user).

Closes #2317
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants