Skip to content

feat(encoding): Parsec could only parse text that was already decoded - #79

Merged
jamesconwaydev merged 4 commits into
mainfrom
encoding
Nov 18, 2026
Merged

jamesconwaydev merged 4 commits into
mainfrom
encoding

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Parsec took only &str. You had to know a page's encoding before you could parse it, and most pages on the web don't tell you in the place you'd look first. Lexbor sniffs it (lxb_html_encoding_determine) and has its own decoders.

Change

A new crate, crates/parsec-encoding (package parsec-html-encoding, crate parsec_encoding), beside the synced core, which it doesn't touch.

  • parse_bytes(&[u8], Options) follows the HTML Standard's encoding sniffing: a byte order mark, then the transport-layer charset if you pass one, then the prescan of the first 1,024 bytes, then a fallback. It returns the document, the encoding and the spec's confidence.
  • When the parser meets a <meta> naming another encoding than a tentative guess (one past the prescan's 1,024 bytes, say), the spec has the browser start again. Here that's one more decode and parse of the same bytes, at most once; the time budget covers both.
  • sniff and prescan on their own. The prescan follows "prescan a byte stream", "get an attribute", "get an XML encoding" and the meta-content extraction step by step.
  • The fallback is windows-1252, and locale_default gives the spec's suggested default for a language. That's the table already in the standard; TLD and text guessing aren't, so they're left out.
  • Decoding and encoding come from encoding_rs, re-exported, so every WHATWG encoding is there by label, and streams decode through its Decoder. Feeding the parser chunk by chunk waits for chunked input (Chunked input that builds exactly the same tree #6).
  • bench/: parsec-html-bench encoding times bytes to UTF-8 against Lexbor's encoding module. It also runs Lexbor's decoders over the WPT character pages. Lexbor 3.0.x gets 5,240 of 7,393 Shift_JIS characters right because it refuses lead bytes 0xE1 to 0xFB. That's fixed on Lexbor's master in Encoding: accept the full Shift_JIS upper lead byte range lexbor/lexbor#424, not yet released.

New dependency: encoding_rs 0.8 ("(Apache-2.0 OR MIT) AND BSD-3-Clause", MSRV 1.88), plus simdutf8, scopeguard, cfg-if and multiversion_no_op under it, all MIT or Apache-2.0. cargo deny check passes.

Tests

  • html5lib-tests encoding/ (tests1.dat, tests2.dat, test-yahoo-jp.dat): 82 passed, 0 failed, 1 skipped (scripted/tests1.dat writes its <meta> with document.write). The prescan alone decides 75; the other 7 put the <meta> past byte 1,024 and need the reparse.
  • web-platform-tests encoding/ data, pinned in crates/parsec-encoding/tests/wpt at 0c423ab with its licence: 228 labels (with case and whitespace variants), 168 single-byte label tables, 38 ISO-2022-JP and 357 GB18030/GBK decoder cases, and 62,352 characters across the Shift_JIS, EUC-JP, ISO-2022-JP, EUC-KR and Big5 pages, each page through parse_bytes. All pass. An encoder round trip over the same pages passes 53,813 of 53,813. That one is not a WPT count.
  • Unit tests for the prescan's corner cases, every prefix of a tricky input, BOM over transport over <meta>, the late-<meta> reparse and the locale table. Doc examples run too.
  • Counts go to $CONFORMANCE_DIR like the other suites, so they show in the run summary.

Closes #5
Refs #25: streaming the decoder into the parser waits for chunked input (#6).

Pages arrive as bytes, and Parsec took only &str, so every caller had
to guess the encoding first. crates/parsec-encoding (package
parsec-html-encoding, crate parsec_encoding) adds parse_bytes, which
follows the HTML Standard's encoding sniffing: byte order mark, then
the transport-layer charset if given, then the prescan of the first
1,024 bytes, then a fallback (windows-1252, or the spec's suggested
default for a locale via locale_default). When the parser meets a
<meta> that names another encoding than a tentative guess, the page is
decoded and parsed once more in that encoding, as a browser restarts;
the time budget covers both parses.

The decoding is encoding_rs, re-exported, so every WHATWG encoding
decodes and encodes by label, and streams through its Decoder.

Tests: the html5lib-tests encoding/ files (82 pass, 1 scripted case
skipped) and the web-platform-tests encoding/ data pinned in
tests/wpt: labels, single-byte tables, the ISO-2022-JP, GB18030 and
GBK decoder cases and the legacy multi-byte character pages, all
passing, with counts written for CI's conformance summary.
`parsec-html-bench encoding` times bytes to UTF-8 for UTF-8,
Shift_JIS, windows-1251 and GBK, on two real pages transcoded with
encoding_rs and a generated page of dense text, against Lexbor's
encoding module (to code points, and on to UTF-8 with its encoder).
It also runs Lexbor's decoders over the WPT character pages, which
shows its Shift_JIS decoder refusing lead bytes 0xE1 to 0xFB.
@jamesconwaydev
jamesconwaydev merged commit eef1d41 into main Nov 18, 2026
13 checks passed
@jamesconwaydev
jamesconwaydev deleted the encoding branch November 18, 2026 23:44
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.

Parse bytes, sniffing the encoding as browsers do

1 participant