HTMLCollection-supported-property-indices.html (7171B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title></title> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <!-- We want to use a tag name that will not interact with our test harness, 7 so just make one up. "foo" is a good one --> 8 9 <!-- Ids that look like negative indices. These should come first, so we can 10 assert that lookups for nonnegative indices find these by index --> 11 <foo id="-2"></foo> 12 <foo id="-1"></foo> 13 14 <!-- Ids that look like nonnegative indices --> 15 <foo id="0"></foo> 16 <foo id="1"></foo> 17 18 <!-- Ids that look like nonnegative indices near 2^31 = 2147483648 --> 19 <foo id="2147483645"></foo> <!-- 2^31 - 3 --> 20 <foo id="2147483646"></foo> <!-- 2^31 - 2 --> 21 <foo id="2147483647"></foo> <!-- 2^31 - 1 --> 22 <foo id="2147483648"></foo> <!-- 2^31 --> 23 <foo id="2147483649"></foo> <!-- 2^31 + 1 --> 24 25 <!-- Ids that look like nonnegative indices near 2^32 = 4294967296 --> 26 <foo id="4294967293"></foo> <!-- 2^32 - 3 --> 27 <foo id="4294967294"></foo> <!-- 2^32 - 2 --> 28 <foo id="4294967295"></foo> <!-- 2^32 - 1 --> 29 <foo id="4294967296"></foo> <!-- 2^32 --> 30 <foo id="4294967297"></foo> <!-- 2^32 + 1 --> 31 32 <script> 33 test(function() { 34 var collection = document.getElementsByTagName("foo"); 35 assert_equals(collection.item(-2), null); 36 assert_equals(collection.item(-1), null); 37 assert_equals(collection.namedItem(-2), document.getElementById("-2")); 38 assert_equals(collection.namedItem(-1), document.getElementById("-1")); 39 assert_equals(collection[-2], document.getElementById("-2")); 40 assert_equals(collection[-1], document.getElementById("-1")); 41 }, "Handling of property names that look like negative integers"); 42 43 test(function() { 44 var collection = document.getElementsByTagName("foo"); 45 assert_equals(collection.item(0), document.getElementById("-2")); 46 assert_equals(collection.item(1), document.getElementById("-1")); 47 assert_equals(collection.namedItem(0), document.getElementById("0")); 48 assert_equals(collection.namedItem(1), document.getElementById("1")); 49 assert_equals(collection[0], document.getElementById("-2")); 50 assert_equals(collection[1], document.getElementById("-1")); 51 }, "Handling of property names that look like small nonnegative integers"); 52 53 test(function() { 54 var collection = document.getElementsByTagName("foo"); 55 assert_equals(collection.item(2147483645), null); 56 assert_equals(collection.item(2147483646), null); 57 assert_equals(collection.item(2147483647), null); 58 assert_equals(collection.item(2147483648), null); 59 assert_equals(collection.item(2147483649), null); 60 assert_equals(collection.namedItem(2147483645), 61 document.getElementById("2147483645")); 62 assert_equals(collection.namedItem(2147483646), 63 document.getElementById("2147483646")); 64 assert_equals(collection.namedItem(2147483647), 65 document.getElementById("2147483647")); 66 assert_equals(collection.namedItem(2147483648), 67 document.getElementById("2147483648")); 68 assert_equals(collection.namedItem(2147483649), 69 document.getElementById("2147483649")); 70 assert_equals(collection[2147483645], undefined); 71 assert_equals(collection[2147483646], undefined); 72 assert_equals(collection[2147483647], undefined); 73 assert_equals(collection[2147483648], undefined); 74 assert_equals(collection[2147483649], undefined); 75 }, "Handling of property names that look like integers around 2^31"); 76 77 test(function() { 78 var collection = document.getElementsByTagName("foo"); 79 assert_equals(collection.item(4294967293), null); 80 assert_equals(collection.item(4294967294), null); 81 assert_equals(collection.item(4294967295), null); 82 assert_equals(collection.item(4294967296), document.getElementById("-2")); 83 assert_equals(collection.item(4294967297), document.getElementById("-1")); 84 assert_equals(collection.namedItem(4294967293), 85 document.getElementById("4294967293")); 86 assert_equals(collection.namedItem(4294967294), 87 document.getElementById("4294967294")); 88 assert_equals(collection.namedItem(4294967295), 89 document.getElementById("4294967295")); 90 assert_equals(collection.namedItem(4294967296), 91 document.getElementById("4294967296")); 92 assert_equals(collection.namedItem(4294967297), 93 document.getElementById("4294967297")); 94 assert_equals(collection[4294967293], undefined); 95 assert_equals(collection[4294967294], undefined); 96 assert_equals(collection[4294967295], document.getElementById("4294967295")); 97 assert_equals(collection[4294967296], document.getElementById("4294967296")); 98 assert_equals(collection[4294967297], document.getElementById("4294967297")); 99 }, "Handling of property names that look like integers around 2^32"); 100 101 test(function() { 102 var elements = document.getElementsByTagName("foo"); 103 var old_item = elements[0]; 104 var old_desc = Object.getOwnPropertyDescriptor(elements, 0); 105 assert_equals(old_desc.value, old_item); 106 assert_true(old_desc.enumerable); 107 assert_true(old_desc.configurable); 108 assert_false(old_desc.writable); 109 110 elements[0] = 5; 111 assert_equals(elements[0], old_item); 112 assert_throws_js(TypeError, function() { 113 "use strict"; 114 elements[0] = 5; 115 }); 116 assert_throws_js(TypeError, function() { 117 Object.defineProperty(elements, 0, { value: 5 }); 118 }); 119 120 delete elements[0]; 121 assert_equals(elements[0], old_item); 122 123 assert_throws_js(TypeError, function() { 124 "use strict"; 125 delete elements[0]; 126 }); 127 assert_equals(elements[0], old_item); 128 }, 'Trying to set an expando that would shadow an already-existing indexed property'); 129 130 test(function() { 131 var elements = document.getElementsByTagName("foo"); 132 var idx = elements.length; 133 var old_item = elements[idx]; 134 var old_desc = Object.getOwnPropertyDescriptor(elements, idx); 135 assert_equals(old_item, undefined); 136 assert_equals(old_desc, undefined); 137 138 // [[DefineOwnProperty]] will disallow defining an indexed expando. 139 elements[idx] = 5; 140 assert_equals(elements[idx], undefined); 141 assert_throws_js(TypeError, function() { 142 "use strict"; 143 elements[idx] = 5; 144 }); 145 assert_throws_js(TypeError, function() { 146 Object.defineProperty(elements, idx, { value: 5 }); 147 }); 148 149 // Check that deletions out of range do not throw 150 delete elements[idx]; 151 (function() { 152 "use strict"; 153 delete elements[idx]; 154 })(); 155 }, 'Trying to set an expando with an indexed property name past the end of the list'); 156 157 test(function(){ 158 var elements = document.getElementsByTagName("foo"); 159 var old_item = elements[0]; 160 var old_desc = Object.getOwnPropertyDescriptor(elements, 0); 161 assert_equals(old_desc.value, old_item); 162 assert_true(old_desc.enumerable); 163 assert_true(old_desc.configurable); 164 assert_false(old_desc.writable); 165 166 Object.prototype[0] = 5; 167 this.add_cleanup(function () { delete Object.prototype[0]; }); 168 assert_equals(elements[0], old_item); 169 170 delete elements[0]; 171 assert_equals(elements[0], old_item); 172 173 assert_throws_js(TypeError, function() { 174 "use strict"; 175 delete elements[0]; 176 }); 177 assert_equals(elements[0], old_item); 178 }, 'Trying to delete an indexed property name should never work'); 179 </script>