tor-browser

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

urlsearchparams-foreach.any.js (2341B)


      1 test(function() {
      2    var params = new URLSearchParams('a=1&b=2&c=3');
      3    var keys = [];
      4    var values = [];
      5    params.forEach(function(value, key) {
      6        keys.push(key);
      7        values.push(value);
      8    });
      9    assert_array_equals(keys, ['a', 'b', 'c']);
     10    assert_array_equals(values, ['1', '2', '3']);
     11 }, "ForEach Check");
     12 
     13 test(function() {
     14    let a = new URL("http://a.b/c?a=1&b=2&c=3&d=4");
     15    let b = a.searchParams;
     16    var c = [];
     17    for (const i of b) {
     18        a.search = "x=1&y=2&z=3";
     19        c.push(i);
     20    }
     21    assert_array_equals(c[0], ["a","1"]);
     22    assert_array_equals(c[1], ["y","2"]);
     23    assert_array_equals(c[2], ["z","3"]);
     24 }, "For-of Check");
     25 
     26 test(function() {
     27    let a = new URL("http://a.b/c");
     28    let b = a.searchParams;
     29    for (const i of b) {
     30        assert_unreached(i);
     31    }
     32 }, "empty");
     33 
     34 test(function() {
     35    const url = new URL("http://localhost/query?param0=0&param1=1&param2=2");
     36    const searchParams = url.searchParams;
     37    const seen = [];
     38    for (const param of searchParams) {
     39        if (param[0] === 'param0') {
     40            searchParams.delete('param1');
     41        }
     42        seen.push(param);
     43    }
     44 
     45    assert_array_equals(seen[0], ["param0", "0"]);
     46    assert_array_equals(seen[1], ["param2", "2"]);
     47 }, "delete next param during iteration");
     48 
     49 test(function() {
     50    const url = new URL("http://localhost/query?param0=0&param1=1&param2=2");
     51    const searchParams = url.searchParams;
     52    const seen = [];
     53    for (const param of searchParams) {
     54        if (param[0] === 'param0') {
     55            searchParams.delete('param0');
     56            // 'param1=1' is now in the first slot, so the next iteration will see 'param2=2'.
     57        } else {
     58            seen.push(param);
     59        }
     60    }
     61 
     62    assert_array_equals(seen[0], ["param2", "2"]);
     63 }, "delete current param during iteration");
     64 
     65 test(function() {
     66    const url = new URL("http://localhost/query?param0=0&param1=1&param2=2");
     67    const searchParams = url.searchParams;
     68    const seen = [];
     69    for (const param of searchParams) {
     70        seen.push(param[0]);
     71        searchParams.delete(param[0]);
     72    }
     73 
     74    assert_array_equals(seen, ["param0", "param2"], "param1 should not have been seen by the loop");
     75    assert_equals(String(searchParams), "param1=1", "param1 should remain");
     76 }, "delete every param seen during iteration");