test_TextDecoder.js 25 KB

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