<!DOCTYPE html>
<html>
<head>
<meta charset=big5> <!-- test breaks if the server overrides this -->
<title>Big5 encoding ASCII</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel='author' title='Richard Ishida' href='mailto:ishida@w3.org'>
<link rel='help' href='https://encoding.spec.whatwg.org/#big5'>
<meta name="assert" content="The browser produces the characters when encoding ASCII in a Big5-encoded document.">
</head>
<body>
<div id=log></div>
<script>
// index is Big5 index pointer, value is Unicode codepoint (dec)

function encode(input, output, desc) {
  // tests whether a Unicode character is converted to an equivalent Big5 %-encoded byte sequence by href
  // input: a escaped Unicode code point from the list of characters in the Big5 index
  // output: expected percent-encoded byte sequence for the code point's equivalent in Big5 encoding
  // desc: what's being tested
  test(function() {
    var a = document.createElement("a"); // <a> uses document encoding for URL's query
    // Append and prepend x to test for off-by-one errors
    a.href = "https://example.com/?x" + input + "x";
    assert_true(
      a.search.substr(1) == "x" + output + "x" ||
        a.search.substr(1) == "x" + input + "x"
    ); // remove leading "?"
  }, "big5 encoder: " + desc);
}

// test ASCII - test separately for chars that aren't escaped
for (var a = 0; a < 0x7f; a++) {
  // The first 3 are stripped from URLs and the last is # which introduces a new URL segment
  if (a === 0x09 || a === 0x0a || a === 0x0d || a === 0x23) {
    continue;
  }
  hex = a.toString(16).toUpperCase();
  while (hex.length < 2) {
    hex = "0" + hex;
  }
  encode(
    String.fromCharCode(a),
    "%" + hex,
    "test for ASCII codepoint 0x" +
      a.toString(16) +
      " " +
      String.fromCharCode(a)
  );
}
</script>
</body>
</html>
