tor-browser

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

encode-form-common.js (4334B)


      1 // These are defined by the test:
      2 // errors (boolean)
      3 // encoder (function)
      4 // ranges (array)
      5 // separator (string)
      6 // expect (function)
      7 
      8 var tests = [];
      9 var cplist = [];
     10 var numTests = null;
     11 var numFrames = 2;
     12 var chunkSize = 400;
     13 var numChunks = null;
     14 var frames = null;
     15 var frames = null;
     16 var forms = null;
     17 var encodedSeparator = encodeURIComponent(separator);
     18 var currentChunkIndex = 0;
     19 var pageCharset = document.querySelector("meta[charset]").getAttribute("charset");
     20 
     21 setup(function() {
     22    // create a simple list of just those code points for which there is an encoding possible
     23    codepoints = [];
     24    for (var range of ranges) {
     25        for (var i = range[0]; i < range[1]; i++) {
     26            result = encoder(String.fromCodePoint(i));
     27            var success = !!result;
     28            if (errors) {
     29              success = !success;
     30            }
     31            if (success) {
     32                var item = {};
     33                codepoints.push(item);
     34                item.cp = i;
     35                item.expected = expect(result, i);
     36                item.desc = range[2];
     37            }
     38        }
     39    }
     40 
     41    // convert the information into a simple array of objects that can be easily traversed
     42    var currentChunk = [];
     43    var currentTests = [];
     44    cplist = [currentChunk];
     45    tests = [currentTests];
     46    for (i = 0; i < codepoints.length; i++) {
     47        if (currentChunk.length == chunkSize) {
     48            currentChunk = [];
     49            cplist.push(currentChunk);
     50            currentTests = [];
     51            tests.push(currentTests);
     52        }
     53        var item = {};
     54        currentChunk.push(item);
     55        item.cp = codepoints[i].cp;
     56        item.expected = codepoints[i].expected;
     57        item.desc = codepoints[i].desc;
     58        currentTests.push(subsetTest(async_test,
     59                                     (item.desc ? item.desc + " " : "") +
     60                                     "U+" +
     61                                     item.cp.toString(16).toUpperCase() +
     62                                     " " +
     63                                     String.fromCodePoint(item.cp) +
     64                                     " " +
     65                                     item.expected
     66        ));
     67    }
     68 
     69    numChunks = cplist.length;
     70 
     71    for (var i = 0; i < numFrames; i++) {
     72        var frame = document.createElement("iframe");
     73        frame.id = frame.name = "frame-" + i;
     74        document.body.appendChild(frame);
     75        var form = document.createElement("form");
     76        form.id = "form-" + i;
     77        form.method = "GET";
     78        form.action = "/common/blank.html";
     79        form.acceptCharset = pageCharset;
     80        form.target = frame.id;
     81        var input = document.createElement("input");
     82        input.id = input.name = "input-" + i;
     83        form.appendChild(input);
     84        document.body.appendChild(form);
     85    }
     86 
     87    addEventListener("load", function() {
     88        frames = Array.prototype.slice.call(
     89            document.getElementsByTagName("iframe")
     90        );
     91        forms = Array.prototype.slice.call(
     92            document.getElementsByTagName("form")
     93        );
     94        inputs = Array.prototype.slice.call(
     95            document.getElementsByTagName("input")
     96        );
     97        for (var i = 0; i < Math.min(numFrames, numChunks); i++) {
     98            runNext(i);
     99        }
    100    });
    101 });
    102 
    103 function runNext(id) {
    104    var i = currentChunkIndex;
    105    currentChunkIndex += 1;
    106 
    107    var iframe = frames[id];
    108    var form = forms[id];
    109    var input = inputs[id];
    110 
    111    input.value = cplist[i]
    112        .map(function(x) {
    113            return String.fromCodePoint(x.cp);
    114        })
    115        .join(separator);
    116    form.submit();
    117 
    118    iframe.onload = function() {
    119        var url = iframe.contentWindow.location;
    120        var query = url.search;
    121        var result_string = query.substr(query.indexOf("=") + 1);
    122        var results = result_string.split(encodedSeparator);
    123 
    124        for (var j = 0; j < cplist[i].length; j++) {
    125            var t = tests[i][j];
    126            if (t) {
    127                t.step(function() {
    128                    assert_equals(
    129                        normalizeStr(results[j]),
    130                        normalizeStr(cplist[i][j].expected)
    131                    );
    132                });
    133                t.done();
    134            }
    135        }
    136        if (currentChunkIndex < numChunks) {
    137            runNext(id);
    138        }
    139    };
    140 }