Browse documentation

TextDecoder decodes UTF-8 and the single-byte encodings, from generated WHATWG tables

Date: 2026-12-01 Area: web, encoding, generated-data Related: #2521, Built-ins — TextDecoder, Build System — Generated Encoding Data, ADR 0216

Context

TextDecoder accepted only UTF-8's six labels and threw RangeError for every other one. Browsers, Node, Deno and Bun accept every label of the WHATWG Encoding Standard, and real code relies on the legacy single-byte ones for byte-oriented text: @pixi/3d 0.1.0 builds new TextDecoder("latin1") at its HDR loader's module top level, so importing the package at all failed.

The standard defines UTF-8, 28 legacy single-byte encodings (one 128-entry index each, for the bytes 0x80-0xFF), the legacy multi-byte encodings (GBK and gb18030, Big5, EUC-JP, ISO-2022-JP, Shift_JIS, EUC-KR, whose indexes run to tens of thousands of entries), UTF-16BE/LE, x-user-defined, and the replacement encoding, whose labels TextDecoder must refuse. The standard's repository (whatwg/encoding) publishes the labels as encodings.json and the indexes as indexes.json. UTF-8 is decoded natively (utf8Decode, Goccia.HostAPI): a decoder written in the prelude was too slow under heaptrc (#2351).

Decision

  • TextDecoder decodes UTF-8 and all 28 single-byte encodings, under every label the standard gives them. A single-byte decoder is one mechanism over 28 tables, so taking one encoding (windows-1252, which latin1, ascii and iso-8859-1 name) would have left the other 27's labels refused for no saving.
  • The labels and the indexes are a generated Pascal constant unit, Generated.EncodingData, produced by scripts/generate-encoding-data.js from encodings.json and indexes.json at one pinned commit of whatwg/encoding, as ADR 0216 made the IDNA tables. The unit records the commit; moving to a newer one regenerates it.
  • The host looks labels up and decodes, not the prelude. encodingFor strips ASCII whitespace from a label's ends, lowercases ASCII letters only, and binary-searches the sorted labels; singleByteDecode writes one code unit a byte into one string, charged to the memory limit before it is made, with each byte charged to an instruction limit as utf8Decode charges its bytes.
  • UTF-16, x-user-defined and the multi-byte encodings are not implemented, and their labels stay a RangeError, as the replacement encoding's labels are. The multi-byte indexes are large and their decoders stateful, and no use has asked for them yet; each can be added as its own decoder over the same label table when one does.

Consequences

  • new TextDecoder("latin1") and every other single-byte label work, and a script that imports a library building such a decoder at load time runs.
  • A label of an unimplemented encoding is a RangeError where a browser decodes, which docs/built-ins.md names.
  • The tables are whatwg/encoding's at the pinned commit; a change to the standard's indexes reaches the engine only when the generator runs again.