1promise_test(() => fetch("resources/percent-encoding.json").then(res => res.json()).then(runTests), "Loading data…"); 2 3function runTests(testUnits) { 4 for (const testUnit of testUnits) { 5 // Ignore comments 6 if (typeof testUnit === "string") { 7 continue; 8 } 9 for (const encoding of Object.keys(testUnit.output)) { 10 async_test(t => { 11 const frame = document.body.appendChild(document.createElement("iframe")); 12 t.add_cleanup(() => frame.remove()); 13 frame.onload = t.step_func_done(() => { 14 const output = frame.contentDocument.querySelector("a"); 15 // Test that the fragment is always UTF-8 encoded 16 assert_equals(output.hash, `#${testUnit.output["utf-8"]}`, "fragment"); 17 assert_equals(output.search, `?${testUnit.output[encoding]}`, "query"); 18 }); 19 frame.src = `resources/percent-encoding.py?encoding=${encoding}&value=${toBase64(testUnit.input)}`; 20 }, `Input ${testUnit.input} with encoding ${encoding}`); 21 } 22 } 23} 24 25// Use base64 to avoid relying on the URL parser to get UTF-8 percent-encoding correctly. This does 26// not use btoa directly as that only works with code points in the range U+0000 to U+00FF, 27// inclusive. 28function toBase64(input) { 29 const bytes = new TextEncoder().encode(input); 30 const byteString = Array.from(bytes, byte => String.fromCharCode(byte)).join(""); 31 const encoded = self.btoa(byteString); 32 return encoded; 33} 34