summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_nodelist.js
blob: 50c79da72e713ddbd84bb84137e3ae6052aee48c (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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
function run_test()
{

  /**
   * NOTE: [i] is not allowed in this test, since it's done via classinfo and
   * we don't have that in xpcshell.
   */
  
  test_getElementsByTagName();
  test_getElementsByTagNameNS();
  test_getElementsByAttribute();
  test_getElementsByAttributeNS();

  // What else should we test?
  // XXXbz we need more tests here to test liveness!

}  

function test_getElementsByTagName()
{
  var doc = ParseFile("nodelist_data_1.xml");
  var root = doc.documentElement;

  // Check that getElementsByTagName returns a nodelist.
  do_check_true(doc.getElementsByTagName("*") instanceof nsIDOMNodeList);
  do_check_true(root.getElementsByTagName("*") instanceof nsIDOMNodeList);
  
  // Check that getElementsByTagName excludes the element it's called on.
  do_check_eq(doc.getElementsByTagName("*").length,
              root.getElementsByTagName("*").length + 1);
  do_check_eq(doc.getElementById("test2").getElementsByTagName("*").length,
              8);
  do_check_eq(doc.getElementById("test2").getElementsByTagName("test").length,
              3);

  // Check that the first element of getElementsByTagName on the document is
  // the right thing.
  do_check_eq(doc.getElementsByTagName("*").item(0), root);

  // Check that we get the right things in the right order
  var numTests = doc.getElementsByTagName("test").length;
  do_check_eq(numTests, 5);

  for (var i = 1; i <= numTests; ++i) {
    do_check_true(doc.getElementById("test" + i) instanceof nsIDOMElement);
    do_check_eq(doc.getElementById("test" + i),
                doc.getElementsByTagName("test").item(i-1));
  }

  // Check that we handle tagnames containing ':' correctly
  do_check_true(doc.getElementsByTagName("foo:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagName("foo:test").length, 2);

  do_check_true(doc.getElementsByTagName("foo2:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagName("foo2:test").length, 3);

  do_check_true(doc.getElementsByTagName("bar:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagName("bar:test").length, 4);
}

function test_getElementsByTagNameNS()
{
  var doc = ParseFile("nodelist_data_1.xml");
  var root = doc.documentElement;

  // Check that getElementsByTagNameNS returns a nodelist.
  do_check_true(doc.getElementsByTagNameNS("*", "*") instanceof nsIDOMNodeList);
  do_check_true(root.getElementsByTagNameNS("*", "*") instanceof nsIDOMNodeList);

  // Check that passing "" and null for the namespace URI gives the same result
  var list1 = doc.getElementsByTagNameNS("", "test");
  var list2 = doc.getElementsByTagNameNS(null, "test");
  do_check_eq(list1.length, list2.length);
  for (var i = 0; i < list1.length; ++i) {
    do_check_eq(list1.item(i), list2.item(i));
  }
  
  // Check that getElementsByTagNameNS excludes the element it's called on.
  do_check_eq(doc.getElementsByTagNameNS("*", "*").length,
              root.getElementsByTagNameNS("*", "*").length + 1);
  do_check_eq(doc.getElementById("test2")
                 .getElementsByTagNameNS("*", "*").length,
              8);
  do_check_eq(doc.getElementById("test2")
                 .getElementsByTagNameNS("", "test").length,
              1);
  do_check_eq(doc.getElementById("test2")
                 .getElementsByTagNameNS("*", "test").length,
              7);

  // Check that the first element of getElementsByTagNameNS on the document is
  // the right thing.
  do_check_eq(doc.getElementsByTagNameNS("*", "*").item(0), root);
  do_check_eq(doc.getElementsByTagNameNS(null, "*").item(0), root);

  // Check that we get the right things in the right order

     
  var numTests = doc.getElementsByTagNameNS("*", "test").length;
  do_check_eq(numTests, 14);

  for (var i = 1; i <= numTests; ++i) {
    do_check_true(doc.getElementById("test" + i) instanceof nsIDOMElement);
    do_check_eq(doc.getElementById("test" + i),
                doc.getElementsByTagNameNS("*", "test").item(i-1));
  }

  // Check general proper functioning of having a non-wildcard namespace.
  var test2 = doc.getElementById("test2");
  do_check_eq(doc.getElementsByTagNameNS("", "test").length,
              3);
  do_check_eq(test2.getElementsByTagNameNS("", "test").length,
              1);
  do_check_eq(doc.getElementsByTagNameNS("foo", "test").length,
              7);
  do_check_eq(test2.getElementsByTagNameNS("foo", "test").length,
              4);
  do_check_eq(doc.getElementsByTagNameNS("foo2", "test").length,
              0);
  do_check_eq(test2.getElementsByTagNameNS("foo2", "test").length,
              0);
  do_check_eq(doc.getElementsByTagNameNS("bar", "test").length,
              4);
  do_check_eq(test2.getElementsByTagNameNS("bar", "test").length,
              2);

  // Check that we handle tagnames containing ':' correctly
  do_check_true(doc.getElementsByTagNameNS(null, "foo:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagNameNS(null, "foo:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("foo", "foo:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("bar", "foo:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("*", "foo:test").length, 0);
  
  do_check_true(doc.getElementsByTagNameNS(null, "foo2:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagNameNS(null, "foo2:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("foo2", "foo2:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("bar", "foo2:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("*", "foo2:test").length, 0);
  
  do_check_true(doc.getElementsByTagNameNS(null, "bar:test")
                instanceof nsIDOMNodeList);
  do_check_eq(doc.getElementsByTagNameNS(null, "bar:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("bar", "bar:test").length, 0);
  do_check_eq(doc.getElementsByTagNameNS("*", "bar:test").length, 0);

  // Check that previously-unknown namespaces are handled right.  Note that we
  // can just hardcode the strings, since we're running only once in XPCshell.
  // If someone wants to run these in a browser, some use of Math.random() may
  // be in order.
  list1 = doc.getElementsByTagNameNS("random-bogus-namespace", "foo");
  list2 = doc.documentElement.getElementsByTagNameNS("random-bogus-namespace2",
                                                     "foo");
  do_check_neq(list1, list2);
  do_check_eq(list1.length, 0);
  do_check_eq(list2.length, 0);
  var newNode = doc.createElementNS("random-bogus-namespace", "foo");
  doc.documentElement.appendChild(newNode);
  var newNode = doc.createElementNS("random-bogus-namespace2", "foo");
  doc.documentElement.appendChild(newNode);
  do_check_eq(list1.length, 1);  
  do_check_eq(list2.length, 1);  
}

function test_getElementsByAttribute()
{
  var doc = ParseFile("nodelist_data_2.xul");
  var root = doc.documentElement;

  // Sadly, DOMParser can't create XULDocument objects.  But at least we have a
  // XULElement!

  do_check_true(root instanceof nsIDOMXULElement);

  do_check_true(root.getElementsByAttribute("foo", "foo") instanceof
                nsIDOMNodeList);

  var master1 = doc.getElementById("master1");
  var master2 = doc.getElementById("master2");
  var master3 = doc.getElementById("master3");
  var external = doc.getElementById("external");

  do_check_true(master1 instanceof nsIDOMXULElement);
  do_check_true(master2 instanceof nsIDOMXULElement);
  do_check_true(master3 instanceof nsIDOMXULElement);
  do_check_true(external instanceof nsIDOMXULElement);

  // Basic tests
  do_check_eq(root.getElementsByAttribute("foo", "foo").length,
              14);
  do_check_eq(master1.getElementsByAttribute("foo", "foo").length,
              4);

  do_check_eq(root.getElementsByAttribute("foo", "bar").length,
              7);
  do_check_eq(master1.getElementsByAttribute("foo", "bar").length,
              2);

  do_check_eq(root.getElementsByAttribute("bar", "bar").length,
              7);
  do_check_eq(master1.getElementsByAttribute("bar", "bar").length,
              2);

  do_check_eq(root.getElementsByAttribute("foo", "*").length,
              21);
  do_check_eq(master1.getElementsByAttribute("foo", "*").length,
              6);

  // Test the various combinations of attributes with colons in the name
  do_check_eq(root.getElementsByAttribute("foo:foo", "foo").length,
              16);
  do_check_eq(master1.getElementsByAttribute("foo:foo", "foo").length,
              5);
  do_check_eq(master2.getElementsByAttribute("foo:foo", "foo").length,
              4);
  do_check_eq(master3.getElementsByAttribute("foo:foo", "foo").length,
              4);
  do_check_eq(external.getElementsByAttribute("foo:foo", "foo").length,
              2);

  do_check_eq(root.getElementsByAttribute("foo:foo", "bar").length,
              9);
  do_check_eq(master1.getElementsByAttribute("foo:foo", "bar").length,
              2);
  do_check_eq(master2.getElementsByAttribute("foo:foo", "bar").length,
              3);
  do_check_eq(master3.getElementsByAttribute("foo:foo", "bar").length,
              2);
  do_check_eq(external.getElementsByAttribute("foo:foo", "bar").length,
              1);

  do_check_eq(root.getElementsByAttribute("foo:bar", "foo").length,
              7);
  do_check_eq(master1.getElementsByAttribute("foo:bar", "foo").length,
              2);
  do_check_eq(master2.getElementsByAttribute("foo:bar", "foo").length,
              2);
  do_check_eq(master3.getElementsByAttribute("foo:bar", "foo").length,
              2);
  do_check_eq(external.getElementsByAttribute("foo:bar", "foo").length,
              1);

  do_check_eq(root.getElementsByAttribute("foo:bar", "bar").length,
              14);
  do_check_eq(master1.getElementsByAttribute("foo:bar", "bar").length,
              4);
  do_check_eq(master2.getElementsByAttribute("foo:bar", "bar").length,
              4);
  do_check_eq(master3.getElementsByAttribute("foo:bar", "bar").length,
              4);
  do_check_eq(external.getElementsByAttribute("foo:bar", "bar").length,
              2);

  do_check_eq(root.getElementsByAttribute("foo2:foo", "foo").length,
              8);
  do_check_eq(master1.getElementsByAttribute("foo2:foo", "foo").length,
              2);
  do_check_eq(master2.getElementsByAttribute("foo2:foo", "foo").length,
              2);
  do_check_eq(master3.getElementsByAttribute("foo2:foo", "foo").length,
              3);
  do_check_eq(external.getElementsByAttribute("foo2:foo", "foo").length,
              1);

  do_check_eq(root.getElementsByAttribute("foo:foo", "*").length,
              25);
  do_check_eq(master1.getElementsByAttribute("foo:foo", "*").length,
              7);
  do_check_eq(master2.getElementsByAttribute("foo:foo", "*").length,
              7);
  do_check_eq(master3.getElementsByAttribute("foo:foo", "*").length,
              6);
  do_check_eq(external.getElementsByAttribute("foo:foo", "*").length,
              3);

  do_check_eq(root.getElementsByAttribute("foo2:foo", "bar").length,
              0);
  do_check_eq(root.getElementsByAttribute("foo:foo", "baz").length,
              0);
}

function test_getElementsByAttributeNS()
{
  var doc = ParseFile("nodelist_data_2.xul");
  var root = doc.documentElement;

  // Sadly, DOMParser can't create XULDocument objects.  But at least we have a
  // XULElement!

  do_check_true(root instanceof nsIDOMXULElement);

  // Check that getElementsByAttributeNS returns a nodelist.
  do_check_true(root.getElementsByAttributeNS("*", "*", "*") instanceof
                nsIDOMNodeList);

  var master1 = doc.getElementById("master1");
  var master2 = doc.getElementById("master2");
  var master3 = doc.getElementById("master3");
  var external = doc.getElementById("external");

  do_check_true(master1 instanceof nsIDOMXULElement);
  do_check_true(master2 instanceof nsIDOMXULElement);
  do_check_true(master3 instanceof nsIDOMXULElement);
  do_check_true(external instanceof nsIDOMXULElement);
  
  // Test wildcard namespace
  do_check_eq(root.getElementsByAttributeNS("*", "foo", "foo").length,
              38);
  do_check_eq(master1.getElementsByAttributeNS("*", "foo", "foo").length,
              11);
  do_check_eq(master2.getElementsByAttributeNS("*", "foo", "foo").length,
              10);
  do_check_eq(master3.getElementsByAttributeNS("*", "foo", "foo").length,
              11);

  do_check_eq(root.getElementsByAttributeNS("*", "foo", "bar").length,
              16);
  do_check_eq(master1.getElementsByAttributeNS("*", "foo", "bar").length,
              4);
  do_check_eq(master2.getElementsByAttributeNS("*", "foo", "bar").length,
              5);
  do_check_eq(master3.getElementsByAttributeNS("*", "foo", "bar").length,
              4);

  do_check_eq(root.getElementsByAttributeNS("*", "bar", "bar").length,
              21);
  do_check_eq(master1.getElementsByAttributeNS("*", "bar", "bar").length,
              6);
  do_check_eq(master2.getElementsByAttributeNS("*", "bar", "bar").length,
              6);
  do_check_eq(master3.getElementsByAttributeNS("*", "bar", "bar").length,
              6);

  do_check_eq(root.getElementsByAttributeNS("*", "foo", "*").length,
              54);
  do_check_eq(master1.getElementsByAttributeNS("*", "foo", "*").length,
              15);
  do_check_eq(master2.getElementsByAttributeNS("*", "foo", "*").length,
              15);
  do_check_eq(master3.getElementsByAttributeNS("*", "foo", "*").length,
              15);

  // Test null namespace. This should be the same as getElementsByAttribute.
  do_check_eq(root.getElementsByAttributeNS("", "foo", "foo").length,
              root.getElementsByAttribute("foo", "foo").length);
  do_check_eq(master1.getElementsByAttributeNS("", "foo", "foo").length,
              master1.getElementsByAttribute("foo", "foo").length);
  do_check_eq(master2.getElementsByAttributeNS("", "foo", "foo").length,
              master2.getElementsByAttribute("foo", "foo").length);
  do_check_eq(master3.getElementsByAttributeNS("", "foo", "foo").length,
              master3.getElementsByAttribute("foo", "foo").length);
  
  // Test namespace "foo"
  do_check_eq(root.getElementsByAttributeNS("foo", "foo", "foo").length,
              24);
  do_check_eq(master1.getElementsByAttributeNS("foo", "foo", "foo").length,
              7);
  do_check_eq(master2.getElementsByAttributeNS("foo", "foo", "foo").length,
              6);
  do_check_eq(master3.getElementsByAttributeNS("foo", "foo", "foo").length,
              7);

  do_check_eq(root.getElementsByAttributeNS("foo", "foo", "bar").length,
              9);
  do_check_eq(master1.getElementsByAttributeNS("foo", "foo", "bar").length,
              2);
  do_check_eq(master2.getElementsByAttributeNS("foo", "foo", "bar").length,
              3);
  do_check_eq(master3.getElementsByAttributeNS("foo", "foo", "bar").length,
              2);

  do_check_eq(root.getElementsByAttributeNS("foo", "bar", "foo").length,
              7);
  do_check_eq(master1.getElementsByAttributeNS("foo", "bar", "foo").length,
              2);
  do_check_eq(master2.getElementsByAttributeNS("foo", "bar", "foo").length,
              2);
  do_check_eq(master3.getElementsByAttributeNS("foo", "bar", "foo").length,
              2);

  do_check_eq(root.getElementsByAttributeNS("foo", "bar", "bar").length,
              14);
  do_check_eq(master1.getElementsByAttributeNS("foo", "bar", "bar").length,
              4);
  do_check_eq(master2.getElementsByAttributeNS("foo", "bar", "bar").length,
              4);
  do_check_eq(master3.getElementsByAttributeNS("foo", "bar", "bar").length,
              4);
}