11cb0ef41Sopenharmony_cipromise_test(() => fetch("resources/IdnaTestV2.json").then(res => res.json()).then(runTests), "Loading data…");
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Performance impact of this seems negligible (performance.now() diff in WebKit went from 48 to 52)
41cb0ef41Sopenharmony_ci// and there was a preference to let more non-ASCII hit the parser.
51cb0ef41Sopenharmony_cifunction encodeHostEndingCodePoints(input) {
61cb0ef41Sopenharmony_ci  let output = "";
71cb0ef41Sopenharmony_ci  for (const codePoint of input) {
81cb0ef41Sopenharmony_ci    if ([":", "/", "?", "#", "\\"].includes(codePoint)) {
91cb0ef41Sopenharmony_ci      output += encodeURIComponent(codePoint);
101cb0ef41Sopenharmony_ci    } else {
111cb0ef41Sopenharmony_ci      output += codePoint;
121cb0ef41Sopenharmony_ci    }
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci  return output;
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction runTests(idnaTests) {
181cb0ef41Sopenharmony_ci  for (const idnaTest of idnaTests) {
191cb0ef41Sopenharmony_ci    if (typeof idnaTest === "string") {
201cb0ef41Sopenharmony_ci      continue // skip comments
211cb0ef41Sopenharmony_ci    }
221cb0ef41Sopenharmony_ci    if (idnaTest.input === "") {
231cb0ef41Sopenharmony_ci      continue // cannot test empty string input through new URL()
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci    // Percent-encode the input such that ? and equivalent code points do not end up counting as
261cb0ef41Sopenharmony_ci    // part of the URL, but are parsed through the host parser instead.
271cb0ef41Sopenharmony_ci    const encodedInput = encodeHostEndingCodePoints(idnaTest.input);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    test(() => {
301cb0ef41Sopenharmony_ci      if (idnaTest.output === null) {
311cb0ef41Sopenharmony_ci        assert_throws_js(TypeError, () => new URL(`https://${encodedInput}/x`));
321cb0ef41Sopenharmony_ci      } else {
331cb0ef41Sopenharmony_ci        const url = new URL(`https://${encodedInput}/x`);
341cb0ef41Sopenharmony_ci        assert_equals(url.host, idnaTest.output);
351cb0ef41Sopenharmony_ci        assert_equals(url.hostname, idnaTest.output);
361cb0ef41Sopenharmony_ci        assert_equals(url.pathname, "/x");
371cb0ef41Sopenharmony_ci        assert_equals(url.href, `https://${idnaTest.output}/x`);
381cb0ef41Sopenharmony_ci      }
391cb0ef41Sopenharmony_ci    }, `ToASCII("${idnaTest.input}")${idnaTest.comment ? " " + idnaTest.comment : ""}`);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci}
42