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