tor-browser

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

case.js (8071B)


      1 /*
      2 * document.createElement(NS)
      3 *
      4 * document.getElementsByTagName(NS)
      5 *
      6 * Element.setAttribute(NS)
      7 *
      8 * Element.getAttribute(NS)
      9 * Element.hasAttribute(NS)
     10 * Element.getElementsByTagName(NS)
     11 */
     12 
     13 var tests = [];
     14 setup(function() {
     15        var name_inputs = ["abc", "Abc", "ABC", "ä", "Ä"];
     16        var namespaces = ["http://www.w3.org/1999/xhtml", "http://www.w3.org/2000/svg", "http://FOO"];
     17        name_inputs.forEach(function(x) {
     18                              tests.push(["createElement " + x, test_create_element, [x]]);
     19                              tests.push(["setAttribute " +x, test_set_attribute, [x]]);
     20                              tests.push(["getAttribute " +x, test_get_attribute, [x]]);
     21                              tests.push(["getElementsByTagName a:" +x, test_get_elements_tag_name,
     22                                          [outer_product(namespaces, ["a"], name_inputs),
     23                                           x]]);
     24                              tests.push(["getElementsByTagName " +x, test_get_elements_tag_name,
     25                                          [outer_product(namespaces, [null], name_inputs),
     26                                           x]]);
     27                            });
     28        outer_product(namespaces, name_inputs, name_inputs).forEach(function(x) {
     29                                                                      tests.push(["createElementNS " + x, test_create_element_ns, x]);
     30                                                                      tests.push(["setAttributeNS " + x, test_set_attribute_ns, x]);
     31                                                                      tests.push(["getAttributeNS " + x, test_get_attribute_ns, x]);
     32                                                                    });
     33        outer_product([null].concat(namespaces), name_inputs).forEach(function(x) {
     34                                                                        tests.push(["getElementsByTagNameNS " + x, test_get_elements_tag_name_ns,
     35                                                                        outer_product(namespaces, name_inputs), x]);
     36                                                                      });
     37        name_inputs.forEach(function(x) {
     38                              tests.push(["createElementNS " + x, test_create_element_ns, [null, null, x]]);
     39                              tests.push(["setAttributeNS " + x, test_set_attribute_ns, [null, null, x]]);
     40                              tests.push(["getAttributeNS " + x, test_get_attribute_ns, [null, null, x]]);
     41                            });
     42 
     43      });
     44 function outer_product() {
     45  var rv = [];
     46  function compute_outer_product() {
     47    var args = Array.prototype.slice.call(arguments);
     48    var index = args[0];
     49    if (index < args.length) {
     50      args[index].forEach(function(x) {
     51                           compute_outer_product.apply(this, [index+1].concat(args.slice(1, index), x, args.slice(index+1)));
     52                          });
     53    } else {
     54      rv.push(args.slice(1));
     55    }
     56  }
     57  compute_outer_product.apply(this, [1].concat(Array.prototype.slice.call(arguments)));
     58  return rv;
     59 }
     60 
     61 function expected_case(input) {
     62  //is_html gets set by a global on the page loading the tests
     63  if (is_html) {
     64    return ascii_lowercase(input);
     65  } else {
     66    return input;
     67  }
     68 }
     69 
     70 function ascii_lowercase(input) {
     71  return input.replace(/[A-Z]/g, function(x) {
     72                         return x.toLowerCase();
     73                       });
     74 }
     75 
     76 function get_qualified_name(el) {
     77  if (el.prefix) {
     78    return el.prefix + ":" + el.localName;
     79  }
     80  return el.localName;
     81 }
     82 
     83 function test_create_element(name) {
     84  var node = document.createElement(name);
     85  assert_equals(node.localName, expected_case(name));
     86 }
     87 
     88 function test_create_element_ns(namespace, prefix, local_name) {
     89  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
     90  var node = document.createElementNS(namespace, qualified_name);
     91  assert_equals(node.prefix, prefix, "prefix");
     92  assert_equals(node.localName, local_name, "localName");
     93 }
     94 
     95 function test_set_attribute(name) {
     96  var node = document.createElement("div");
     97  node.setAttribute(name, "test");
     98  assert_equals(node.attributes[0].localName, expected_case(name));
     99 }
    100 
    101 function test_set_attribute_ns(namespace, prefix, local_name) {
    102  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
    103  var node = document.createElement("div");
    104  node.setAttributeNS(namespace, qualified_name, "test");
    105  var attr = node.attributes[0];
    106  assert_equals(attr.prefix, prefix, "prefix");
    107  assert_equals(attr.localName, local_name, "localName");
    108 }
    109 
    110 function test_get_attribute(name) {
    111  var node = document.createElement("div");
    112  node.setAttribute(name, "test");
    113  var expected_name = expected_case(name);
    114  assert_equals(node.getAttribute(expected_name), "test");
    115  if (expected_name != name) {
    116    assert_equals(node.getAttribute(expected_name), "test");
    117  } else if (name !== ascii_lowercase(name)) {
    118    assert_equals(node.getAttribute(ascii_lowercase(name)), null);
    119  }
    120 }
    121 
    122 function test_get_attribute_ns(namespace, prefix, local_name) {
    123  var qualified_name = prefix ? prefix + ":" + local_name : local_name;
    124  var node = document.createElement("div");
    125  node.setAttributeNS(namespace, qualified_name, "test");
    126  var expected_name = local_name;
    127  assert_equals(node.getAttributeNS(namespace, expected_name), "test");
    128  if (local_name !== ascii_lowercase(local_name)) {
    129    assert_equals(node.getAttributeNS(namespace, ascii_lowercase(local_name)), null);
    130  }
    131 }
    132 
    133 function test_get_elements_tag_name(elements_to_create, search_string) {
    134  var container = document.createElement("div");
    135  elements_to_create.forEach(function(x) {
    136                               var qualified_name = x[1] ? x[1] + ":" + x[2] : x[2];
    137                               var element = document.createElementNS(x[0], qualified_name);
    138                               container.appendChild(element);
    139                             });
    140  var expected = Array.prototype.filter.call(container.childNodes,
    141                                            function(node) {
    142                                              if (is_html && node.namespaceURI === "http://www.w3.org/1999/xhtml") {
    143                                                return get_qualified_name(node) === expected_case(search_string);
    144                                              } else {
    145                                                return get_qualified_name(node) === search_string;
    146                                              }
    147                                            });
    148  document.documentElement.appendChild(container);
    149  try {
    150    assert_array_equals(document.getElementsByTagName(search_string), expected);
    151  } finally {
    152    document.documentElement.removeChild(container);
    153  }
    154 }
    155 
    156 function test_get_elements_tag_name_ns(elements_to_create, search_input) {
    157  var search_uri = search_input[0];
    158  var search_name = search_input[1];
    159  var container = document.createElement("div");
    160  elements_to_create.forEach(function(x) {
    161                               var qualified_name = x[1] ? x[1] + ":" + x[2] : x[2];
    162                               var element = document.createElementNS(x[0], qualified_name);
    163                               container.appendChild(element);
    164                             });
    165  var expected = Array.prototype.filter.call(container.childNodes,
    166                                            function(node) {
    167                                              return node.namespaceURI === search_uri;
    168                                              return node.localName === search_name;
    169                                            });
    170  document.documentElement.appendChild(container);
    171  try {
    172    assert_array_equals(document.getElementsByTagNameNS(search_uri, search_name), expected);
    173  } catch(e) {
    174    throw e;
    175  } finally {
    176    document.documentElement.removeChild(container);
    177  }
    178 }
    179 
    180 function test_func() {
    181  var func = arguments[0];
    182  var rest = arguments[1];
    183  func.apply(this, rest);
    184 }
    185 
    186 generate_tests(test_func, tests);