11cb0ef41Sopenharmony_ci// Decode an URL encoded string, using XHR and data: URL. Returns a Promise.
21cb0ef41Sopenharmony_cifunction decode(label, url_encoded_string) {
31cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
41cb0ef41Sopenharmony_ci    const req = new XMLHttpRequest;
51cb0ef41Sopenharmony_ci    req.open('GET', `data:text/plain,${url_encoded_string}`);
61cb0ef41Sopenharmony_ci    req.overrideMimeType(`text/plain; charset="${label}"`);
71cb0ef41Sopenharmony_ci    req.send();
81cb0ef41Sopenharmony_ci    req.onload = () => resolve(req.responseText);
91cb0ef41Sopenharmony_ci    req.onerror = () => reject(new Error(req.statusText));
101cb0ef41Sopenharmony_ci  });
111cb0ef41Sopenharmony_ci}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Convert code units in a decoded string into: "U+0001/U+0002/...'
141cb0ef41Sopenharmony_cifunction to_code_units(string) {
151cb0ef41Sopenharmony_ci  return string.split('')
161cb0ef41Sopenharmony_ci    .map(unit => unit.charCodeAt(0))
171cb0ef41Sopenharmony_ci    .map(code => 'U+' + ('0000' + code.toString(16).toUpperCase()).slice(-4))
181cb0ef41Sopenharmony_ci    .join('/');
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifunction decode_test(label,
221cb0ef41Sopenharmony_ci                     url_encoded_input,
231cb0ef41Sopenharmony_ci                     expected_code_units,
241cb0ef41Sopenharmony_ci                     description) {
251cb0ef41Sopenharmony_ci  promise_test(() => {
261cb0ef41Sopenharmony_ci    return decode(label, url_encoded_input)
271cb0ef41Sopenharmony_ci      .then(decoded => to_code_units(decoded))
281cb0ef41Sopenharmony_ci      .then(actual => {
291cb0ef41Sopenharmony_ci        assert_equals(actual, expected_code_units, `Decoding with ${label}`);
301cb0ef41Sopenharmony_ci      });
311cb0ef41Sopenharmony_ci  }, description);
321cb0ef41Sopenharmony_ci}
33