summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Node-childNodes.html
blob: 0d38df37b2d044f4da2237fa7c1dc090fc817b27 (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
<!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>
<div style="display: none">
  <ul id='test'><li>1</li><li>2</li><li>3</li><li>4</li></ul>
</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");


test(() => {
  var node = document.getElementById("test");
  var children = node.childNodes;
  assert_true(children instanceof NodeList);
  var li = document.createElement("li");
  assert_equals(children.length, 4);

  node.appendChild(li);
  assert_equals(children.length, 5);

  node.removeChild(li);
  assert_equals(children.length, 4);
}, "Node.childNodes should be a live collection");
</script>