dom/encoding/test/test_TextDecoder.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9c84e4e8e3fd
1 /*
2 * test_TextDecoderOptions.js
3 * bug 764234 tests
4 */
5
6 function runTextDecoderOptions()
7 {
8 const data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
9 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
10 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
11 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
12 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
13 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
14 0xda, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
15 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
16 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb];
17
18 const expectedString = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07"
19 + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f"
20 + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17"
21 + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f"
22 + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27"
23 + "\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f"
24 + "\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
25 + "\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43"
26 + "\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b"
27 + "\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53"
28 + "\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a\u0e5b";
29
30 test(testDecoderGetEncoding, "testDecoderGetEncoding");
31 test(testDecodeGreek, "testDecodeGreek");
32 test(function() {
33 testConstructorFatalOption(data, expectedString);
34 }, "testConstructorFatalOption");
35 test(function() {
36 testConstructorEncodingOption(data, expectedString);
37 }, "testConstructorEncodingOption");
38 test(function() {
39 testDecodeStreamOption(data, expectedString);
40 }, "testDecodeStreamOption");
41 test(testDecodeStreamCompositions, "testDecodeStreamCompositions");
42 test(function() {
43 testDecodeABVOption(data, expectedString);
44 }, "testDecodeABVOption");
45 test(testDecoderForThaiEncoding, "testDecoderForThaiEncoding");
46 test(testInvalid2022JP, "testInvalid2022JP");
47 }
48
49 /*
50 * function testConstructor()
51 *
52 * - This function tests the constructor optional arguments.
53 * - Stream option remains null for this test.
54 * - The stream option is passed to the decode function.
55 * - This function is not testing the decode function.
56 *
57 */
58 function testConstructorFatalOption(data, expectedString)
59 {
60 //invalid string to decode passed, fatal = false
61 testCharset({fatal: false, encoding: "iso-8859-11", input: [], expected: "",
62 msg: "constructor fatal option set to false test."});
63
64 //invalid string to decode passed, fatal = true
65 testCharset({fatal: true, encoding: "iso-8859-11", input: [], expected: "",
66 msg: "constructor fatal option set to true test."});
67 }
68
69 function testConstructorEncodingOption(aData, aExpectedString)
70 {
71 // valid encoding passed
72 testCharset({encoding: "iso-8859-11", input: aData, expected: aExpectedString,
73 msg: "decoder testing constructor valid encoding."});
74
75 // invalid encoding passed
76 testCharset({encoding: "asdfasdf", input: aData, error: "TypeError",
77 msg: "constructor encoding, invalid encoding test."});
78
79 // passing spaces for encoding
80 testCharset({encoding: " ", input: aData, error: "TypeError",
81 msg: "constructor encoding, spaces encoding test."});
82
83 // passing null for encoding
84 testCharset({encoding: null, input: aData, error: "TypeError",
85 msg: "constructor encoding, \"null\" encoding test."});
86
87 // empty encoding passed
88 testCharset({encoding: "", input: aData, error: "TypeError",
89 msg: "constuctor encoding, empty encoding test."});
90
91 // replacement character test
92 aExpectedString = "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
93 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
94 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
95 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
96 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
97 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
98 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
99 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
100 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
101 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
102 + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd";
103 testCharset({encoding: "utf-8", input: aData, expected: aExpectedString,
104 msg: "constuctor encoding, utf-8 test."});
105 }
106
107 /*
108 * function testDecodeStreamOption()
109 *
110 * - fatal remains null for the entire test
111 * - encoding remains as "iso-8859-11"
112 * - The stream option is modified for this test.
113 * - ArrayBufferView is modified for this test.
114 */
115 function testDecodeStreamOption(data, expectedString)
116 {
117 const streamData = [[0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
118 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
119 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8,
120 0xb9, 0xba, 0xbb, 0xbc, 0xbd],
121 [0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
122 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
123 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
124 0xd8, 0xd9, 0xda, 0xdf, 0xe0, 0xe1, 0xe2],
125 [0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
126 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
127 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb]];
128
129 const expectedStringOne = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07"
130 + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f"
131 + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17"
132 + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d";
133 const expectedStringTwo = "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25"
134 + "\u0e26\u0e27\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d"
135 + "\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35"
136 + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41"
137 + "\u0e42";
138 const expectedStringThree = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a"
139 + "\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51"
140 + "\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58"
141 + "\u0e59\u0e5a\u0e5b";
142 expectedString = [expectedStringOne, expectedStringTwo, expectedStringThree];
143
144 // streaming test
145
146 /* - the streaming is null
147 * - streaming is not set in the decode function
148 */
149 testCharset({encoding: "iso-8859-11", array: [
150 {input: streamData[0], expected: expectedStringOne},
151 {input: streamData[1], expected: expectedStringTwo},
152 {input: streamData[2], expected: expectedStringThree},
153 ], msg: "decode() stream test zero."});
154
155 testCharset({encoding: "iso-8859-11", array: [
156 {input: streamData[0], expected: expectedStringOne, stream: true},
157 {input: streamData[1], expected: expectedStringTwo, stream: true},
158 {input: streamData[2], expected: expectedStringThree, stream: true},
159 ], msg: "decode() stream test one."});
160
161 testCharset({encoding: "iso-8859-11", array: [
162 {input: streamData[0], expected: expectedStringOne, stream: true},
163 {input: streamData[1], expected: expectedStringTwo},
164 {input: streamData[2], expected: expectedStringThree},
165 ], msg: "decode() stream test two."});
166
167 testCharset({encoding: "utf-8", array: [
168 {input: [0xC2], expected: "\uFFFD"},
169 {input: [0x80], expected: "\uFFFD"},
170 ], msg: "decode() stream test utf-8."});
171
172 testCharset({encoding: "utf-8", fatal: true, array: [
173 {input: [0xC2], error: "EncodingError"},
174 {input: [0x80], error: "EncodingError"},
175 ], msg: "decode() stream test utf-8 fatal."});
176 }
177
178 function testDecodeStreamCompositions() {
179 var tests = [
180 {encoding: "utf-8", input: [0xC2,0x80], expected: ["","\x80"]},
181 {encoding: "utf-8", input: [0xEF,0xBB,0xBF,0xC2,0x80], expected: ["","","","","\x80"]},
182 {encoding: "utf-16", input: [0x01,0x00], expected: ["","\x01"]},
183 {encoding: "utf-16", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]},
184 {encoding: "utf-16", input: [0xFF,0xFD], expected: ["","\uFDFF"]},
185 {encoding: "utf-16", input: [0xFF,0xFE], expected: ["",""]},
186 {encoding: "utf-16", input: [0xFF,0xFF], expected: ["","\uFFFF"]},
187 {encoding: "utf-16", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]},
188 {encoding: "utf-16", input: [0xFF,0xFE,0xFF,0xFE], expected: ["","","","\uFEFF"]},
189 {encoding: "utf-16", input: [0xFF,0xFE,0xFE,0xFF], expected: ["","","","\uFFFE"]},
190 {encoding: "utf-16", input: [0xFD,0xFE], expected: ["","\uFEFD"]},
191 {encoding: "utf-16", input: [0xFD,0xFF], expected: ["","\uFFFD"]},
192 {encoding: "utf-16", input: [0xFE,0xFD], expected: ["","\uFDFE"]},
193 {encoding: "utf-16", input: [0xFE,0xFE], expected: ["","\uFEFE"]},
194 {encoding: "utf-16", input: [0xFE,0xFF], expected: ["","\uFFFE"]},
195 {encoding: "utf-16", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]},
196 {encoding: "utf-16", input: [0xFE,0xFF,0xFF,0xFE], expected: ["","\uFFFE","","\uFEFF"]},
197 {encoding: "utf-16", input: [0xFE,0xFF,0xFE,0xFF], expected: ["","\uFFFE","","\uFFFE"]},
198 {encoding: "utf-16le", input: [0x01,0x00], expected: ["","\x01"]},
199 {encoding: "utf-16le", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]},
200 {encoding: "utf-16le", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]},
201 {encoding: "utf-16le", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]},
202 {encoding: "utf-16be", input: [0x01,0x00], expected: ["","\u0100"]},
203 {encoding: "utf-16be", input: [0x01,0x00,0x03,0x02], expected: ["","\u0100","","\u0302"]},
204 {encoding: "utf-16be", input: [0xFD,0xFE], expected: ["","\uFDFE"]},
205 {encoding: "utf-16be", input: [0xFD,0xFF], expected: ["","\uFDFF"]},
206 {encoding: "utf-16be", input: [0xFE,0xFD], expected: ["","\uFEFD"]},
207 {encoding: "utf-16be", input: [0xFE,0xFE], expected: ["","\uFEFE"]},
208 {encoding: "utf-16be", input: [0xFE,0xFF], expected: ["",""]},
209 {encoding: "utf-16be", input: [0xFE,0xFF,0x01,0x00], expected: ["","","","\u0100"]},
210 {encoding: "utf-16be", input: [0xFF,0xFD], expected: ["","\uFFFD"]},
211 {encoding: "utf-16be", input: [0xFF,0xFE], expected: ["","\uFFFE"]},
212 {encoding: "utf-16be", input: [0xFF,0xFF], expected: ["","\uFFFF"]},
213 {encoding: "utf-16be", input: [0xFF,0xFE,0x01,0x00], expected: ["","\uFFFE","","\u0100"]},
214 {encoding: "shift_jis", input: [0x81,0x40], expected: ["","\u3000"]},
215 ];
216 tests.forEach(function(t) {
217 (function generateCompositions(a, n) {
218 a.push(n);
219 var l = a.length - 1;
220 var array=[];
221 for (var i = 0, o = 0; i <= l; i++) {
222 array.push({
223 input: t.input.slice(o, o+a[i]),
224 expected: t.expected.slice(o, o+=a[i]).join(""),
225 stream: i < l
226 });
227 }
228 testCharset({encoding: t.encoding, array: array,
229 msg: "decode() stream test " + t.encoding + " " + a.join("-") + "."});
230 while (a[l] > 1) {
231 a[l]--;
232 generateCompositions(a.slice(0), n - a[l]);
233 }
234 })([], t.input.length);
235 });
236 }
237
238 /*
239 * function testDecodeABVOption()
240 *
241 * - ABV for ArrayBufferView
242 * - fatal remains null for the entire test
243 * - encoding remains as "iso-8859-11"
244 * - The stream option is modified for this test.
245 * - ArrayBufferView is modified for this test.
246 */
247 function testDecodeABVOption(data, expectedString)
248 {
249 // valid data
250 testCharset({encoding: "iso-8859-11", input: data, expected: expectedString,
251 msg: "decode test ABV valid data."});
252
253 // invalid empty data
254 testCharset({encoding: "iso-8859-11", input: [], expected: "",
255 msg: "decode test ABV empty data."});
256
257 // spaces
258 testCharset({encoding: "iso-8859-11", input: ["\u0020\u0020"], expected: "\0",
259 msg: "text decoding ABV string test."});
260
261 testCharset({encoding: "iso-8859-11", input: [""], expected: "\0",
262 msg: "text decoding ABV empty string test."});
263
264 // null for Array Buffer
265 testCharset({encoding: "iso-8859-11", input: null, error: "TypeError",
266 msg: "text decoding ABV null test."});
267 }
268
269 function testDecodeGreek()
270 {
271 var data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
272 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4,
273 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
274 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca,
275 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd3, 0xd4, 0xd5, 0xd6,
276 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1,
277 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec,
278 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
279 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe];
280
281 var expectedString = "\u00a0\u2018\u2019\u00a3\u20ac\u20af\u00a6\u00a7\u00a8"
282 + "\u00a9\u037a\u00ab\u00ac\u00ad\u2015\u00b0\u00b1"
283 + "\u00b2\u00b3\u0384\u0385\u0386\u00b7\u0388\u0389"
284 + "\u038a\u00bb\u038c\u00bd\u038e\u038f\u0390\u0391"
285 + "\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399"
286 + "\u039a\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1"
287 + "\u03a3\u03a4\u03a5\u03a6\u03a7\u03a8\u03a9\u03aa"
288 + "\u03ab\u03ac\u03ad\u03ae\u03af\u03b0\u03b1\u03b2"
289 + "\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba"
290 + "\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2"
291 + "\u03c3\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9\u03ca"
292 + "\u03cb\u03cc\u03cd\u03ce";
293
294 testCharset({encoding: "greek", input: data, expected: expectedString,
295 msg: "decode greek test."});
296 }
297
298 function testDecoderForThaiEncoding()
299 {
300 // TEST One
301 const data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb];
302
303 const expectedString = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a\u0e5b";
304
305 const aliases = [ "ISO-8859-11", "iso-8859-11", "iso8859-11", "iso885911" ];
306
307 testCharset({encoding: "iso-8859-11", input: data, expected: expectedString,
308 msg: "decoder testing valid ISO-8859-11 encoding."});
309 }
310
311 function testDecoderGetEncoding()
312 {
313 var labelEncodings = [
314 {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]},
315 {encoding: "ibm866", labels: ["866", "cp866", "csibm866", "ibm866"]},
316 {encoding: "iso-8859-2", labels: ["csisolatin2", "iso-8859-2", "iso-ir-101", "iso8859-2", "iso88592", "iso_8859-2", "iso_8859-2:1987", "l2", "latin2"]},
317 {encoding: "iso-8859-3", labels: ["csisolatin3", "iso-8859-3", "iso-ir-109", "iso8859-3", "iso88593", "iso_8859-3", "iso_8859-3:1988", "l3", "latin3"]},
318 {encoding: "iso-8859-4", labels: ["csisolatin4", "iso-8859-4", "iso-ir-110", "iso8859-4", "iso88594", "iso_8859-4", "iso_8859-4:1988", "l4", "latin4"]},
319 {encoding: "iso-8859-5", labels: ["csisolatincyrillic", "cyrillic", "iso-8859-5", "iso-ir-144", "iso8859-5", "iso88595", "iso_8859-5", "iso_8859-5:1988"]},
320 {encoding: "iso-8859-6", labels: ["arabic", "asmo-708", "csiso88596e", "csiso88596i", "csisolatinarabic", "ecma-114", "iso-8859-6", "iso-8859-6-e", "iso-8859-6-i", "iso-ir-127", "iso8859-6", "iso88596", "iso_8859-6", "iso_8859-6:1987"]},
321 {encoding: "iso-8859-7", labels: ["csisolatingreek", "ecma-118", "elot_928", "greek", "greek8", "iso-8859-7", "iso-ir-126", "iso8859-7", "iso88597", "iso_8859-7", "iso_8859-7:1987", "sun_eu_greek"]},
322 {encoding: "iso-8859-8", labels: ["csiso88598e", "csisolatinhebrew", "hebrew", "iso-8859-8", "iso-8859-8-e", "iso-ir-138", "iso8859-8", "iso88598", "iso_8859-8", "iso_8859-8:1988", "visual"]},
323 {encoding: "iso-8859-8-i", labels: ["csiso88598i", "iso-8859-8-i", "logical"]},
324 {encoding: "iso-8859-10", labels: ["csisolatin6", "iso-8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"]},
325 {encoding: "iso-8859-13", labels: ["iso-8859-13", "iso8859-13", "iso885913"]},
326 {encoding: "iso-8859-14", labels: ["iso-8859-14", "iso8859-14", "iso885914"]},
327 {encoding: "iso-8859-15", labels: ["csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"]},
328 {encoding: "iso-8859-16", labels: ["iso-8859-16"]},
329 {encoding: "koi8-r", labels: ["cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"]},
330 {encoding: "koi8-u", labels: ["koi8-u"]},
331 {encoding: "macintosh", labels: ["csmacintosh", "mac", "macintosh", "x-mac-roman"]},
332 {encoding: "windows-874", labels: ["dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"]},
333 {encoding: "windows-1250", labels: ["cp1250", "windows-1250", "x-cp1250"]},
334 {encoding: "windows-1251", labels: ["cp1251", "windows-1251", "x-cp1251"]},
335 {encoding: "windows-1252", labels: ["ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csisolatin1", "ibm819", "iso-8859-1", "iso-ir-100", "iso8859-1", "iso88591", "iso_8859-1", "iso_8859-1:1987", "l1", "latin1", "us-ascii", "windows-1252", "x-cp1252"]},
336 {encoding: "windows-1253", labels: ["cp1253", "windows-1253", "x-cp1253"]},
337 {encoding: "windows-1254", labels: ["cp1254", "csisolatin5", "iso-8859-9", "iso-ir-148", "iso8859-9", "iso88599", "iso_8859-9", "iso_8859-9:1989", "l5", "latin5", "windows-1254", "x-cp1254"]},
338 {encoding: "windows-1255", labels: ["cp1255", "windows-1255", "x-cp1255"]},
339 {encoding: "windows-1256", labels: ["cp1256", "windows-1256", "x-cp1256"]},
340 {encoding: "windows-1257", labels: ["cp1257", "windows-1257", "x-cp1257"]},
341 {encoding: "windows-1258", labels: ["cp1258", "windows-1258", "x-cp1258"]},
342 {encoding: "x-mac-cyrillic", labels: ["x-mac-cyrillic", "x-mac-ukrainian"]},
343 {encoding: "gbk", labels: ["chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"]},
344 {encoding: "gb18030", labels: ["gb18030"]},
345 {encoding: "hz-gb-2312", labels: ["hz-gb-2312"]},
346 {encoding: "big5", labels: ["big5", "cn-big5", "csbig5", "x-x-big5"]},
347 {encoding: "big5-hkscs", labels: ["big5-hkscs"]},
348 {encoding: "euc-jp", labels: ["cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"]},
349 {encoding: "iso-2022-jp", labels: ["csiso2022jp", "iso-2022-jp"]},
350 {encoding: "shift_jis", labels: ["csshiftjis", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"]},
351 {encoding: "euc-kr", labels: ["cseuckr", "csksc56011987", "euc-kr", "iso-ir-149", "korean", "ks_c_5601-1987", "ks_c_5601-1989", "ksc5601", "ksc_5601", "windows-949"]},
352 {encoding: "utf-16le", labels: ["utf-16", "utf-16le"]},
353 {encoding: "utf-16be", labels: ["utf-16be"]},
354 {encoding: "x-user-defined", labels: ["x-user-defined"]},
355 {error: "TypeError", labels: ["x-windows-949", "\u0130SO-8859-1", "csiso2022kr", "iso-2022-kr", "iso-2022-cn", "iso-2022-cn-ext", "replacement"]},
356 ];
357
358 for (var le of labelEncodings) {
359 for (var label of le.labels) {
360 try {
361 var decoder = new TextDecoder(label);
362 } catch (e) {
363 assert_true(!!le.error, label + " shoud not throw " + e.name);
364 assert_equals(e.name, le.error, label + " label encoding unsupported test.");
365 continue;
366 }
367 assert_true(!le.error, label + " shoud throw " + le.error);
368 assert_equals(decoder.encoding, le.encoding, label + " label encoding test.");
369 }
370 }
371 }
372
373 function testCharset(test)
374 {
375 try {
376 var fatal = test.fatal ? {fatal: test.fatal} : null;
377 var decoder = new TextDecoder(test.encoding, fatal);
378 } catch (e) {
379 assert_equals(e.name, test.error, test.msg + " error thrown from the constructor.");
380 return;
381 }
382
383 var array = test.array || [test];
384 var num_strings = array.length;
385 for (var i = 0; i < num_strings; i++) {
386 var decodeView = array[i].input !== null ? new Uint8Array(array[i].input) : null;
387 var stream = array[i].stream ? {stream: array[i].stream} : null;
388 var outText;
389 try {
390 outText = decoder.decode(decodeView, stream);
391 } catch (e) {
392 assert_equals(e.name, array[i].error, test.msg + " error thrown from decode().");
393 return;
394 }
395
396 var expected = array[i].expected;
397 if (outText !== expected) {
398 assert_equals(escape(outText), escape(expected), test.msg + " Code points do not match expected code points.");
399 break;
400 }
401 }
402 assert_true(!test.error, test.msg);
403 }
404
405 function testInvalid2022JP()
406 {
407 var inputs = [
408 [0x80],
409 [0x1b, 0xFF],
410 [0x1b, 0x28, 0xFF],
411 [0x1b, 0x24, 0x80],
412 [0x1b, 0x24, 0x28, 0x80],
413 [0x1b, 0x28, 0x4a, 0xFF],
414 [0x1b, 0x28, 0x49, 0xFF],
415 [0x1b, 0x24, 0x40, 0x20],
416 [0x1b, 0x24, 0x41, 0x20],
417 [0x1b, 0x24, 0x42, 0x20],
418 [0x1b, 0x24, 0x28, 0x43, 0x20],
419 [0x1b, 0x24, 0x28, 0x44, 0x20],
420 [0x1b, 0x24, 0x40, 0x80, 0x21],
421 [0x1b, 0x24, 0x41, 0xFF, 0x21],
422 [0x1b, 0x24, 0x42, 0x80, 0x21],
423 [0x1b, 0x24, 0x28, 0x43, 0xFF, 0x21],
424 [0x1b, 0x24, 0x28, 0x44, 0x80, 0x21],
425 [0x1b, 0x24, 0x40, 0x21, 0x20],
426 [0x1b, 0x24, 0x41, 0x21, 0x20],
427 [0x1b, 0x24, 0x42, 0x21, 0x20],
428 [0x1b, 0x24, 0x28, 0x43, 0x21, 0x20],
429 [0x1b, 0x24, 0x28, 0x44, 0x21, 0x20],
430 [0x1b, 0x2e, 0xFF],
431 [0x1b, 0x4e, 0x20],
432 [0x1b, 0x4e, 0x7F],
433 [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0x80],
434 [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0xFF],
435 ];
436
437 var failureCount = 0;
438 inputs.forEach(function(input) {
439 try {
440 // decode() should never throw unless {fatal: true} is specified
441 (new TextDecoder("iso-2022-jp")).decode(new Uint8Array(input));
442 } catch (e) {
443 if (e.name !== "EncodingError") {
444 throw e;
445 }
446 failureCount++;
447 }
448 });
449 assert_equals(failureCount, 0, failureCount + " of " + inputs.length + " tests failed");
450 }

mercurial