ladybird/Tests/LibWeb/Text/input/Encoding/TextDecoder-decode.html
Andreas Kling 45da0e4a0e LibTextCodec: Preserve malformed decoder replacements
Reject UTF-8 second bytes outside the Encoding Standard's per-lead-byte
bounds before consuming the rest of each sequence. This keeps surrogate
and out-of-range sequences from collapsing multiple malformed bytes into
one replacement character.

Also report an odd trailing UTF-16 byte as U+FFFD through the streaming
code point path and route UTF-16 to_utf8() through the same logic. This
keeps lazy and eager script decoding aligned for bytecode cache source
hashes.

Cover the malformed UTF-8 and UTF-16 cases in LibTextCodec, TextDecoder,
and bytecode-cache source decoding tests.
2026-05-18 14:08:22 +02:00

19 lines
839 B
HTML

<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
try {
let decoder = new TextDecoder("utf-8");
println(`[${decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))}]`); // "ABC"
println(`[${decoder.decode()}]`);
const surrogate = decoder.decode(new Uint8Array([0xed, 0xa0, 0x80])); // U+D800
println(`[${surrogate.length}, ${surrogate.codePointAt(0).toString(16)}, ${surrogate.codePointAt(1).toString(16)}, ${surrogate.codePointAt(2).toString(16)}]`);
const truncatedTail = decoder.decode(new Uint8Array([0xf0, 0x9f, 0x98]));
println(`[${truncatedTail.length}, ${truncatedTail.codePointAt(0).toString(16)}]`);
} catch (e) {
println("ERROR: " + e.name + ": " + e.message);
}
});
</script>