OLD | NEW |
(Empty) | |
| 1 function test_getElementsByTagName(context, element) { |
| 2 // TODO: getElementsByTagName("*") |
| 3 test(function() { |
| 4 assert_false(context.getElementsByTagName("html") instanceof NodeList, |
| 5 "Should not return a NodeList") |
| 6 assert_true(context.getElementsByTagName("html") instanceof HTMLCollection, |
| 7 "Should return an HTMLCollection") |
| 8 }, "Interfaces") |
| 9 |
| 10 test(function() { |
| 11 var firstCollection = context.getElementsByTagName("html"), |
| 12 secondCollection = context.getElementsByTagName("html") |
| 13 assert_true(firstCollection !== secondCollection || |
| 14 firstCollection === secondCollection) |
| 15 }, "Caching is allowed") |
| 16 |
| 17 test(function() { |
| 18 var l = context.getElementsByTagName("nosuchtag") |
| 19 l[5] = "foopy" |
| 20 assert_equals(l[5], undefined) |
| 21 assert_equals(l.item(5), null) |
| 22 }, "Shouldn't be able to set unsigned properties on a HTMLCollection (non-stri
ct mode)") |
| 23 |
| 24 test(function() { |
| 25 var l = context.getElementsByTagName("nosuchtag") |
| 26 assert_throws(new TypeError(), function() { |
| 27 "use strict"; |
| 28 l[5] = "foopy" |
| 29 }) |
| 30 assert_equals(l[5], undefined) |
| 31 assert_equals(l.item(5), null) |
| 32 }, "Shouldn't be able to set unsigned properties on a HTMLCollection (strict m
ode)") |
| 33 |
| 34 test(function() { |
| 35 var l = context.getElementsByTagName("nosuchtag") |
| 36 var fn = l.item; |
| 37 assert_equals(fn, HTMLCollection.prototype.item); |
| 38 l.item = "pass" |
| 39 assert_equals(l.item, "pass") |
| 40 assert_equals(HTMLCollection.prototype.item, fn); |
| 41 }, "Should be able to set expando shadowing a proto prop (item)") |
| 42 |
| 43 test(function() { |
| 44 var l = context.getElementsByTagName("nosuchtag") |
| 45 var fn = l.namedItem; |
| 46 assert_equals(fn, HTMLCollection.prototype.namedItem); |
| 47 l.namedItem = "pass" |
| 48 assert_equals(l.namedItem, "pass") |
| 49 assert_equals(HTMLCollection.prototype.namedItem, fn); |
| 50 }, "Should be able to set expando shadowing a proto prop (namedItem)") |
| 51 |
| 52 test(function() { |
| 53 var t1 = element.appendChild(document.createElement("pre")); |
| 54 t1.id = "x"; |
| 55 var t2 = element.appendChild(document.createElement("pre")); |
| 56 t2.setAttribute("name", "y"); |
| 57 var t3 = element.appendChild(document.createElementNS("", "pre")); |
| 58 t3.setAttribute("id", "z"); |
| 59 var t4 = element.appendChild(document.createElementNS("", "pre")); |
| 60 t4.setAttribute("name", "w"); |
| 61 this.add_cleanup(function() { |
| 62 element.removeChild(t1) |
| 63 element.removeChild(t2) |
| 64 element.removeChild(t3) |
| 65 element.removeChild(t4) |
| 66 }); |
| 67 |
| 68 var list = context.getElementsByTagName('pre'); |
| 69 var pre = list[0]; |
| 70 assert_equals(pre.id, "x"); |
| 71 |
| 72 var exposedNames = { 'x': 0, 'y': 1, 'z': 2 }; |
| 73 for (var exposedName in exposedNames) { |
| 74 assert_equals(list[exposedName], list[exposedNames[exposedName]]); |
| 75 assert_equals(list[exposedName], list.namedItem(exposedName)); |
| 76 assert_true(exposedName in list, "'" + exposedName + "' in list"); |
| 77 assert_true(list.hasOwnProperty(exposedName), |
| 78 "list.hasOwnProperty('" + exposedName + "')"); |
| 79 } |
| 80 |
| 81 var unexposedNames = ["w"]; |
| 82 for (var unexposedName of unexposedNames) { |
| 83 assert_false(unexposedName in list); |
| 84 assert_false(list.hasOwnProperty(unexposedName)); |
| 85 assert_equals(list[unexposedName], undefined); |
| 86 assert_equals(list.namedItem(unexposedName), null); |
| 87 } |
| 88 |
| 89 assert_array_equals(Object.getOwnPropertyNames(list).sort(), |
| 90 ["0", "1", "2", "3", "x", "y", "z"]); |
| 91 |
| 92 var desc = Object.getOwnPropertyDescriptor(list, '0'); |
| 93 assert_equals(typeof desc, "object", "descriptor should be an object"); |
| 94 assert_true(desc.enumerable, "desc.enumerable"); |
| 95 assert_true(desc.configurable, "desc.configurable"); |
| 96 |
| 97 desc = Object.getOwnPropertyDescriptor(list, 'x'); |
| 98 assert_equals(typeof desc, "object", "descriptor should be an object"); |
| 99 assert_false(desc.enumerable, "desc.enumerable"); |
| 100 assert_true(desc.configurable, "desc.configurable"); |
| 101 }, "hasOwnProperty, getOwnPropertyDescriptor, getOwnPropertyNames") |
| 102 |
| 103 test(function() { |
| 104 assert_equals(document.createElementNS("http://www.w3.org/1999/xhtml", "i").
localName, "i") // Sanity |
| 105 var t = element.appendChild(document.createElementNS("http://www.w3.org/1999
/xhtml", "I")) |
| 106 this.add_cleanup(function() {element.removeChild(t)}) |
| 107 assert_equals(t.localName, "I") |
| 108 assert_equals(t.tagName, "I") |
| 109 assert_equals(context.getElementsByTagName("I").length, 0) |
| 110 assert_equals(context.getElementsByTagName("i").length, 0) |
| 111 }, "HTML element with uppercase tagName never matches in HTML Documents") |
| 112 |
| 113 test(function() { |
| 114 var t = element.appendChild(document.createElementNS("test", "st")) |
| 115 this.add_cleanup(function() {element.removeChild(t)}) |
| 116 assert_array_equals(context.getElementsByTagName("st"), [t]) |
| 117 assert_array_equals(context.getElementsByTagName("ST"), []) |
| 118 }, "Element in non-HTML namespace, no prefix, lowercase name") |
| 119 |
| 120 test(function() { |
| 121 var t = element.appendChild(document.createElementNS("test", "ST")) |
| 122 this.add_cleanup(function() {element.removeChild(t)}) |
| 123 assert_array_equals(context.getElementsByTagName("ST"), [t]) |
| 124 assert_array_equals(context.getElementsByTagName("st"), []) |
| 125 }, "Element in non-HTML namespace, no prefix, uppercase name") |
| 126 |
| 127 test(function() { |
| 128 var t = element.appendChild(document.createElementNS("test", "te:st")) |
| 129 this.add_cleanup(function() {element.removeChild(t)}) |
| 130 assert_array_equals(context.getElementsByTagName("st"), [t]) |
| 131 assert_array_equals(context.getElementsByTagName("ST"), []) |
| 132 }, "Element in non-HTML namespace, prefix, lowercase name") |
| 133 |
| 134 test(function() { |
| 135 var t = element.appendChild(document.createElementNS("test", "te:ST")) |
| 136 this.add_cleanup(function() {element.removeChild(t)}) |
| 137 assert_array_equals(context.getElementsByTagName("ST"), [t]) |
| 138 assert_array_equals(context.getElementsByTagName("st"), []) |
| 139 assert_array_equals(context.getElementsByTagName("te:st"), []) |
| 140 assert_array_equals(context.getElementsByTagName("te:ST"), []) |
| 141 }, "Element in non-HTML namespace, prefix, uppercase name") |
| 142 |
| 143 test(function() { |
| 144 var t = element.appendChild(document.createElement("aÇ")) |
| 145 this.add_cleanup(function() {element.removeChild(t)}) |
| 146 assert_equals(t.localName, "aÇ") |
| 147 assert_array_equals(context.getElementsByTagName("AÇ"), [t], "All uppercase
input") |
| 148 assert_array_equals(context.getElementsByTagName("aÇ"), [t], "Ascii lowercas
e input") |
| 149 assert_array_equals(context.getElementsByTagName("aç"), [], "All lowercase i
nput") |
| 150 }, "Element in HTML namespace, no prefix, non-ascii characters in name") |
| 151 |
| 152 test(function() { |
| 153 var t = element.appendChild(document.createElementNS("test", "AÇ")) |
| 154 this.add_cleanup(function() {element.removeChild(t)}) |
| 155 assert_array_equals(context.getElementsByTagName("AÇ"), [t]) |
| 156 assert_array_equals(context.getElementsByTagName("aÇ"), []) |
| 157 assert_array_equals(context.getElementsByTagName("aç"), []) |
| 158 }, "Element in non-HTML namespace, non-ascii characters in name") |
| 159 |
| 160 test(function() { |
| 161 var t = element.appendChild(document.createElementNS("http://www.w3.org/1999
/xhtml", "test:aÇ")) |
| 162 this.add_cleanup(function() {element.removeChild(t)}) |
| 163 assert_array_equals(context.getElementsByTagName("AÇ"), [t], "All uppercase
input") |
| 164 assert_array_equals(context.getElementsByTagName("aÇ"), [t], "Ascii lowercas
e input") |
| 165 assert_array_equals(context.getElementsByTagName("aç"), [], "All lowercase i
nput") |
| 166 }, "Element in HTML namespace, prefix, non-ascii characters in name") |
| 167 |
| 168 test(function() { |
| 169 var t = element.appendChild(document.createElementNS("test", "test:AÇ")) |
| 170 this.add_cleanup(function() {element.removeChild(t)}) |
| 171 assert_array_equals(context.getElementsByTagName("AÇ"), [t], "All uppercase
input") |
| 172 assert_array_equals(context.getElementsByTagName("aÇ"), [], "Ascii lowercase
input") |
| 173 assert_array_equals(context.getElementsByTagName("aç"), [], "All lowercase i
nput") |
| 174 }, "Element in non-HTML namespace, prefix, non-ascii characters in name") |
| 175 |
| 176 test(function() { |
| 177 var actual = context.getElementsByTagName("*"); |
| 178 var expected = []; |
| 179 var get_elements = function(node) { |
| 180 for (var i = 0; i < node.childNodes.length; i++) { |
| 181 var child = node.childNodes[i]; |
| 182 if (child.nodeType === child.ELEMENT_NODE) { |
| 183 expected.push(child); |
| 184 get_elements(child); |
| 185 } |
| 186 } |
| 187 } |
| 188 get_elements(context); |
| 189 assert_array_equals(actual, expected); |
| 190 }, "getElementsByTagName('*')") |
| 191 } |
OLD | NEW |