Skip to content

Commit

Permalink
Support case-insensitive encoding in TextDecoder API (Fixes #2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanau committed Jun 20, 2024
1 parent ebb5b48 commit c715f73
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/workerd/api/encoding.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// https://opensource.org/licenses/Apache-2.0

#include "encoding.h"
#include "util.h"
#include <workerd/jsg/jsg.h>
#include <workerd/jsg/buffersource.h>
#include <unicode/ucnv.h>
Expand Down Expand Up @@ -256,6 +257,7 @@ kj::StringPtr getEncodingId(Encoding encoding) {
}

Encoding getEncodingForLabel(kj::StringPtr label) {
kj::String labelInsensitive = toLower(label);
const auto trim = [](kj::StringPtr label) {
const auto isAsciiWhitespace = [](auto c) {
return c == 0x09 /* tab */ ||
Expand All @@ -271,7 +273,7 @@ Encoding getEncodingForLabel(kj::StringPtr label) {
return label.slice(start, end).asChars();
};

auto trimmed = trim(label);
auto trimmed = trim(labelInsensitive);
#define V(label, key) if (trimmed == label##_kj) return Encoding::key;
EW_ENCODING_LABELS(V)
#undef V
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/tests/encoding-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ export const allTheDecoders = {
["utf-16", "utf-16le"],
["utf-16le", "utf-16le"],
["x-user-defined", undefined],
// Test that match is case-insensitive
["UTF-8", "utf-8"],
["UtF-8", "utf-8"],
].forEach((pair) => {
const [label, key] = pair;
if (key === undefined) {
Expand Down

0 comments on commit c715f73

Please sign in to comment.