• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.hasIntl) {
4  // A handful of the tests fail when ICU is not included.
5  common.skip('missing Intl');
6}
7
8const fixtures = require('../common/fixtures');
9const { test, assert_equals, assert_throws } = require('../common/wpt').harness;
10
11const request = {
12  response: require(
13    fixtures.path('wpt', 'url', 'resources', 'toascii.json')
14  )
15};
16
17// The following tests are copied from WPT. Modifications to them should be
18// upstreamed first.
19// Refs: https://github.com/w3c/web-platform-tests/blob/4839a0a804/url/toascii.window.js
20// License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
21
22/* eslint-disable */
23// async_test(t => {
24//   const request = new XMLHttpRequest()
25//   request.open("GET", "toascii.json")
26//   request.send()
27//   request.responseType = "json"
28//   request.onload = t.step_func_done(() => {
29    runTests(request.response)
30//   })
31// }, "Loading data…")
32
33function makeURL(type, input) {
34  input = "https://" + input + "/x"
35  if(type === "url") {
36    return new URL(input)
37  } else {
38    const url = document.createElement(type)
39    url.href = input
40    return url
41  }
42}
43
44function runTests(tests) {
45  for(var i = 0, l = tests.length; i < l; i++) {
46    let hostTest = tests[i]
47    if (typeof hostTest === "string") {
48      continue // skip comments
49    }
50    const typeName = { "url": "URL", "a": "<a>", "area": "<area>" }
51    // ;["url", "a", "area"].forEach((type) => {
52    ;["url"].forEach((type) => {
53      test(() => {
54        if(hostTest.output !== null) {
55          const url = makeURL("url", hostTest.input)
56          assert_equals(url.host, hostTest.output)
57          assert_equals(url.hostname, hostTest.output)
58          assert_equals(url.pathname, "/x")
59          assert_equals(url.href, "https://" + hostTest.output + "/x")
60        } else {
61          if(type === "url") {
62            assert_throws(new TypeError, () => makeURL("url", hostTest.input))
63          } else {
64            const url = makeURL(type, hostTest.input)
65            assert_equals(url.host, "")
66            assert_equals(url.hostname, "")
67            assert_equals(url.pathname, "")
68            assert_equals(url.href, "https://" + hostTest.input + "/x")
69          }
70        }
71      }, hostTest.input + " (using " + typeName[type] + ")")
72      ;["host", "hostname"].forEach((val) => {
73        test(() => {
74          const url = makeURL(type, "x")
75          url[val] = hostTest.input
76          if(hostTest.output !== null) {
77            assert_equals(url[val], hostTest.output)
78          } else {
79            assert_equals(url[val], "x")
80          }
81        }, hostTest.input + " (using " + typeName[type] + "." + val + ")")
82      })
83    })
84  }
85}
86/* eslint-enable */
87