summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Node-isConnected.html
blob: da0b460de44681ca009eaaa2b615732fe733577d (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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Node.prototype.isConnected</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-isconnected">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>

"use strict";

test(function() {
  var nodes = [document.createElement("div"),
               document.createElement("div"),
               document.createElement("div")];
  checkNodes([], nodes);

  // Append nodes[0].
  document.body.appendChild(nodes[0]);
  checkNodes([nodes[0]],
             [nodes[1], nodes[2]]);

  // Append nodes[1] and nodes[2] together.
  nodes[1].appendChild(nodes[2]);
  checkNodes([nodes[0]],
             [nodes[1], nodes[2]]);

  nodes[0].appendChild(nodes[1]);
  checkNodes(nodes, []);

  // Remove nodes[2].
  nodes[2].remove();
  checkNodes([nodes[0], nodes[1]],
             [nodes[2]]);

  // Remove nodes[0] and nodes[1] together.
  nodes[0].remove();
  checkNodes([], nodes);
}, "Test with ordinary child nodes");

test(function() {
  var nodes = [document.createElement("iframe"),
               document.createElement("iframe"),
               document.createElement("iframe"),
               document.createElement("iframe"),
               document.createElement("div")];
  var frames = [nodes[0],
                nodes[1],
                nodes[2],
                nodes[3]];
  checkNodes([], nodes);

  // Since we cannot append anything to the contentWindow of an iframe before it
  // is appended to the main DOM tree, we append the iframes one after another.
  document.body.appendChild(nodes[0]);
  checkNodes([nodes[0]],
             [nodes[1], nodes[2], nodes[3], nodes[4]]);

  frames[0].contentDocument.body.appendChild(nodes[1]);
  checkNodes([nodes[0], nodes[1]],
             [nodes[2], nodes[3], nodes[4]]);

  frames[1].contentDocument.body.appendChild(nodes[2]);
  checkNodes([nodes[0], nodes[1], nodes[2]],
             [nodes[3], nodes[4]]);

  frames[2].contentDocument.body.appendChild(nodes[3]);
  checkNodes([nodes[0], nodes[1], nodes[2], nodes[3]],
             [nodes[4]]);

  frames[3].contentDocument.body.appendChild(nodes[4]);
  checkNodes(nodes, []);

  frames[3].remove();
  // Since node[4] is still under the doument of frame[3], it's still connected.
  checkNodes([nodes[0], nodes[1], nodes[2], nodes[4]],
             [nodes[3]]);

  frames[0].remove();
  // Since node[1] and node[2] are still under the doument of frame[0], they are
  // still connected.
  checkNodes([nodes[1], nodes[2], nodes[4]],
             [nodes[0], nodes[3]]);
}, "Test with iframes");

// This helper function is used to check whether nodes should be connected.
function checkNodes(aConnectedNodes, aDisconnectedNodes) {
  aConnectedNodes.forEach(node => assert_true(node.isConnected));
  aDisconnectedNodes.forEach(node => assert_false(node.isConnected));
}

</script>
</body>