11cb0ef41Sopenharmony_ci<!doctype html>
21cb0ef41Sopenharmony_ci<meta charset=gb18030>
31cb0ef41Sopenharmony_ci<script src=/resources/testharness.js></script>
41cb0ef41Sopenharmony_ci<script src=/resources/testharnessreport.js></script>
51cb0ef41Sopenharmony_ci<script src=resources/ranges.js></script>
61cb0ef41Sopenharmony_ci<script>
71cb0ef41Sopenharmony_ci const encode = (input, output, desc) => {
81cb0ef41Sopenharmony_ci   test(function() {
91cb0ef41Sopenharmony_ci     const a = document.createElement("a"); // <a> uses document encoding for URL's query
101cb0ef41Sopenharmony_ci     a.href = "https://example.com/?" + input;
111cb0ef41Sopenharmony_ci     assert_equals(a.search.substr(1), output); // remove leading "?"
121cb0ef41Sopenharmony_ci   }, "gb18030 encoder: " + desc);
131cb0ef41Sopenharmony_ci }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci encode("s", "s", "very basic");
161cb0ef41Sopenharmony_ci encode("\u20AC", "%A2%E3", "Euro");
171cb0ef41Sopenharmony_ci encode("\u4E02", "%81@", "character");
181cb0ef41Sopenharmony_ci encode("\uE4C6", "%A1@", "PUA");
191cb0ef41Sopenharmony_ci encode("\uE4C5", "%FE%FE", "PUA #2");
201cb0ef41Sopenharmony_ci encode("\uE5E5", "%26%2358853%3B", "PUA #3");
211cb0ef41Sopenharmony_ci encode("\ud83d\udca9", "%949%DA3", "poo");
221cb0ef41Sopenharmony_ci encode("\uE7C7", "%815%F47", "Ranges pointer special case");
231cb0ef41Sopenharmony_ci encode("\uE7C8", "%836%C80", "legacy ICU special case 1");
241cb0ef41Sopenharmony_ci encode("\u2026", "%A1%AD", "legacy ICU special case 2");
251cb0ef41Sopenharmony_ci encode("\uFF5E", "%A1%AB", "legacy ICU special case 3");
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci const upperCaseNibble = x => {
281cb0ef41Sopenharmony_ci   return Math.floor(x).toString(16).toUpperCase();
291cb0ef41Sopenharmony_ci }
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci const encodePointer = pointer => {
321cb0ef41Sopenharmony_ci   const firstByte = Math.floor(pointer / 12600) + 0x81;
331cb0ef41Sopenharmony_ci   const thirdByte = Math.floor((pointer % 1260) / 10) + 0x81;
341cb0ef41Sopenharmony_ci   return "%"
351cb0ef41Sopenharmony_ci     + upperCaseNibble(firstByte / 16)
361cb0ef41Sopenharmony_ci     + upperCaseNibble(firstByte % 16)
371cb0ef41Sopenharmony_ci     + String.fromCharCode(Math.floor((pointer % 12600) / 1260) + 0x30)
381cb0ef41Sopenharmony_ci     + "%"
391cb0ef41Sopenharmony_ci     + upperCaseNibble(thirdByte / 16)
401cb0ef41Sopenharmony_ci     + upperCaseNibble(thirdByte % 16)
411cb0ef41Sopenharmony_ci     + String.fromCharCode(pointer % 10 + 0x30);
421cb0ef41Sopenharmony_ci }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci let i = 0;
451cb0ef41Sopenharmony_ci for (const range of ranges) {
461cb0ef41Sopenharmony_ci   encode(range[1], encodePointer(range[0]), "range " + i++);
471cb0ef41Sopenharmony_ci }
481cb0ef41Sopenharmony_ci</script>
49