Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions Polyfills/TextDecoder/Source/TextDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@

namespace
{
// Normalize an encoding label per the WHATWG Encoding Standard "get an encoding"
// algorithm: strip leading/trailing ASCII whitespace and ASCII-lowercase the result.
std::string NormalizeEncodingLabel(const std::string& encoding)
{
const auto isAsciiWhitespace = [](char c) {
return c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == ' ';
};

size_t begin = 0;
size_t end = encoding.size();
while (begin < end && isAsciiWhitespace(encoding[begin]))
{
++begin;
}
while (end > begin && isAsciiWhitespace(encoding[end - 1]))
{
--end;
}

std::string label = encoding.substr(begin, end - begin);
for (auto& c : label)
{
if (c >= 'A' && c <= 'Z')
{
c = static_cast<char>(c - 'A' + 'a');
}
}
return label;
}

class TextDecoder final : public Napi::ObjectWrap<TextDecoder>
{
public:
Expand Down Expand Up @@ -33,9 +63,18 @@ namespace
if (info.Length() > 0 && info[0].IsString())
{
auto encoding = info[0].As<Napi::String>().Utf8Value();
if (encoding != "utf-8" && encoding != "UTF-8")

// Several labels (e.g. "utf8", "unicode-1-1-utf-8") all map to UTF-8 after
// normalization; callers such as the glTF/Draco loader pass "utf8".
const std::string label = NormalizeEncodingLabel(encoding);
if (label != "utf-8" &&
label != "utf8" &&
label != "unicode-1-1-utf-8" &&
label != "unicode11utf8" &&
label != "unicode20utf8" &&
label != "x-unicode20utf8")
{
throw Napi::Error::New(Env(), "TextDecoder: unsupported encoding '" + encoding + "', only 'utf-8' is supported");
throw Napi::Error::New(Env(), "TextDecoder: unsupported encoding '" + encoding + "', only UTF-8 is supported");
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/UnitTests/Scripts/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,30 @@ describe("TextDecoder", function () {
const decoder = new TextDecoder("utf-8");
expect(decoder.decode(new Uint8Array([79, 75]))).to.equal("OK");
});

it("should accept the WHATWG 'utf8' label (no hyphen)", function () {
const decoder = new TextDecoder("utf8");
const result = decoder.decode(new Uint8Array([72, 105])); // "Hi"
expect(result).to.equal("Hi");
});

it("should accept utf-8 labels case-insensitively and with surrounding whitespace", function () {
for (const label of ["UTF-8", "UTF8", " utf-8 ", "\tUtf8\n"]) {
const decoder = new TextDecoder(label);
expect(decoder.decode(new Uint8Array([79, 75]))).to.equal("OK");
}
});

it("should accept the other WHATWG utf-8 aliases", function () {
for (const label of ["unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "x-unicode20utf8"]) {
const decoder = new TextDecoder(label);
expect(decoder.decode(new Uint8Array([79, 75]))).to.equal("OK");
}
});

it("should still throw for a genuinely unsupported encoding", function () {
expect(() => new TextDecoder("utf-16")).to.throw();
});
});

describe("TextEncoder", function () {
Expand Down
Loading