Skip to content

Commit 96f2c21

Browse files
committed
lib: ensure TextDecoder is only removing utf-8 BOM on utf-8 encoding
1 parent bde889b commit 96f2c21

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/internal/encoding.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ function makeTextDecoderJS() {
512512
this[kHandle].write(input);
513513

514514
if (result.length > 0 &&
515+
this[kEncoding] === 'utf-8' &&
515516
!this[kBOMSeen] &&
516517
!(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) {
517518
// If the very first result in the stream is a BOM, and we are not

test/parallel/test-whatwg-encoding-custom-textdecoder.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ assert(TextDecoder);
3434
});
3535
}
3636

37+
// Test TextDecoder, UTF-16LE, fatal: false, ignoreBOM: false
38+
{
39+
['utf-16', 'utf-16le'].forEach((i) => {
40+
// This is a utf16le buffer with a utf8 BOM,
41+
// which should not be removed
42+
const buf = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x00, 0x65,
43+
0x00, 0x73, 0x00, 0x74, 0x00, 0xac,
44+
0x20])
45+
const dec = new TextDecoder(i);
46+
assert.strictEqual(dec.encoding, 'utf-16-le');
47+
const res = dec.decode(buf);
48+
assert.strictEqual(res, '\ufefftest€');
49+
});
50+
}
51+
3752
// Test TextDecoder, UTF-8, fatal: false, ignoreBOM: true
3853
{
3954
['unicode-1-1-utf-8', 'utf8', 'utf-8'].forEach((i) => {

0 commit comments

Comments
 (0)