OLD | NEW |
(Empty) | |
| 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/200
0/svg", "http://FOO"]; |
| 17 name_inputs.forEach(function(x) { |
| 18 tests.push(["createElement " + x, test_create_elem
ent, [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_e
lements_tag_name, |
| 25 [outer_product(namespaces, [null], nam
e_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.pu
sh(["getElementsByTagNameNS " + x, test_get_elements_tag_name_ns, |
| 35 outer_pr
oduct(namespaces, name_inputs), x]); |
| 36 }); |
| 37 name_inputs.forEach(function(x) { |
| 38 tests.push(["createElementNS " + x, test_create_el
ement_ns, [null, null, x]]); |
| 39 tests.push(["setAttributeNS " + x, test_set_attrib
ute_ns, [null, null, x]]); |
| 40 tests.push(["getAttributeNS " + x, test_get_attrib
ute_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(ar
gs.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(argume
nts))); |
| 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 test_create_element(name) { |
| 77 var node = document.createElement(name); |
| 78 assert_equals(node.localName, expected_case(name)); |
| 79 } |
| 80 |
| 81 function test_create_element_ns(namespace, prefix, local_name) { |
| 82 var qualified_name = prefix ? prefix + ":" + local_name : local_name; |
| 83 var node = document.createElementNS(namespace, qualified_name); |
| 84 assert_equals(node.prefix, prefix, "prefix"); |
| 85 assert_equals(node.localName, local_name, "localName"); |
| 86 } |
| 87 |
| 88 function test_set_attribute(name) { |
| 89 var node = document.createElement("div"); |
| 90 node.setAttribute(name, "test"); |
| 91 assert_equals(node.attributes[0].localName, expected_case(name)); |
| 92 } |
| 93 |
| 94 function test_set_attribute_ns(namespace, prefix, local_name) { |
| 95 var qualified_name = prefix ? prefix + ":" + local_name : local_name; |
| 96 var node = document.createElement("div"); |
| 97 node.setAttributeNS(namespace, qualified_name, "test"); |
| 98 var attr = node.attributes[0]; |
| 99 assert_equals(attr.prefix, prefix, "prefix"); |
| 100 assert_equals(attr.localName, local_name, "localName"); |
| 101 } |
| 102 |
| 103 function test_get_attribute(name) { |
| 104 var node = document.createElement("div"); |
| 105 node.setAttribute(name, "test"); |
| 106 var expected_name = expected_case(name); |
| 107 assert_equals(node.getAttribute(expected_name), "test"); |
| 108 if (expected_name != name) { |
| 109 assert_equals(node.getAttribute(expected_name), "test"); |
| 110 } else if (name !== ascii_lowercase(name)) { |
| 111 assert_equals(node.getAttribute(ascii_lowercase(name)), null); |
| 112 } |
| 113 } |
| 114 |
| 115 function test_get_attribute_ns(namespace, prefix, local_name) { |
| 116 var qualified_name = prefix ? prefix + ":" + local_name : local_name; |
| 117 var node = document.createElement("div"); |
| 118 node.setAttributeNS(namespace, qualified_name, "test"); |
| 119 var expected_name = local_name; |
| 120 assert_equals(node.getAttributeNS(namespace, expected_name), "test"); |
| 121 if (local_name !== ascii_lowercase(local_name)) { |
| 122 assert_equals(node.getAttributeNS(namespace, ascii_lowercase(local_name)), n
ull); |
| 123 } |
| 124 } |
| 125 |
| 126 function test_get_elements_tag_name(elements_to_create, search_string) { |
| 127 var container = document.createElement("div"); |
| 128 elements_to_create.forEach(function(x) { |
| 129 var qualified_name = x[1] ? x[1] + ":" + x[2] : x
[2]; |
| 130 var element = document.createElementNS(x[0], qual
ified_name); |
| 131 container.appendChild(element); |
| 132 }); |
| 133 var expected = Array.prototype.filter.call(container.childNodes, |
| 134 function(node) { |
| 135 if (is_html && node.namespaceURI =
== "http://www.w3.org/1999/xhtml") { |
| 136 return node.localName === expect
ed_case(search_string); |
| 137 } else { |
| 138 return node.localName === search
_string; |
| 139 } |
| 140 }); |
| 141 document.documentElement.appendChild(container); |
| 142 try { |
| 143 assert_array_equals(document.getElementsByTagName(search_string), expected); |
| 144 } finally { |
| 145 document.documentElement.removeChild(container); |
| 146 } |
| 147 } |
| 148 |
| 149 function test_get_elements_tag_name_ns(elements_to_create, search_input) { |
| 150 var search_uri = search_input[0]; |
| 151 var search_name = search_input[1]; |
| 152 var container = document.createElement("div"); |
| 153 elements_to_create.forEach(function(x) { |
| 154 var qualified_name = x[1] ? x[1] + ":" + x[2] : x
[2]; |
| 155 var element = document.createElementNS(x[0], qual
ified_name); |
| 156 container.appendChild(element); |
| 157 }); |
| 158 var expected = Array.prototype.filter.call(container.childNodes, |
| 159 function(node) { |
| 160 return node.namespaceURI === searc
h_uri; |
| 161 return node.localName === search_n
ame; |
| 162 }); |
| 163 document.documentElement.appendChild(container); |
| 164 try { |
| 165 assert_array_equals(document.getElementsByTagNameNS(search_uri, search_name)
, expected); |
| 166 } catch(e) { |
| 167 throw e; |
| 168 } finally { |
| 169 document.documentElement.removeChild(container); |
| 170 } |
| 171 } |
| 172 |
| 173 function test_func() { |
| 174 var func = arguments[0]; |
| 175 var rest = arguments[1]; |
| 176 func.apply(this, rest); |
| 177 } |
| 178 |
| 179 generate_tests(test_func, tests); |
OLD | NEW |