1promise_test(() => fetch("resources/urltestdata.json").then(res => res.json()).then(runURLTests), "Loading data…"); 2 3function setBase(base) { 4 document.getElementById("base").href = base; 5} 6 7function bURL(url, base) { 8 setBase(base); 9 const a = document.createElement("a"); 10 a.setAttribute("href", url); 11 return a; 12} 13 14function runURLTests(urlTests) { 15 for (const expected of urlTests) { 16 // Skip comments 17 if (typeof expected === "string") 18 continue; 19 20 // Fragments are relative against "about:blank" 21 if (expected.relativeTo === "any-base") 22 continue; 23 24 // We cannot use a null base for HTML tests 25 const base = expected.base === null ? "about:blank" : expected.base; 26 27 function getKey(expected) { 28 if (expected.protocol) { 29 return expected.protocol.replace(":", ""); 30 } 31 if (expected.failure) { 32 return expected.input.split(":")[0]; 33 } 34 return "other"; 35 } 36 37 subsetTestByKey(getKey(expected), test, function() { 38 var url = bURL(expected.input, base) 39 if(expected.failure) { 40 if(url.protocol !== ':') { 41 assert_unreached("Expected URL to fail parsing") 42 } 43 assert_equals(url.href, expected.input, "failure should set href to input") 44 return 45 } 46 47 assert_equals(url.href, expected.href, "href") 48 assert_equals(url.protocol, expected.protocol, "protocol") 49 assert_equals(url.username, expected.username, "username") 50 assert_equals(url.password, expected.password, "password") 51 assert_equals(url.host, expected.host, "host") 52 assert_equals(url.hostname, expected.hostname, "hostname") 53 assert_equals(url.port, expected.port, "port") 54 assert_equals(url.pathname, expected.pathname, "pathname") 55 assert_equals(url.search, expected.search, "search") 56 assert_equals(url.hash, expected.hash, "hash") 57 }, "Parsing: <" + expected.input + "> against <" + base + ">") 58 } 59} 60