badStrings.forEach(function (t) { var encoded = new TextEncoder().encode(t.input); var decoded = new TextDecoder("utf-8").decode(encoded);
assert_equals(t.expected, decoded);
});
}, "bad data");
encodings.forEach(function (test) {
assert_equals( new TextDecoder(test.label.toLowerCase()).encoding,
test.encoding
);
assert_equals( new TextDecoder(test.label.toUpperCase()).encoding,
test.encoding
);
});
}, "Encoding names are case insensitive");
var string = "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD"; // z, cent, CJK water, G-Clef, Private-use character
// missing BOMs
assert_equals(new TextDecoder("utf-8").decode(new Uint8Array(utf8)), string);
assert_equals( new TextDecoder("utf-16le").decode(new Uint8Array(utf16le)),
string
);
assert_equals( new TextDecoder("utf-16be").decode(new Uint8Array(utf16be)),
string
);
// matching BOMs
assert_equals( new TextDecoder("utf-8").decode(new Uint8Array(utf8_bom.concat(utf8))),
string
);
assert_equals( new TextDecoder("utf-16le").decode( new Uint8Array(utf16le_bom.concat(utf16le))
),
string
);
assert_equals( new TextDecoder("utf-16be").decode( new Uint8Array(utf16be_bom.concat(utf16be))
),
string
);
// mismatching BOMs
assert_not_equals( new TextDecoder("utf-8").decode(new Uint8Array(utf16le_bom.concat(utf8))),
string
);
assert_not_equals( new TextDecoder("utf-8").decode(new Uint8Array(utf16be_bom.concat(utf8))),
string
);
assert_not_equals( new TextDecoder("utf-16le").decode( new Uint8Array(utf8_bom.concat(utf16le))
),
string
);
assert_not_equals( new TextDecoder("utf-16le").decode( new Uint8Array(utf16be_bom.concat(utf16le))
),
string
);
assert_not_equals( new TextDecoder("utf-16be").decode( new Uint8Array(utf8_bom.concat(utf16be))
),
string
);
assert_not_equals( new TextDecoder("utf-16be").decode( new Uint8Array(utf16le_bom.concat(utf16be))
),
string
);
}, "Byte-order marks");
test(function () {
assert_equals(new TextDecoder("utf-8").encoding, "utf-8"); // canonical case
assert_equals(new TextDecoder("UTF-16").encoding, "utf-16le"); // canonical case and name
assert_equals(new TextDecoder("UTF-16BE").encoding, "utf-16be"); // canonical case and name
assert_equals(new TextDecoder("iso8859-1").encoding, "windows-1252"); // canonical case and name
assert_equals(new TextDecoder("iso-8859-1").encoding, "windows-1252"); // canonical case and name
}, "Encoding names");
for (var len = 1; len <= 5; ++len) { var out = "",
decoder = new TextDecoder(encoding); for (var i = 0; i < encoded.length; i += len) { var sub = []; for (var j = i; j < encoded.length && j < i + len; ++j) {
sub.push(encoded[j]);
}
out += decoder.decode(new Uint8Array(sub), { stream: true });
}
out += decoder.decode();
assert_equals(out, string, "streaming decode " + encoding);
}
});
}, "Streaming Decode");
test(function () { var jis = [0x82, 0xc9, 0x82, 0xd9, 0x82, 0xf1]; var expected = "\u306B\u307B\u3093"; // Nihon
assert_equals( new TextDecoder("shift_jis").decode(new Uint8Array(jis)),
expected
);
}, "Shift_JIS Decode");
encodings.forEach(function (encoding) { var string = "",
bytes = []; for (var i = 0; i < 128; ++i) { // Encodings that have escape codes in 0x00-0x7F if (
encoding === "iso-2022-jp" &&
(i === 0x1b || i === 0xe || i === 0xf)
) { continue;
}
string += String.fromCharCode(i);
bytes.push(i);
} var ascii_encoded = new TextEncoder().encode(string);
assert_equals( new TextDecoder(encoding).decode(ascii_encoded),
string,
encoding
); //assert_array_equals(new TextEncoder().encode(string), bytes, encoding);
});
}, "Supersets of ASCII decode ASCII correctly");
test(function () {
assert_throws({ name: "TypeError" }, function () { new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array([0xff]));
}); // This should not hang: new TextDecoder("utf-8").decode(new Uint8Array([0xff]));
assert_throws({ name: "TypeError" }, function () { new TextDecoder("utf-16", { fatal: true }).decode(new Uint8Array([0x00]));
}); // This should not hang: new TextDecoder("utf-16").decode(new Uint8Array([0x00]));
assert_throws({ name: "TypeError" }, function () { new TextDecoder("utf-16be", { fatal: true }).decode(new Uint8Array([0x00]));
}); // This should not hang: new TextDecoder("utf-16be").decode(new Uint8Array([0x00]));
}, "Non-fatal errors at EOF");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.