summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/collections/HTMLCollection-supported-property-indices.html
blob: 5339ec31ea1d5e644ced1dfd628a6cc9e34e207c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<!-- We want to use a tag name that will not interact with our test harness,
     so just make one up.  "foo" is a good one -->

<!-- Ids that look like negative indices.  These should come first, so we can
     assert that lookups for nonnegative indices find these by index -->
<foo id="-2"></foo>
<foo id="-1"></foo>

<!-- Ids that look like nonnegative indices -->
<foo id="0"></foo>
<foo id="1"></foo>

<!-- Ids that look like nonnegative indices near 2^31 = 2147483648 -->
<foo id="2147483645"></foo> <!-- 2^31 - 3 -->
<foo id="2147483646"></foo> <!-- 2^31 - 2 -->
<foo id="2147483647"></foo> <!-- 2^31 - 1 -->
<foo id="2147483648"></foo> <!-- 2^31 -->
<foo id="2147483649"></foo> <!-- 2^31 + 1 -->

<!-- Ids that look like nonnegative indices near 2^32 = 4294967296 -->
<foo id="4294967293"></foo> <!-- 2^32 - 3 -->
<foo id="4294967294"></foo> <!-- 2^32 - 2 -->
<foo id="4294967295"></foo> <!-- 2^32 - 1 -->
<foo id="4294967296"></foo> <!-- 2^32 -->
<foo id="4294967297"></foo> <!-- 2^32 + 1 -->

<script>
test(function() {
  var collection = document.getElementsByTagName("foo");
  assert_equals(collection.item(-2), null);
  assert_equals(collection.item(-1), null);
  assert_equals(collection.namedItem(-2), document.getElementById("-2"));
  assert_equals(collection.namedItem(-1), document.getElementById("-1"));
  assert_equals(collection[-2], document.getElementById("-2"));
  assert_equals(collection[-1], document.getElementById("-1"));
}, "Handling of property names that look like negative integers");

test(function() {
  var collection = document.getElementsByTagName("foo");
  assert_equals(collection.item(0), document.getElementById("-2"));
  assert_equals(collection.item(1), document.getElementById("-1"));
  assert_equals(collection.namedItem(0), document.getElementById("0"));
  assert_equals(collection.namedItem(1), document.getElementById("1"));
  assert_equals(collection[0], document.getElementById("-2"));
  assert_equals(collection[1], document.getElementById("-1"));
}, "Handling of property names that look like small nonnegative integers");

test(function() {
  var collection = document.getElementsByTagName("foo");
  assert_equals(collection.item(2147483645), null);
  assert_equals(collection.item(2147483646), null);
  assert_equals(collection.item(2147483647), null);
  assert_equals(collection.item(2147483648), null);
  assert_equals(collection.item(2147483649), null);
  assert_equals(collection.namedItem(2147483645),
                document.getElementById("2147483645"));
  assert_equals(collection.namedItem(2147483646),
                document.getElementById("2147483646"));
  assert_equals(collection.namedItem(2147483647),
                document.getElementById("2147483647"));
  assert_equals(collection.namedItem(2147483648),
                document.getElementById("2147483648"));
  assert_equals(collection.namedItem(2147483649),
                document.getElementById("2147483649"));
  assert_equals(collection[2147483645], undefined);
  assert_equals(collection[2147483646], undefined);
  assert_equals(collection[2147483647], undefined);
  assert_equals(collection[2147483648], undefined);
  assert_equals(collection[2147483649], undefined);
}, "Handling of property names that look like integers around 2^31");

test(function() {
  var collection = document.getElementsByTagName("foo");
  assert_equals(collection.item(4294967293), null);
  assert_equals(collection.item(4294967294), null);
  assert_equals(collection.item(4294967295), null);
  assert_equals(collection.item(4294967296), document.getElementById("-2"));
  assert_equals(collection.item(4294967297), document.getElementById("-1"));
  assert_equals(collection.namedItem(4294967293),
                document.getElementById("4294967293"));
  assert_equals(collection.namedItem(4294967294),
                document.getElementById("4294967294"));
  assert_equals(collection.namedItem(4294967295),
                document.getElementById("4294967295"));
  assert_equals(collection.namedItem(4294967296),
                document.getElementById("4294967296"));
  assert_equals(collection.namedItem(4294967297),
                document.getElementById("4294967297"));
  assert_equals(collection[4294967293], undefined);
  assert_equals(collection[4294967294], undefined);
  assert_equals(collection[4294967295], document.getElementById("4294967295"));
  assert_equals(collection[4294967296], document.getElementById("4294967296"));
  assert_equals(collection[4294967297], document.getElementById("4294967297"));
}, "Handling of property names that look like integers around 2^32");

test(function() {
  var elements = document.getElementsByTagName("foo");
  var old_item = elements[0];
  var old_desc = Object.getOwnPropertyDescriptor(elements, 0);
  assert_equals(old_desc.value, old_item);
  assert_true(old_desc.enumerable);
  assert_true(old_desc.configurable);
  assert_false(old_desc.writable);

  elements[0] = 5;
  assert_equals(elements[0], old_item);
  assert_throws_js(TypeError, function() {
    "use strict";
    elements[0] = 5;
  });
  assert_throws_js(TypeError, function() {
    Object.defineProperty(elements, 0, { value: 5 });
  });

  delete elements[0];
  assert_equals(elements[0], old_item);

  assert_throws_js(TypeError, function() {
    "use strict";
    delete elements[0];
  });
  assert_equals(elements[0], old_item);
}, 'Trying to set an expando that would shadow an already-existing indexed property');

test(function() {
  var elements = document.getElementsByTagName("foo");
  var idx = elements.length;
  var old_item = elements[idx];
  var old_desc = Object.getOwnPropertyDescriptor(elements, idx);
  assert_equals(old_item, undefined);
  assert_equals(old_desc, undefined);

  // [[DefineOwnProperty]] will disallow defining an indexed expando.
  elements[idx] = 5;
  assert_equals(elements[idx], undefined);
  assert_throws_js(TypeError, function() {
    "use strict";
    elements[idx] = 5;
  });
  assert_throws_js(TypeError, function() {
    Object.defineProperty(elements, idx, { value: 5 });
  });

  // Check that deletions out of range do not throw
  delete elements[idx];
  (function() {
    "use strict";
    delete elements[idx];
  })();
}, 'Trying to set an expando with an indexed property name past the end of the list');

test(function(){
  var elements = document.getElementsByTagName("foo");
  var old_item = elements[0];
  var old_desc = Object.getOwnPropertyDescriptor(elements, 0);
  assert_equals(old_desc.value, old_item);
  assert_true(old_desc.enumerable);
  assert_true(old_desc.configurable);
  assert_false(old_desc.writable);

  Object.prototype[0] = 5;
  this.add_cleanup(function () { delete Object.prototype[0]; });
  assert_equals(elements[0], old_item);

  delete elements[0];
  assert_equals(elements[0], old_item);

  assert_throws_js(TypeError, function() {
    "use strict";
    delete elements[0];
  });
  assert_equals(elements[0], old_item);
}, 'Trying to delete an indexed property name should never work');
</script>