summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/url/a-element.js
blob: c77df5bf2eebc906cb6ae9269fd74d3dc18378ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var setup = async_test("Loading data…")
setup.step(function() {
  var request = new XMLHttpRequest()
  request.open("GET", "urltestdata.json")
  request.send()
  request.responseType = "json"
  request.onload = setup.step_func(function() {
    runURLTests(request.response)
    setup.done()
  })
})

function setBase(base) {
  document.getElementById("base").href = base
}

function bURL(url, base) {
  base = base || "about:blank"
  setBase(base)
  var a = document.createElement("a")
  a.setAttribute("href", url)
  return a
}

function runURLTests(urltests) {
  for(var i = 0, l = urltests.length; i < l; i++) {
    var expected = urltests[i]
    if (typeof expected === "string") continue // skip comments

    test(function() {
      var url = bURL(expected.input, expected.base)
      if(expected.failure) {
        if(url.protocol !== ':') {
          assert_unreached("Expected URL to fail parsing")
        }
        assert_equals(url.href, expected.input, "failure should set href to input")
        return
      }

      assert_equals(url.href, expected.href, "href")
      if ("origin" in expected) {
        assert_equals(url.origin, expected.origin, "origin")
      }
      assert_equals(url.protocol, expected.protocol, "protocol")
      assert_equals(url.username, expected.username, "username")
      assert_equals(url.password, expected.password, "password")
      assert_equals(url.host, expected.host, "host")
      assert_equals(url.hostname, expected.hostname, "hostname")
      assert_equals(url.port, expected.port, "port")
      assert_equals(url.pathname, expected.pathname, "pathname")
      assert_equals(url.search, expected.search, "search")
      assert_equals(url.hash, expected.hash, "hash")
    }, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
  }
}