dom/encoding/test/test_TextDecoder.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/encoding/test/test_TextDecoder.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,450 @@
     1.4 +/*
     1.5 + * test_TextDecoderOptions.js
     1.6 + * bug 764234 tests
     1.7 +*/
     1.8 +
     1.9 +function runTextDecoderOptions()
    1.10 +{
    1.11 +  const data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
    1.12 +                0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
    1.13 +                0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
    1.14 +                0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
    1.15 +                0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
    1.16 +                0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
    1.17 +                0xda, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
    1.18 +                0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
    1.19 +                0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb];
    1.20 +
    1.21 +  const expectedString = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07"
    1.22 +                         + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f"
    1.23 +                         + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17"
    1.24 +                         + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f"
    1.25 +                         + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27"
    1.26 +                         + "\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f"
    1.27 +                         + "\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37"
    1.28 +                         + "\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43"
    1.29 +                         + "\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b"
    1.30 +                         + "\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53"
    1.31 +                         + "\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a\u0e5b";
    1.32 +
    1.33 +  test(testDecoderGetEncoding, "testDecoderGetEncoding");
    1.34 +  test(testDecodeGreek, "testDecodeGreek");
    1.35 +  test(function() {
    1.36 +    testConstructorFatalOption(data, expectedString);
    1.37 +  }, "testConstructorFatalOption");
    1.38 +  test(function() {
    1.39 +    testConstructorEncodingOption(data, expectedString);
    1.40 +  }, "testConstructorEncodingOption");
    1.41 +  test(function() {
    1.42 +    testDecodeStreamOption(data, expectedString);
    1.43 +  }, "testDecodeStreamOption");
    1.44 +  test(testDecodeStreamCompositions, "testDecodeStreamCompositions");
    1.45 +  test(function() {
    1.46 +    testDecodeABVOption(data, expectedString);
    1.47 +  }, "testDecodeABVOption");
    1.48 +  test(testDecoderForThaiEncoding, "testDecoderForThaiEncoding");
    1.49 +  test(testInvalid2022JP, "testInvalid2022JP");
    1.50 +}
    1.51 +
    1.52 +/*
    1.53 +* function testConstructor()
    1.54 +*
    1.55 +* - This function tests the constructor optional arguments.
    1.56 +* - Stream option remains null for this test.
    1.57 +* - The stream option is passed to the decode function.
    1.58 +* - This function is not testing the decode function.
    1.59 +*
    1.60 +*/
    1.61 +function testConstructorFatalOption(data, expectedString)
    1.62 +{
    1.63 +  //invalid string to decode passed, fatal = false
    1.64 +  testCharset({fatal: false, encoding: "iso-8859-11", input: [], expected: "",
    1.65 +    msg: "constructor fatal option set to false test."});
    1.66 +
    1.67 +  //invalid string to decode passed, fatal = true
    1.68 +  testCharset({fatal: true, encoding: "iso-8859-11", input: [], expected: "",
    1.69 +    msg: "constructor fatal option set to true test."});
    1.70 +}
    1.71 +
    1.72 +function testConstructorEncodingOption(aData, aExpectedString)
    1.73 +{
    1.74 +  // valid encoding passed
    1.75 +  testCharset({encoding: "iso-8859-11", input: aData, expected: aExpectedString,
    1.76 +    msg: "decoder testing constructor valid encoding."});
    1.77 +
    1.78 +  // invalid encoding passed
    1.79 +  testCharset({encoding: "asdfasdf", input: aData, error: "TypeError",
    1.80 +    msg: "constructor encoding, invalid encoding test."});
    1.81 +
    1.82 +  // passing spaces for encoding
    1.83 +  testCharset({encoding: "   ", input: aData, error: "TypeError",
    1.84 +    msg: "constructor encoding, spaces encoding test."});
    1.85 +
    1.86 +  // passing null for encoding
    1.87 +  testCharset({encoding: null, input: aData, error: "TypeError",
    1.88 +    msg: "constructor encoding, \"null\" encoding test."});
    1.89 +
    1.90 +  // empty encoding passed
    1.91 +  testCharset({encoding: "", input: aData, error: "TypeError",
    1.92 +    msg: "constuctor encoding, empty encoding test."});
    1.93 +
    1.94 +  // replacement character test
    1.95 +  aExpectedString = "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
    1.96 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
    1.97 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
    1.98 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
    1.99 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.100 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.101 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.102 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.103 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.104 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"
   1.105 +                  + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd";
   1.106 +  testCharset({encoding: "utf-8", input: aData, expected: aExpectedString,
   1.107 +    msg: "constuctor encoding, utf-8 test."});
   1.108 +}
   1.109 +
   1.110 +/*
   1.111 +* function testDecodeStreamOption()
   1.112 +*
   1.113 +* - fatal remains null for the entire test
   1.114 +* - encoding remains as "iso-8859-11"
   1.115 +* - The stream option is modified for this test.
   1.116 +* - ArrayBufferView is modified for this test.
   1.117 +*/
   1.118 +function testDecodeStreamOption(data, expectedString)
   1.119 +{
   1.120 +  const streamData = [[0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
   1.121 +                      0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
   1.122 +                      0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8,
   1.123 +                      0xb9, 0xba, 0xbb, 0xbc, 0xbd],
   1.124 +                      [0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
   1.125 +                      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce,
   1.126 +                      0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
   1.127 +                      0xd8, 0xd9, 0xda, 0xdf, 0xe0, 0xe1, 0xe2],
   1.128 +                      [0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
   1.129 +                      0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
   1.130 +                      0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb]];
   1.131 +
   1.132 +  const expectedStringOne = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07"
   1.133 +                            + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f"
   1.134 +                            + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17"
   1.135 +                            + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d";
   1.136 +  const expectedStringTwo = "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25"
   1.137 +                            + "\u0e26\u0e27\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d"
   1.138 +                            + "\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35"
   1.139 +                            + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41"
   1.140 +                            + "\u0e42";
   1.141 +  const expectedStringThree = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a"
   1.142 +                              + "\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51"
   1.143 +                              + "\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58"
   1.144 +                              + "\u0e59\u0e5a\u0e5b";
   1.145 +  expectedString = [expectedStringOne, expectedStringTwo, expectedStringThree];
   1.146 +
   1.147 +  // streaming test
   1.148 +
   1.149 +  /* - the streaming is null
   1.150 +  *  - streaming is not set in the decode function
   1.151 +  */
   1.152 +  testCharset({encoding: "iso-8859-11", array: [
   1.153 +    {input: streamData[0], expected: expectedStringOne},
   1.154 +    {input: streamData[1], expected: expectedStringTwo},
   1.155 +    {input: streamData[2], expected: expectedStringThree},
   1.156 +  ], msg: "decode() stream test zero."});
   1.157 +
   1.158 +  testCharset({encoding: "iso-8859-11", array: [
   1.159 +    {input: streamData[0], expected: expectedStringOne, stream: true},
   1.160 +    {input: streamData[1], expected: expectedStringTwo, stream: true},
   1.161 +    {input: streamData[2], expected: expectedStringThree, stream: true},
   1.162 +  ], msg: "decode() stream test one."});
   1.163 +
   1.164 +  testCharset({encoding: "iso-8859-11", array: [
   1.165 +    {input: streamData[0], expected: expectedStringOne, stream: true},
   1.166 +    {input: streamData[1], expected: expectedStringTwo},
   1.167 +    {input: streamData[2], expected: expectedStringThree},
   1.168 +  ], msg: "decode() stream test two."});
   1.169 +
   1.170 +  testCharset({encoding: "utf-8", array: [
   1.171 +    {input: [0xC2], expected: "\uFFFD"},
   1.172 +    {input: [0x80], expected: "\uFFFD"},
   1.173 +  ], msg: "decode() stream test utf-8."});
   1.174 +
   1.175 +  testCharset({encoding: "utf-8", fatal: true, array: [
   1.176 +    {input: [0xC2], error: "EncodingError"},
   1.177 +    {input: [0x80], error: "EncodingError"},
   1.178 +  ], msg: "decode() stream test utf-8 fatal."});
   1.179 +}
   1.180 +
   1.181 +function testDecodeStreamCompositions() {
   1.182 +  var tests = [
   1.183 +    {encoding: "utf-8", input: [0xC2,0x80], expected: ["","\x80"]},
   1.184 +    {encoding: "utf-8", input: [0xEF,0xBB,0xBF,0xC2,0x80], expected: ["","","","","\x80"]},
   1.185 +    {encoding: "utf-16", input: [0x01,0x00], expected: ["","\x01"]},
   1.186 +    {encoding: "utf-16", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]},
   1.187 +    {encoding: "utf-16", input: [0xFF,0xFD], expected: ["","\uFDFF"]},
   1.188 +    {encoding: "utf-16", input: [0xFF,0xFE], expected: ["",""]},
   1.189 +    {encoding: "utf-16", input: [0xFF,0xFF], expected: ["","\uFFFF"]},
   1.190 +    {encoding: "utf-16", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]},
   1.191 +    {encoding: "utf-16", input: [0xFF,0xFE,0xFF,0xFE], expected: ["","","","\uFEFF"]},
   1.192 +    {encoding: "utf-16", input: [0xFF,0xFE,0xFE,0xFF], expected: ["","","","\uFFFE"]},
   1.193 +    {encoding: "utf-16", input: [0xFD,0xFE], expected: ["","\uFEFD"]},
   1.194 +    {encoding: "utf-16", input: [0xFD,0xFF], expected: ["","\uFFFD"]},
   1.195 +    {encoding: "utf-16", input: [0xFE,0xFD], expected: ["","\uFDFE"]},
   1.196 +    {encoding: "utf-16", input: [0xFE,0xFE], expected: ["","\uFEFE"]},
   1.197 +    {encoding: "utf-16", input: [0xFE,0xFF], expected: ["","\uFFFE"]},
   1.198 +    {encoding: "utf-16", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]},
   1.199 +    {encoding: "utf-16", input: [0xFE,0xFF,0xFF,0xFE], expected: ["","\uFFFE","","\uFEFF"]},
   1.200 +    {encoding: "utf-16", input: [0xFE,0xFF,0xFE,0xFF], expected: ["","\uFFFE","","\uFFFE"]},
   1.201 +    {encoding: "utf-16le", input: [0x01,0x00], expected: ["","\x01"]},
   1.202 +    {encoding: "utf-16le", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]},
   1.203 +    {encoding: "utf-16le", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]},
   1.204 +    {encoding: "utf-16le", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]},
   1.205 +    {encoding: "utf-16be", input: [0x01,0x00], expected: ["","\u0100"]},
   1.206 +    {encoding: "utf-16be", input: [0x01,0x00,0x03,0x02], expected: ["","\u0100","","\u0302"]},
   1.207 +    {encoding: "utf-16be", input: [0xFD,0xFE], expected: ["","\uFDFE"]},
   1.208 +    {encoding: "utf-16be", input: [0xFD,0xFF], expected: ["","\uFDFF"]},
   1.209 +    {encoding: "utf-16be", input: [0xFE,0xFD], expected: ["","\uFEFD"]},
   1.210 +    {encoding: "utf-16be", input: [0xFE,0xFE], expected: ["","\uFEFE"]},
   1.211 +    {encoding: "utf-16be", input: [0xFE,0xFF], expected: ["",""]},
   1.212 +    {encoding: "utf-16be", input: [0xFE,0xFF,0x01,0x00], expected: ["","","","\u0100"]},
   1.213 +    {encoding: "utf-16be", input: [0xFF,0xFD], expected: ["","\uFFFD"]},
   1.214 +    {encoding: "utf-16be", input: [0xFF,0xFE], expected: ["","\uFFFE"]},
   1.215 +    {encoding: "utf-16be", input: [0xFF,0xFF], expected: ["","\uFFFF"]},
   1.216 +    {encoding: "utf-16be", input: [0xFF,0xFE,0x01,0x00], expected: ["","\uFFFE","","\u0100"]},
   1.217 +    {encoding: "shift_jis", input: [0x81,0x40], expected: ["","\u3000"]},
   1.218 +  ];
   1.219 +  tests.forEach(function(t) {
   1.220 +    (function generateCompositions(a, n) {
   1.221 +      a.push(n);
   1.222 +      var l = a.length - 1;
   1.223 +      var array=[];
   1.224 +      for (var i = 0, o = 0; i <= l; i++) {
   1.225 +        array.push({
   1.226 +          input: t.input.slice(o, o+a[i]),
   1.227 +          expected: t.expected.slice(o, o+=a[i]).join(""),
   1.228 +          stream: i < l
   1.229 +        });
   1.230 +      }
   1.231 +      testCharset({encoding: t.encoding, array: array,
   1.232 +        msg: "decode() stream test " + t.encoding + " " + a.join("-") + "."});
   1.233 +      while (a[l] > 1) {
   1.234 +        a[l]--;
   1.235 +        generateCompositions(a.slice(0), n - a[l]);
   1.236 +      }
   1.237 +    })([], t.input.length);
   1.238 +  });
   1.239 +}
   1.240 +
   1.241 +/*
   1.242 +* function testDecodeABVOption()
   1.243 +*
   1.244 +* - ABV for ArrayBufferView
   1.245 +* - fatal remains null for the entire test
   1.246 +* - encoding remains as "iso-8859-11"
   1.247 +* - The stream option is modified for this test.
   1.248 +* - ArrayBufferView is modified for this test.
   1.249 +*/
   1.250 +function testDecodeABVOption(data, expectedString)
   1.251 +{
   1.252 +  // valid data
   1.253 +  testCharset({encoding: "iso-8859-11", input: data, expected: expectedString,
   1.254 +    msg: "decode test ABV valid data."});
   1.255 +
   1.256 +  // invalid empty data
   1.257 +  testCharset({encoding: "iso-8859-11", input: [], expected: "",
   1.258 +    msg: "decode test ABV empty data."});
   1.259 +
   1.260 +  // spaces
   1.261 +  testCharset({encoding: "iso-8859-11", input: ["\u0020\u0020"], expected: "\0",
   1.262 +    msg: "text decoding ABV string test."});
   1.263 +
   1.264 +  testCharset({encoding: "iso-8859-11", input: [""], expected: "\0",
   1.265 +    msg: "text decoding ABV empty string test."});
   1.266 +
   1.267 +  // null for Array Buffer
   1.268 +  testCharset({encoding: "iso-8859-11", input: null, error: "TypeError",
   1.269 +    msg: "text decoding ABV null test."});
   1.270 +}
   1.271 +
   1.272 +function testDecodeGreek()
   1.273 +{
   1.274 +  var data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
   1.275 +              0xa9, 0xaa, 0xab, 0xac, 0xad, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4,
   1.276 +              0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
   1.277 +              0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca,
   1.278 +              0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd3, 0xd4, 0xd5, 0xd6,
   1.279 +              0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1,
   1.280 +              0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec,
   1.281 +              0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
   1.282 +              0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe];
   1.283 +
   1.284 +  var expectedString = "\u00a0\u2018\u2019\u00a3\u20ac\u20af\u00a6\u00a7\u00a8"
   1.285 +                       + "\u00a9\u037a\u00ab\u00ac\u00ad\u2015\u00b0\u00b1"
   1.286 +                       + "\u00b2\u00b3\u0384\u0385\u0386\u00b7\u0388\u0389"
   1.287 +                       + "\u038a\u00bb\u038c\u00bd\u038e\u038f\u0390\u0391"
   1.288 +                       + "\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399"
   1.289 +                       + "\u039a\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1"
   1.290 +                       + "\u03a3\u03a4\u03a5\u03a6\u03a7\u03a8\u03a9\u03aa"
   1.291 +                       + "\u03ab\u03ac\u03ad\u03ae\u03af\u03b0\u03b1\u03b2"
   1.292 +                       + "\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba"
   1.293 +                       + "\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2"
   1.294 +                       + "\u03c3\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9\u03ca"
   1.295 +                       + "\u03cb\u03cc\u03cd\u03ce";
   1.296 +
   1.297 +  testCharset({encoding: "greek", input: data, expected: expectedString,
   1.298 +    msg: "decode greek test."});
   1.299 +}
   1.300 +
   1.301 +function testDecoderForThaiEncoding()
   1.302 +{
   1.303 +  // TEST One
   1.304 +  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];
   1.305 +
   1.306 +  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";
   1.307 +
   1.308 +  const aliases = [ "ISO-8859-11", "iso-8859-11", "iso8859-11", "iso885911" ];
   1.309 +
   1.310 +  testCharset({encoding: "iso-8859-11", input: data, expected: expectedString,
   1.311 +    msg: "decoder testing valid ISO-8859-11 encoding."});
   1.312 +}
   1.313 +
   1.314 +function testDecoderGetEncoding()
   1.315 +{
   1.316 +  var labelEncodings = [
   1.317 +    {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]},
   1.318 +    {encoding: "ibm866", labels: ["866", "cp866", "csibm866", "ibm866"]},
   1.319 +    {encoding: "iso-8859-2", labels: ["csisolatin2", "iso-8859-2", "iso-ir-101", "iso8859-2", "iso88592", "iso_8859-2", "iso_8859-2:1987", "l2", "latin2"]},
   1.320 +    {encoding: "iso-8859-3", labels: ["csisolatin3", "iso-8859-3", "iso-ir-109", "iso8859-3", "iso88593", "iso_8859-3", "iso_8859-3:1988", "l3", "latin3"]},
   1.321 +    {encoding: "iso-8859-4", labels: ["csisolatin4", "iso-8859-4", "iso-ir-110", "iso8859-4", "iso88594", "iso_8859-4", "iso_8859-4:1988", "l4", "latin4"]},
   1.322 +    {encoding: "iso-8859-5", labels: ["csisolatincyrillic", "cyrillic", "iso-8859-5", "iso-ir-144", "iso8859-5", "iso88595", "iso_8859-5", "iso_8859-5:1988"]},
   1.323 +    {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"]},
   1.324 +    {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"]},
   1.325 +    {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"]},
   1.326 +    {encoding: "iso-8859-8-i", labels: ["csiso88598i", "iso-8859-8-i", "logical"]},
   1.327 +    {encoding: "iso-8859-10", labels: ["csisolatin6", "iso-8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"]},
   1.328 +    {encoding: "iso-8859-13", labels: ["iso-8859-13", "iso8859-13", "iso885913"]},
   1.329 +    {encoding: "iso-8859-14", labels: ["iso-8859-14", "iso8859-14", "iso885914"]},
   1.330 +    {encoding: "iso-8859-15", labels: ["csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"]},
   1.331 +    {encoding: "iso-8859-16", labels: ["iso-8859-16"]},
   1.332 +    {encoding: "koi8-r", labels: ["cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"]},
   1.333 +    {encoding: "koi8-u", labels: ["koi8-u"]},
   1.334 +    {encoding: "macintosh", labels: ["csmacintosh", "mac", "macintosh", "x-mac-roman"]},
   1.335 +    {encoding: "windows-874", labels: ["dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"]},
   1.336 +    {encoding: "windows-1250", labels: ["cp1250", "windows-1250", "x-cp1250"]},
   1.337 +    {encoding: "windows-1251", labels: ["cp1251", "windows-1251", "x-cp1251"]},
   1.338 +    {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"]},
   1.339 +    {encoding: "windows-1253", labels: ["cp1253", "windows-1253", "x-cp1253"]},
   1.340 +    {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"]},
   1.341 +    {encoding: "windows-1255", labels: ["cp1255", "windows-1255", "x-cp1255"]},
   1.342 +    {encoding: "windows-1256", labels: ["cp1256", "windows-1256", "x-cp1256"]},
   1.343 +    {encoding: "windows-1257", labels: ["cp1257", "windows-1257", "x-cp1257"]},
   1.344 +    {encoding: "windows-1258", labels: ["cp1258", "windows-1258", "x-cp1258"]},
   1.345 +    {encoding: "x-mac-cyrillic", labels: ["x-mac-cyrillic", "x-mac-ukrainian"]},
   1.346 +    {encoding: "gbk", labels: ["chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"]},
   1.347 +    {encoding: "gb18030", labels: ["gb18030"]},
   1.348 +    {encoding: "hz-gb-2312", labels: ["hz-gb-2312"]},
   1.349 +    {encoding: "big5", labels: ["big5", "cn-big5", "csbig5", "x-x-big5"]},
   1.350 +    {encoding: "big5-hkscs", labels: ["big5-hkscs"]},
   1.351 +    {encoding: "euc-jp", labels: ["cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"]},
   1.352 +    {encoding: "iso-2022-jp", labels: ["csiso2022jp", "iso-2022-jp"]},
   1.353 +    {encoding: "shift_jis", labels: ["csshiftjis", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"]},
   1.354 +    {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"]},
   1.355 +    {encoding: "utf-16le", labels: ["utf-16", "utf-16le"]},
   1.356 +    {encoding: "utf-16be", labels: ["utf-16be"]},
   1.357 +    {encoding: "x-user-defined", labels: ["x-user-defined"]},
   1.358 +    {error: "TypeError", labels: ["x-windows-949", "\u0130SO-8859-1", "csiso2022kr", "iso-2022-kr", "iso-2022-cn", "iso-2022-cn-ext", "replacement"]},
   1.359 +  ];
   1.360 +
   1.361 +  for (var le of labelEncodings) {
   1.362 +    for (var label of le.labels) {
   1.363 +      try {
   1.364 +        var decoder = new TextDecoder(label);
   1.365 +      } catch (e) {
   1.366 +        assert_true(!!le.error, label + " shoud not throw " + e.name);
   1.367 +        assert_equals(e.name, le.error, label + " label encoding unsupported test.");
   1.368 +        continue;
   1.369 +      }
   1.370 +      assert_true(!le.error, label + " shoud throw " + le.error);
   1.371 +      assert_equals(decoder.encoding, le.encoding, label + " label encoding test.");
   1.372 +    }
   1.373 +  }
   1.374 +}
   1.375 +
   1.376 +function testCharset(test)
   1.377 +{
   1.378 +  try {
   1.379 +    var fatal = test.fatal ? {fatal: test.fatal} : null;
   1.380 +    var decoder = new TextDecoder(test.encoding, fatal);
   1.381 +  } catch (e) {
   1.382 +    assert_equals(e.name, test.error, test.msg + " error thrown from the constructor.");
   1.383 +    return;
   1.384 +  }
   1.385 +
   1.386 +  var array = test.array || [test];
   1.387 +  var num_strings = array.length;
   1.388 +  for (var i = 0; i < num_strings; i++) {
   1.389 +    var decodeView = array[i].input !== null ? new Uint8Array(array[i].input) : null;
   1.390 +    var stream = array[i].stream ? {stream: array[i].stream} : null;
   1.391 +    var outText;
   1.392 +    try {
   1.393 +      outText = decoder.decode(decodeView, stream);
   1.394 +    } catch (e) {
   1.395 +      assert_equals(e.name, array[i].error, test.msg + " error thrown from decode().");
   1.396 +      return;
   1.397 +    }
   1.398 +
   1.399 +    var expected = array[i].expected;
   1.400 +    if (outText !== expected) {
   1.401 +      assert_equals(escape(outText), escape(expected), test.msg + " Code points do not match expected code points.");
   1.402 +      break;
   1.403 +    }
   1.404 +  }
   1.405 +  assert_true(!test.error, test.msg);
   1.406 +}
   1.407 +
   1.408 +function testInvalid2022JP()
   1.409 +{
   1.410 +  var inputs = [
   1.411 +    [0x80],
   1.412 +    [0x1b, 0xFF],
   1.413 +    [0x1b, 0x28, 0xFF],
   1.414 +    [0x1b, 0x24, 0x80],
   1.415 +    [0x1b, 0x24, 0x28, 0x80],
   1.416 +    [0x1b, 0x28, 0x4a, 0xFF],
   1.417 +    [0x1b, 0x28, 0x49, 0xFF],
   1.418 +    [0x1b, 0x24, 0x40, 0x20],
   1.419 +    [0x1b, 0x24, 0x41, 0x20],
   1.420 +    [0x1b, 0x24, 0x42, 0x20],
   1.421 +    [0x1b, 0x24, 0x28, 0x43, 0x20],
   1.422 +    [0x1b, 0x24, 0x28, 0x44, 0x20],
   1.423 +    [0x1b, 0x24, 0x40, 0x80, 0x21],
   1.424 +    [0x1b, 0x24, 0x41, 0xFF, 0x21],
   1.425 +    [0x1b, 0x24, 0x42, 0x80, 0x21],
   1.426 +    [0x1b, 0x24, 0x28, 0x43, 0xFF, 0x21],
   1.427 +    [0x1b, 0x24, 0x28, 0x44, 0x80, 0x21],
   1.428 +    [0x1b, 0x24, 0x40, 0x21, 0x20],
   1.429 +    [0x1b, 0x24, 0x41, 0x21, 0x20],
   1.430 +    [0x1b, 0x24, 0x42, 0x21, 0x20],
   1.431 +    [0x1b, 0x24, 0x28, 0x43, 0x21, 0x20],
   1.432 +    [0x1b, 0x24, 0x28, 0x44, 0x21, 0x20],
   1.433 +    [0x1b, 0x2e, 0xFF],
   1.434 +    [0x1b, 0x4e, 0x20],
   1.435 +    [0x1b, 0x4e, 0x7F],
   1.436 +    [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0x80],
   1.437 +    [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0xFF],
   1.438 +  ];
   1.439 +
   1.440 +  var failureCount = 0;
   1.441 +  inputs.forEach(function(input) {
   1.442 +    try {
   1.443 +      // decode() should never throw unless {fatal: true} is specified
   1.444 +      (new TextDecoder("iso-2022-jp")).decode(new Uint8Array(input));
   1.445 +    } catch (e) {
   1.446 +      if (e.name !== "EncodingError") {
   1.447 +        throw e;
   1.448 +      }
   1.449 +      failureCount++;
   1.450 +    }
   1.451 +  });
   1.452 +  assert_equals(failureCount, 0, failureCount + " of " + inputs.length + " tests failed");
   1.453 +}

mercurial