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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
<!DOCTYPE html>
<title>HTMLTableElement.rows</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
function assert_nodelist_equals(actual, expected) {
assert_equals(actual.length, expected.length);
for (var i = 0; i < actual.length; ++i) {
assert_true(i in actual);
assert_true(actual.hasOwnProperty(i),
"property " + i + " expected to be present on the object");
assert_equals(actual.item(i), expected[i]);
assert_equals(actual[i], expected[i]);
}
}
function test_table_simple(group, table) {
var foo1 = group.appendChild(document.createElement("tr"));
foo1.id = "foo";
var bar1 = group.appendChild(document.createElement("tr"));
bar1.id = "bar";
var foo2 = group.appendChild(document.createElement("tr"));
foo2.id = "foo";
var bar2 = group.appendChild(document.createElement("tr"));
bar2.id = "bar";
assert_true(table.rows instanceof HTMLCollection, "table.rows should be a HTMLCollection.");
assert_nodelist_equals(table.rows, [foo1, bar1, foo2, bar2]);
assert_equals(table.rows.foo, foo1);
assert_equals(table.rows["foo"], foo1);
assert_equals(table.rows.namedItem("foo"), foo1);
assert_equals(table.rows.bar, bar1);
assert_equals(table.rows["bar"], bar1);
assert_equals(table.rows.namedItem("bar"), bar1);
assert_array_equals(Object.getOwnPropertyNames(table.rows), [
"0",
"1",
"2",
"3",
"foo",
"bar"
]);
}
test(function() {
var table = document.createElement("table");
test_table_simple(table, table);
}, "Children of table");
test(function() {
var table = document.createElement("table");
var group = table.appendChild(document.createElement("thead"));
test_table_simple(group, table);
}, "Children of thead");
test(function() {
var table = document.createElement("table");
var group = table.appendChild(document.createElement("tbody"));
test_table_simple(group, table);
}, "Children of tbody");
test(function() {
var table = document.createElement("table");
var group = table.appendChild(document.createElement("tfoot"));
test_table_simple(group, table);
}, "Children of tfoot");
test(function() {
var table = document.createElement("table");
var orphan1 = table.appendChild(document.createElement("tr"));
orphan1.id = "orphan1";
var foot1 = table.appendChild(document.createElement("tfoot"));
var orphan2 = table.appendChild(document.createElement("tr"));
orphan2.id = "orphan2";
var foot2 = table.appendChild(document.createElement("tfoot"));
var orphan3 = table.appendChild(document.createElement("tr"));
orphan3.id = "orphan3";
var body1 = table.appendChild(document.createElement("tbody"));
var orphan4 = table.appendChild(document.createElement("tr"));
orphan4.id = "orphan4";
var body2 = table.appendChild(document.createElement("tbody"));
var orphan5 = table.appendChild(document.createElement("tr"));
orphan5.id = "orphan5";
var head1 = table.appendChild(document.createElement("thead"));
var orphan6 = table.appendChild(document.createElement("tr"));
orphan6.id = "orphan6";
var head2 = table.appendChild(document.createElement("thead"));
var orphan7 = table.appendChild(document.createElement("tr"));
orphan7.id = "orphan7";
var foot1row1 = foot1.appendChild(document.createElement("tr"));
foot1row1.id = "foot1row1";
var foot1row2 = foot1.appendChild(document.createElement("tr"));
foot1row2.id = "foot1row2";
var foot2row1 = foot2.appendChild(document.createElement("tr"));
foot2row1.id = "foot2row1";
var foot2row2 = foot2.appendChild(document.createElement("tr"));
foot2row2.id = "foot2row2";
var body1row1 = body1.appendChild(document.createElement("tr"));
body1row1.id = "body1row1";
var body1row2 = body1.appendChild(document.createElement("tr"));
body1row2.id = "body1row2";
var body2row1 = body2.appendChild(document.createElement("tr"));
body2row1.id = "body2row1";
var body2row2 = body2.appendChild(document.createElement("tr"));
body2row2.id = "body2row2";
var head1row1 = head1.appendChild(document.createElement("tr"));
head1row1.id = "head1row1";
var head1row2 = head1.appendChild(document.createElement("tr"));
head1row2.id = "head1row2";
var head2row1 = head2.appendChild(document.createElement("tr"));
head2row1.id = "head2row1";
var head2row2 = head2.appendChild(document.createElement("tr"));
head2row2.id = "head2row2";
// These elements should not end up in any collection.
table.appendChild(document.createElement("div"))
.appendChild(document.createElement("tr"));
foot1.appendChild(document.createElement("div"))
.appendChild(document.createElement("tr"));
body1.appendChild(document.createElement("div"))
.appendChild(document.createElement("tr"));
head1.appendChild(document.createElement("div"))
.appendChild(document.createElement("tr"));
table.appendChild(document.createElementNS("http://example.com/test", "tr"));
foot1.appendChild(document.createElementNS("http://example.com/test", "tr"));
body1.appendChild(document.createElementNS("http://example.com/test", "tr"));
head1.appendChild(document.createElementNS("http://example.com/test", "tr"));
assert_true(table.rows instanceof HTMLCollection, "table.rows should be a HTMLCollection.");
assert_nodelist_equals(table.rows, [
// thead
head1row1,
head1row2,
head2row1,
head2row2,
// tbody + table
orphan1,
orphan2,
orphan3,
body1row1,
body1row2,
orphan4,
body2row1,
body2row2,
orphan5,
orphan6,
orphan7,
// tfoot
foot1row1,
foot1row2,
foot2row1,
foot2row2
]);
assert_array_equals(Object.getOwnPropertyNames(table.rows), [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"head1row1",
"head1row2",
"head2row1",
"head2row2",
"orphan1",
"orphan2",
"orphan3",
"body1row1",
"body1row2",
"orphan4",
"body2row1",
"body2row2",
"orphan5",
"orphan6",
"orphan7",
"foot1row1",
"foot1row2",
"foot2row1",
"foot2row2"
]);
var ids = [
"orphan1",
"orphan2",
"orphan3",
"orphan4",
"orphan5",
"orphan6",
"orphan7",
"foot1row1",
"foot1row2",
"foot2row1",
"foot2row2",
"body1row1",
"body1row2",
"body2row1",
"body2row2",
"head1row1",
"head1row2",
"head2row1",
"head2row2"
];
ids.forEach(function(id) {
assert_equals(table.rows.namedItem(id).id, id);
assert_true(id in table.rows);
assert_equals(table.rows[id].id, id);
assert_true(id in table.rows);
});
while (table.firstChild) {
table.removeChild(table.firstChild);
}
ids.forEach(function(id) {
assert_equals(table.rows.namedItem(id), null);
assert_false(id in table.rows);
assert_equals(table.rows[id], undefined);
assert_false(id in table.rows);
});
}, "Complicated case");
</script>
|