{"size": 1446, "ext": "js", "max_stars_repo_path": "url/percent-encoding.window.js", "max_stars_repo_name": "meyerweb/wpt", "max_stars_repo_stars_event_min_datetime": "2015-01-01T01:57:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T23:33:32.000Z", "max_issues_repo_path": "url/percent-encoding.window.js", "max_issues_repo_name": "meyerweb/wpt", "max_issues_repo_issues_event_min_datetime": "2018-05-28T09:38:03.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T20:55:48.000Z", "max_forks_repo_path": "url/percent-encoding.window.js", "max_forks_repo_name": "meyerweb/wpt", "max_forks_repo_forks_event_min_datetime": "2015-01-02T11:32:21.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T16:35:46.000Z"}
{"max_stars_count": 14668, "max_issues_count": 7642, "max_forks_count": 5941, "avg_line_length": 42.5294117647, "max_line_length": 117, "alphanum_fraction": 0.6583679115}
promise_test(() => fetch("resources/percent-encoding.json").then(res => res.json()).then(runTests), "Loading data…");

function runTests(testUnits) {
  for (const testUnit of testUnits) {
    // Ignore comments
    if (typeof testUnit === "string") {
      continue;
    }
    for (const encoding of Object.keys(testUnit.output)) {
      async_test(t => {
        const frame = document.body.appendChild(document.createElement("iframe"));
        t.add_cleanup(() => frame.remove());
        frame.onload = t.step_func_done(() => {
          const output = frame.contentDocument.querySelector("a");
          // Test that the fragment is always UTF-8 encoded
          assert_equals(output.hash, `#${testUnit.output["utf-8"]}`, "fragment");
          assert_equals(output.search, `?${testUnit.output[encoding]}`, "query");
        });
        frame.src = `resources/percent-encoding.py?encoding=${encoding}&value=${toBase64(testUnit.input)}`;
      }, `Input ${testUnit.input} with encoding ${encoding}`);
    }
  }
}

// Use base64 to avoid relying on the URL parser to get UTF-8 percent-encoding correctly. This does
// not use btoa directly as that only works with code points in the range U+0000 to U+00FF,
// inclusive.
function toBase64(input) {
  const bytes = new TextEncoder().encode(input);
  const byteString = Array.from(bytes, byte => String.fromCharCode(byte)).join("");
  const encoded = self.btoa(byteString);
  return encoded;
}
