tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

decoding-helpers.js (1112B)


      1 // Decode an URL encoded string, using XHR and data: URL. Returns a Promise.
      2 function 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/...'
     14 function 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 
     21 function 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 }