handler-tools.js (2493B)
1 // These can be used in an environment that has these global variables defined: 2 // * type (one of "path", "query", or "fragment") 3 // * noSW (a boolean) 4 5 if (type === "path" && noSW) { 6 throw new Error("There is no support for a path handler without a service worker."); 7 } 8 9 const swString = noSW ? "" : "sw"; 10 const handler = { 11 "path": "PSS%sPSE/?QES\u2020QEE#FES\u2020FEE", 12 "query": "?QES\u2020QEEPSS%sPSE#FES\u2020FEE", 13 "fragment": "?QES\u2020QEE#FES\u2020FEEPSS%sPSE" 14 }[type]; 15 const scheme = `web+wpt${type}${swString}`; 16 17 function register() { 18 const handlerURL = noSW ? `resources/handler.html${handler}${type}` : `resources/handler/${type}/${handler}`; 19 navigator.registerProtocolHandler(scheme, handlerURL, `WPT ${type} handler${noSW ? ", without service worker" : ""}`); 20 } 21 22 function runTest({ includeNull = false } = {}) { 23 promise_test(async t => { 24 const bc = new BroadcastChannel(`protocol-handler-${type}${swString}`); 25 if (!noSW) { 26 const reg = await service_worker_unregister_and_register(t, "resources/handler-sw.js", "resources/handler/"); 27 t.add_cleanup(async () => await reg.unregister()); 28 await wait_for_state(t, reg.installing, 'activated'); 29 } 30 const a = document.body.appendChild(document.createElement("a")); 31 const codePoints = []; 32 let i = includeNull ? 0 : 1; 33 for (; i < 0x82; i++) { 34 codePoints.push(String.fromCharCode(i)); 35 } 36 a.href = `${scheme}:${codePoints.join("")}`; 37 a.target = "_blank"; 38 a.click(); 39 await new Promise(resolve => { 40 bc.onmessage = t.step_func(e => { 41 resultingURL = e.data; 42 assert_equals(stringBetweenMarkers(resultingURL, "QES", "QEE"), "%86", "query baseline"); 43 assert_equals(stringBetweenMarkers(resultingURL, "FES", "FEE"), "%E2%80%A0", "fragment baseline"); 44 assert_equals(stringBetweenMarkers(resultingURL, "PSS", "PSE"), `${encodeURIComponent(scheme)}%3A${includeNull ? "%2500" : ""}%2501%2502%2503%2504%2505%2506%2507%2508%250B%250C%250E%250F%2510%2511%2512%2513%2514%2515%2516%2517%2518%2519%251A%251B%251C%251D%251E%251F%20!%22%23%24%25%26${type === "query" ? "%27" : "'"}()*%2B%2C-.%2F0123456789%3A%3B%253C%3D%253E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%2560abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%257F%25C2%2580%25C2%2581`, "actual test"); 45 resolve(); 46 }); 47 }); 48 }); 49 } 50 51 function stringBetweenMarkers(string, start, end) { 52 return string.substring(string.indexOf(start) + start.length, string.indexOf(end)); 53 }