summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Node-childNodes.html
blob: f7586fa74a9a22428d63ebcb081c2cef54d4cfdf (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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Node.childNodes</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-childnodes">
<link rel=author title="Tim Taubert" href="mailto:ttaubert@mozilla.com">
<link rel=author title="Ms2ger" href="mailto:Ms2ger@gmail.com">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
  var element = document.createElement("p");
  assert_equals(element.childNodes, element.childNodes);
}, "Caching of Node.childNodes");

var check_parent_node = function(node) {
  assert_array_equals(node.childNodes, []);

  var children = node.childNodes;
  var child = document.createElement("p");
  node.appendChild(child);
  assert_equals(node.childNodes, children);
  assert_array_equals(children, [child]);
  assert_equals(children.item(0), child);

  var child2 = document.createComment("comment");
  node.appendChild(child2);
  assert_array_equals(children, [child, child2]);
  assert_equals(children.item(0), child);
  assert_equals(children.item(1), child2);

  assert_false(2 in children);
  assert_equals(children[2], undefined);
  assert_equals(children.item(2), null);
};

test(function() {
  check_parent_node(document.createElement("p"));
}, "Node.childNodes on an Element.");

test(function() {
  check_parent_node(document.createDocumentFragment());
}, "Node.childNodes on a DocumentFragment.");

test(function() {
  check_parent_node(new Document());
}, "Node.childNodes on a Document.");

test(function() {
  var node = document.createElement("div");
  var kid1 = document.createElement("p");
  var kid2 = document.createTextNode("hey");
  var kid3 = document.createElement("span");
  node.appendChild(kid1);
  node.appendChild(kid2);
  node.appendChild(kid3);

  var list = node.childNodes;
  assert_array_equals([...list], [kid1, kid2, kid3]);

  var keys = list.keys();
  assert_false(keys instanceof Array);
  keys = [...keys];
  assert_array_equals(keys, [0, 1, 2]);

  var values = list.values();
  assert_false(values instanceof Array);
  values = [...values];
  assert_array_equals(values, [kid1, kid2, kid3]);

  var entries = list.entries();
  assert_false(entries instanceof Array);
  entries = [...entries];
  assert_equals(entries.length, keys.length);
  assert_equals(entries.length, values.length);
  for (var i = 0; i < entries.length; ++i) {
    assert_array_equals(entries[i], [keys[i], values[i]]);
  }

  var cur = 0;
  var thisObj = {};
  list.forEach(function(value, key, listObj) {
    assert_equals(listObj, list);
    assert_equals(this, thisObj);
    assert_equals(value, values[cur]);
    assert_equals(key, keys[cur]);
    cur++;
  }, thisObj);
  assert_equals(cur, entries.length);

  assert_equals(list[Symbol.iterator], Array.prototype[Symbol.iterator]);
  assert_equals(list.keys, Array.prototype.keys);
  if (Array.prototype.values) {
    assert_equals(list.values, Array.prototype.values);
  }
  assert_equals(list.entries, Array.prototype.entries);
  assert_equals(list.forEach, Array.prototype.forEach);
}, "Iterator behavior of Node.childNodes");
</script>