11cb0ef41Sopenharmony_cipromise_test(() => fetch("resources/urltestdata.json").then(res => res.json()).then(runURLTests), "Loading data…");
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cifunction setBase(base) {
41cb0ef41Sopenharmony_ci  document.getElementById("base").href = base;
51cb0ef41Sopenharmony_ci}
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cifunction bURL(url, base) {
81cb0ef41Sopenharmony_ci  setBase(base);
91cb0ef41Sopenharmony_ci  const a = document.createElement("a");
101cb0ef41Sopenharmony_ci  a.setAttribute("href", url);
111cb0ef41Sopenharmony_ci  return a;
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction runURLTests(urlTests) {
151cb0ef41Sopenharmony_ci  for (const expected of urlTests) {
161cb0ef41Sopenharmony_ci    // Skip comments
171cb0ef41Sopenharmony_ci    if (typeof expected === "string")
181cb0ef41Sopenharmony_ci      continue;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    // Fragments are relative against "about:blank"
211cb0ef41Sopenharmony_ci    if (expected.relativeTo === "any-base")
221cb0ef41Sopenharmony_ci      continue;
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    // We cannot use a null base for HTML tests
251cb0ef41Sopenharmony_ci    const base = expected.base === null ? "about:blank" : expected.base;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    function getKey(expected) {
281cb0ef41Sopenharmony_ci      if (expected.protocol) {
291cb0ef41Sopenharmony_ci        return expected.protocol.replace(":", "");
301cb0ef41Sopenharmony_ci      }
311cb0ef41Sopenharmony_ci      if (expected.failure) {
321cb0ef41Sopenharmony_ci        return expected.input.split(":")[0];
331cb0ef41Sopenharmony_ci      }
341cb0ef41Sopenharmony_ci      return "other";
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    subsetTestByKey(getKey(expected), test, function() {
381cb0ef41Sopenharmony_ci      var url = bURL(expected.input, base)
391cb0ef41Sopenharmony_ci      if(expected.failure) {
401cb0ef41Sopenharmony_ci        if(url.protocol !== ':') {
411cb0ef41Sopenharmony_ci          assert_unreached("Expected URL to fail parsing")
421cb0ef41Sopenharmony_ci        }
431cb0ef41Sopenharmony_ci        assert_equals(url.href, expected.input, "failure should set href to input")
441cb0ef41Sopenharmony_ci        return
451cb0ef41Sopenharmony_ci      }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci      assert_equals(url.href, expected.href, "href")
481cb0ef41Sopenharmony_ci      assert_equals(url.protocol, expected.protocol, "protocol")
491cb0ef41Sopenharmony_ci      assert_equals(url.username, expected.username, "username")
501cb0ef41Sopenharmony_ci      assert_equals(url.password, expected.password, "password")
511cb0ef41Sopenharmony_ci      assert_equals(url.host, expected.host, "host")
521cb0ef41Sopenharmony_ci      assert_equals(url.hostname, expected.hostname, "hostname")
531cb0ef41Sopenharmony_ci      assert_equals(url.port, expected.port, "port")
541cb0ef41Sopenharmony_ci      assert_equals(url.pathname, expected.pathname, "pathname")
551cb0ef41Sopenharmony_ci      assert_equals(url.search, expected.search, "search")
561cb0ef41Sopenharmony_ci      assert_equals(url.hash, expected.hash, "hash")
571cb0ef41Sopenharmony_ci    }, "Parsing: <" + expected.input + "> against <" + base + ">")
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci}
60