summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/dom/documents/dom-tree-accessors/Document.body.html
blob: 7d8548885a6723ae4d839413161e0616fe7e6945 (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
<!DOCTYPE html>
<title>Document.body</title>
<link rel="author" title="Ms2ger" href="mailto:ms2ger@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-document-body">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
function createDocument() {
  var doc = document.implementation.createHTMLDocument("");
  doc.removeChild(doc.documentElement);
  return doc;
}
test(function() {
  var doc = createDocument();
  assert_equals(doc.body, null);
}, "Childless document");
test(function() {
  var doc = createDocument();
  doc.appendChild(doc.createElement("html"));
  assert_equals(doc.body, null);
}, "Childless html element");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var b =
    html.appendChild(doc.createElement("body"));
  html.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, b);
}, "Body followed by frameset inside the html element");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var f =
    html.appendChild(doc.createElement("frameset"));
  html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, f);
}, "Frameset followed by body inside the html element");
test(function() {
  var doc = createDocument();
  var html =
    doc.appendChild(doc.createElementNS("http://example.org/test", "html"));
  html.appendChild(doc.createElement("body"));
  html.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, null);
}, "Body followed by frameset inside a non-HTML html element");
test(function() {
  var doc = createDocument();
  var html =
    doc.appendChild(doc.createElementNS("http://example.org/test", "html"));
  html.appendChild(doc.createElement("frameset"));
  html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, null);
}, "Frameset followed by body inside a non-HTML html element");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  html.appendChild(
    doc.createElementNS("http://example.org/test", "body"));
  var b = html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, b);
}, "Non-HTML body followed by body inside the html element");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  html.appendChild(
    doc.createElementNS("http://example.org/test", "frameset"));
  var b = html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, b);
}, "Non-HTML frameset followed by body inside the html element");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var x = html.appendChild(doc.createElement("x"));
  x.appendChild(doc.createElement("body"));
  var body = html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, body);
}, "Body inside an x element followed by a body");
test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var x = html.appendChild(doc.createElement("x"));
  x.appendChild(doc.createElement("frameset"));
  var frameset = html.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, frameset);
}, "Frameset inside an x element followed by a frameset");

// Root node is not a html element.
test(function() {
  var doc = createDocument();
  doc.appendChild(doc.createElement("body"));
  assert_equals(doc.body, null);
}, "Body as the root node");
test(function() {
  var doc = createDocument();
  doc.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, null);
}, "Frameset as the root node");
test(function() {
  var doc = createDocument();
  var body = doc.appendChild(doc.createElement("body"));
  body.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, null);
}, "Body as the root node with a frameset child");
test(function() {
  var doc = createDocument();
  var frameset = doc.appendChild(doc.createElement("frameset"));
  frameset.appendChild(doc.createElement("body"));
  assert_equals(doc.body, null);
}, "Frameset as the root node with a body child");
test(function() {
  var doc = createDocument();
  doc.appendChild(doc.createElementNS("http://example.org/test", "body"));
  assert_equals(doc.body, null);
}, "Non-HTML body as the root node");
test(function() {
  var doc = createDocument();
  doc.appendChild(doc.createElementNS("http://example.org/test", "frameset"));
  assert_equals(doc.body, null);
}, "Non-HTML frameset as the root node");

test(function() {
  assert_not_equals(document.body, null);
  assert_true(document.body instanceof HTMLBodyElement, "should be HTMLBodyElement");
  assert_equals(document.body.tagName, "BODY");
}, "existing document's body");


var originalBody = document.body;
test(function() {
  assert_throws(new TypeError(), function() {
    document.body = "text"
  })
  assert_equals(document.body, originalBody);
}, "Setting document.body to a string.")
test(function() {
  assert_throws("HierarchyRequestError", function() {
    document.body = document.createElement("div")
  })
  assert_equals(document.body, originalBody);
}, "Setting document.body to a div element.")
test(function() {
  var doc = createDocument();
  assert_throws("HierarchyRequestError", function() {
    doc.body = doc.createElement("body")
  })
  assert_equals(doc.body, null);
}, "Setting document.body when there's no root element.")
test(function() {
  var doc = document.implementation.createHTMLDocument();

  var new_body = doc.createElement("body");
  assert_true(new_body instanceof HTMLBodyElement, "should be HTMLBodyElement");
  assert_equals(new_body.tagName, "BODY");

  doc.body = new_body;
  assert_equals(doc.body, new_body);
}, "Setting document.body to a new body element.");
test(function() {
  var doc = document.implementation.createHTMLDocument();

  var new_frameset = doc.createElement("frameset");
  assert_true(new_frameset instanceof HTMLFrameSetElement, "should be HTMLFrameSetElement");
  assert_equals(new_frameset.tagName, "FRAMESET");

  doc.body = new_frameset;
  assert_equals(doc.body, new_frameset, "test6-3, append frameset to a new document");
}, "Setting document.body to a new frameset element.");

test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var f =
    html.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, f);

  var b = doc.createElement("body");
  doc.body = b;

  assert_equals(f.parentNode, null,
                "Frameset should have been removed from the tree");
  assert_equals(doc.body, b, "Body should be the new doc.body");
}, "Setting document.body to a body will replace an existing frameset if there is one.");

test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var b =
    html.appendChild(doc.createElement("body"));
  assert_equals(doc.body, b);

  var f = doc.createElement("frameset");
  doc.body = f;

  assert_equals(b.parentNode, null,
                "Body should have been removed from the tree");
  assert_equals(doc.body, f, "Frameset should be the new doc.body");
}, "Setting document.body to a frameset will replace an existing body if there is one.");

test(function() {
  var doc = createDocument();
  var html = doc.appendChild(doc.createElement("html"));
  var b =
    html.appendChild(doc.createElement("body"));
  var f1 = html.appendChild(doc.createElement("frameset"));
  assert_equals(doc.body, b);

  var f2 = doc.createElement("frameset");
  doc.body = f2;

  assert_equals(b.parentNode, null,
                "Body should have been removed from the tree");
  assert_equals(f1.parentNode, html,
                "Frameset following body should still be in the tree.");
  assert_equals(doc.body, f2, "New frameset should be the new doc.body");
  assert_equals(f2.nextSibling, f1, "New frameset should have replaced the body");
}, "Setting document.body to a frameset will replace the first existing body/frameset.");

</script>