• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1promise_test(() => fetch("resources/IdnaTestV2.json").then(res => res.json()).then(runTests), "Loading data…");
2
3// Performance impact of this seems negligible (performance.now() diff in WebKit went from 48 to 52)
4// and there was a preference to let more non-ASCII hit the parser.
5function encodeHostEndingCodePoints(input) {
6  let output = "";
7  for (const codePoint of input) {
8    if ([":", "/", "?", "#", "\\"].includes(codePoint)) {
9      output += encodeURIComponent(codePoint);
10    } else {
11      output += codePoint;
12    }
13  }
14  return output;
15}
16
17function runTests(idnaTests) {
18  for (const idnaTest of idnaTests) {
19    if (typeof idnaTest === "string") {
20      continue // skip comments
21    }
22    if (idnaTest.input === "") {
23      continue // cannot test empty string input through new URL()
24    }
25    // Percent-encode the input such that ? and equivalent code points do not end up counting as
26    // part of the URL, but are parsed through the host parser instead.
27    const encodedInput = encodeHostEndingCodePoints(idnaTest.input);
28
29    test(() => {
30      if (idnaTest.output === null) {
31        assert_throws_js(TypeError, () => new URL(`https://${encodedInput}/x`));
32      } else {
33        const url = new URL(`https://${encodedInput}/x`);
34        assert_equals(url.host, idnaTest.output);
35        assert_equals(url.hostname, idnaTest.output);
36        assert_equals(url.pathname, "/x");
37        assert_equals(url.href, `https://${idnaTest.output}/x`);
38      }
39    }, `ToASCII("${idnaTest.input}")${idnaTest.comment ? " " + idnaTest.comment : ""}`);
40  }
41}
42