Skip to content

Commit 16e5206

Browse files
committed
Adding tests (some commented for now) and fixing typos following comments
1 parent 17770b7 commit 16e5206

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

lib/TextDecoder-impl.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
const { labelToName, isSupported, decode } = require("./whatwg-encoding");
44

55
exports.implementation = class TextDecoderImpl {
6-
constructor(globalObject, constructorArgs = []) {
7-
const label = constructorArgs[0];
8-
const options = constructorArgs[1] || {};
9-
6+
constructor(globalObject, [label, options = {}]) {
107
const encoding = labelToName(label);
11-
if (!isSupported(encoding) || encoding === "replacement") {
12-
throw new RangeError(`"${encoding}" is not a supported encoding name`);
8+
if (encoding === null || !isSupported(encoding) || encoding === "replacement") {
9+
throw new RangeError(`"${label}" is not a supported encoding name`);
1310
}
1411
this._encoding = encoding;
15-
this._errorMode = options._fatal ? "fatal" : "replacement";
16-
this._ignoreBOM = options._ignoreBOM || false;
12+
this._errorMode = options.fatal === true ? "fatal" : "replacement";
13+
this._ignoreBOM = options.ignoreBOM || false;
1714
}
1815

1916
get encoding() {
20-
return String(this._encoding).toLowerCase();
17+
return this._encoding.toLowerCase();
2118
}
2219

2320
get fatal() {
@@ -29,12 +26,12 @@ exports.implementation = class TextDecoderImpl {
2926
}
3027

3128
decode(input, options = {}) {
32-
if (options.steam === true) {
29+
if (options.stream === true) {
3330
// TODO: Implement stream support
3431
}
3532
try {
3633
// TODO: Implement ignoreBOM support
37-
return decode(Buffer.from(input), this._encoding);
34+
return decode(Buffer.from(input), this._encoding, this._ignoreBOM);
3835
} catch (exception) {
3936
if (this._errorMode === "fatal") {
4037
throw new TypeError(exception);

lib/TextEncoder-impl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.implementation = class TextEncoderImpl {
1515
return encode(Buffer.from(input));
1616
}
1717

18-
encodeInto(source, destination) {
18+
encodeInto(/* source, destination */) {
1919
// TODO Implement stream copy support
2020
}
2121
};

lib/whatwg-encoding.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@ exports.labelToName = label => {
1313
};
1414

1515
// https://encoding.spec.whatwg.org/#decode
16-
exports.decode = (buffer, fallbackEncodingName) => {
16+
exports.decode = (buffer, fallbackEncodingName, ignoreBOM) => {
1717
let encoding = fallbackEncodingName;
1818
if (!exports.isSupported(encoding)) {
1919
throw new RangeError(`"${encoding}" is not a supported encoding name`);
2020
}
2121

2222
const bomEncoding = exports.getBOMEncoding(buffer);
23-
if (bomEncoding !== null) {
23+
if (!ignoreBOM && bomEncoding !== null) {
2424
encoding = bomEncoding;
2525
}
2626

2727
// iconv-lite will strip BOMs for us, so no need to do the stuff the spec does
28+
const decodeOptions = {
29+
stripBOM: !ignoreBOM
30+
};
2831

29-
return iconvLite.decode(buffer, encoding);
32+
return iconvLite.decode(buffer, encoding, decodeOptions);
3033
};
3134

3235

scripts/get-latest-platform-tests.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,23 @@ const targetDir = path.resolve(__dirname, "..", "test", "web-platform-tests");
3030

3131
for (const file of [
3232
"api-basics.any.js",
33+
// "api-invalid-label.any.js",
34+
"api-replacement-encodings.any.js",
3335
"api-surrogates-utf8.any.js",
34-
// "textdecoder-ignorebom.any.js",
35-
"textencoder-utf16-surrogates.any.js"
36+
// "encodeInto.any.js",
37+
// "replacement-encodings.any.js",
38+
"textdecoder-byte-order-marks.any.js",
39+
// "textdecoder-copy.any.js",
40+
// "textdecoder-fatal-single-byte.any.js",
41+
// "textdecoder-fatal-streaming.any.js",
42+
// "textdecoder-fatal.any.js",
43+
"textdecoder-ignorebom.any.js",
44+
// "textdecoder-labels.any.js",
45+
// "textdecoder-streaming.any.js",
46+
// "textdecoder-utf16-surrogates.any.js"
47+
// "textencoder-constructor-non-utf.any.js",
48+
// "textencoder-utf16-surrogates.any.js",
49+
// "unsupported-encodings.any.js"
3650
]) {
3751
pipeline(
3852
got.stream(`${urlPrefix}${file}`),

test/testharness.js

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module.exports = {
1111
// No-op; the only place this is used in WPT is not applicable to our usage.
1212
},
1313

14+
subsetTest(testFunc, ...args) {
15+
return testFunc(...args);
16+
},
17+
1418
assert_true(actual) {
1519
assert.strictEqual(actual, true);
1620
},
@@ -23,6 +27,10 @@ module.exports = {
2327
assert.strictEqual(actual, expected);
2428
},
2529

30+
assert_not_equals(actual, expected) {
31+
assert.notEqual(actual, expected);
32+
},
33+
2634
assert_array_equals(actual, expected) {
2735
assert.deepStrictEqual([...actual], [...expected]);
2836
},
@@ -31,6 +39,10 @@ module.exports = {
3139
assert.throws(func);
3240
},
3341

42+
assert_throws_js(code, func) {
43+
assert.throws(func);
44+
},
45+
3446
assert_unreached() {
3547
assert(false);
3648
}

0 commit comments

Comments
 (0)