Skip to content

Commit

Permalink
lib: ensure TextDecoder is only removing utf-8 BOM on utf-8 enc…
Browse files Browse the repository at this point in the history
…oding
  • Loading branch information
phated committed Apr 18, 2022
1 parent bde889b commit 96f2c21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ function makeTextDecoderJS() {
this[kHandle].write(input);

if (result.length > 0 &&
this[kEncoding] === 'utf-8' &&
!this[kBOMSeen] &&
!(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) {
// If the very first result in the stream is a BOM, and we are not
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ assert(TextDecoder);
});
}

// Test TextDecoder, UTF-16LE, fatal: false, ignoreBOM: false
{
['utf-16', 'utf-16le'].forEach((i) => {
// This is a utf16le buffer with a utf8 BOM,
// which should not be removed
const buf = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x00, 0x65,
0x00, 0x73, 0x00, 0x74, 0x00, 0xac,
0x20])
const dec = new TextDecoder(i);
assert.strictEqual(dec.encoding, 'utf-16-le');
const res = dec.decode(buf);
assert.strictEqual(res, '\ufefftest€');
});
}

// Test TextDecoder, UTF-8, fatal: false, ignoreBOM: true
{
['unicode-1-1-utf-8', 'utf8', 'utf-8'].forEach((i) => {
Expand Down

0 comments on commit 96f2c21

Please sign in to comment.