1// Decode an URL encoded string, using XHR and data: URL. Returns a Promise. 2function decode(label, url_encoded_string) { 3 return new Promise((resolve, reject) => { 4 const req = new XMLHttpRequest; 5 req.open('GET', `data:text/plain,${url_encoded_string}`); 6 req.overrideMimeType(`text/plain; charset="${label}"`); 7 req.send(); 8 req.onload = () => resolve(req.responseText); 9 req.onerror = () => reject(new Error(req.statusText)); 10 }); 11} 12 13// Convert code units in a decoded string into: "U+0001/U+0002/...' 14function to_code_units(string) { 15 return string.split('') 16 .map(unit => unit.charCodeAt(0)) 17 .map(code => 'U+' + ('0000' + code.toString(16).toUpperCase()).slice(-4)) 18 .join('/'); 19} 20 21function decode_test(label, 22 url_encoded_input, 23 expected_code_units, 24 description) { 25 promise_test(() => { 26 return decode(label, url_encoded_input) 27 .then(decoded => to_code_units(decoded)) 28 .then(actual => { 29 assert_equals(actual, expected_code_units, `Decoding with ${label}`); 30 }); 31 }, description); 32} 33