Document.body.html (8585B)
1 <!DOCTYPE html> 2 <title>Document.body</title> 3 <link rel="author" title="Ms2ger" href="mailto:ms2ger@gmail.com"> 4 <link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-document-body"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <div id="log"></div> 8 <script> 9 function createDocument() { 10 var doc = document.implementation.createHTMLDocument(""); 11 doc.removeChild(doc.documentElement); 12 return doc; 13 } 14 test(function() { 15 var doc = createDocument(); 16 assert_equals(doc.body, null); 17 }, "Childless document"); 18 test(function() { 19 var doc = createDocument(); 20 doc.appendChild(doc.createElement("html")); 21 assert_equals(doc.body, null); 22 }, "Childless html element"); 23 test(function() { 24 var doc = createDocument(); 25 var html = doc.appendChild(doc.createElement("html")); 26 var b = 27 html.appendChild(doc.createElement("body")); 28 html.appendChild(doc.createElement("frameset")); 29 assert_equals(doc.body, b); 30 }, "Body followed by frameset inside the html element"); 31 test(function() { 32 var doc = createDocument(); 33 var html = doc.appendChild(doc.createElement("html")); 34 var f = 35 html.appendChild(doc.createElement("frameset")); 36 html.appendChild(doc.createElement("body")); 37 assert_equals(doc.body, f); 38 }, "Frameset followed by body inside the html element"); 39 test(function() { 40 var doc = createDocument(); 41 var html = 42 doc.appendChild(doc.createElementNS("http://example.org/test", "html")); 43 html.appendChild(doc.createElement("body")); 44 html.appendChild(doc.createElement("frameset")); 45 assert_equals(doc.body, null); 46 }, "Body followed by frameset inside a non-HTML html element"); 47 test(function() { 48 var doc = createDocument(); 49 var html = 50 doc.appendChild(doc.createElementNS("http://example.org/test", "html")); 51 html.appendChild(doc.createElement("frameset")); 52 html.appendChild(doc.createElement("body")); 53 assert_equals(doc.body, null); 54 }, "Frameset followed by body inside a non-HTML html element"); 55 test(function() { 56 var doc = createDocument(); 57 var html = doc.appendChild(doc.createElement("html")); 58 html.appendChild( 59 doc.createElementNS("http://example.org/test", "body")); 60 var b = html.appendChild(doc.createElement("body")); 61 assert_equals(doc.body, b); 62 }, "Non-HTML body followed by body inside the html element"); 63 test(function() { 64 var doc = createDocument(); 65 var html = doc.appendChild(doc.createElement("html")); 66 html.appendChild( 67 doc.createElementNS("http://example.org/test", "frameset")); 68 var b = html.appendChild(doc.createElement("body")); 69 assert_equals(doc.body, b); 70 }, "Non-HTML frameset followed by body inside the html element"); 71 test(function() { 72 var doc = createDocument(); 73 var html = doc.appendChild(doc.createElement("html")); 74 var x = html.appendChild(doc.createElement("x")); 75 x.appendChild(doc.createElement("body")); 76 var body = html.appendChild(doc.createElement("body")); 77 assert_equals(doc.body, body); 78 }, "Body inside an x element followed by a body"); 79 test(function() { 80 var doc = createDocument(); 81 var html = doc.appendChild(doc.createElement("html")); 82 var x = html.appendChild(doc.createElement("x")); 83 x.appendChild(doc.createElement("frameset")); 84 var frameset = html.appendChild(doc.createElement("frameset")); 85 assert_equals(doc.body, frameset); 86 }, "Frameset inside an x element followed by a frameset"); 87 88 // Root node is not a html element. 89 test(function() { 90 var doc = createDocument(); 91 doc.appendChild(doc.createElement("body")); 92 assert_equals(doc.body, null); 93 }, "Body as the root node"); 94 test(function() { 95 var doc = createDocument(); 96 doc.appendChild(doc.createElement("frameset")); 97 assert_equals(doc.body, null); 98 }, "Frameset as the root node"); 99 test(function() { 100 var doc = createDocument(); 101 var body = doc.appendChild(doc.createElement("body")); 102 body.appendChild(doc.createElement("frameset")); 103 assert_equals(doc.body, null); 104 }, "Body as the root node with a frameset child"); 105 test(function() { 106 var doc = createDocument(); 107 var frameset = doc.appendChild(doc.createElement("frameset")); 108 frameset.appendChild(doc.createElement("body")); 109 assert_equals(doc.body, null); 110 }, "Frameset as the root node with a body child"); 111 test(function() { 112 var doc = createDocument(); 113 doc.appendChild(doc.createElementNS("http://example.org/test", "body")); 114 assert_equals(doc.body, null); 115 }, "Non-HTML body as the root node"); 116 test(function() { 117 var doc = createDocument(); 118 doc.appendChild(doc.createElementNS("http://example.org/test", "frameset")); 119 assert_equals(doc.body, null); 120 }, "Non-HTML frameset as the root node"); 121 122 test(function() { 123 assert_not_equals(document.body, null); 124 assert_true(document.body instanceof HTMLBodyElement, "should be HTMLBodyElement"); 125 assert_equals(document.body.tagName, "BODY"); 126 }, "existing document's body"); 127 128 129 var originalBody = document.body; 130 test(function() { 131 assert_throws_js(TypeError, function() { 132 document.body = "text" 133 }) 134 assert_equals(document.body, originalBody); 135 }, "Setting document.body to a string.") 136 test(function() { 137 assert_throws_dom("HierarchyRequestError", function() { 138 document.body = document.createElement("div") 139 }) 140 assert_equals(document.body, originalBody); 141 }, "Setting document.body to a div element.") 142 test(function() { 143 var doc = createDocument(); 144 assert_throws_dom("HierarchyRequestError", function() { 145 doc.body = doc.createElement("body") 146 }) 147 assert_equals(doc.body, null); 148 }, "Setting document.body when there's no root element.") 149 test(function() { 150 var doc = document.implementation.createHTMLDocument(); 151 152 var new_body = doc.createElement("body"); 153 assert_true(new_body instanceof HTMLBodyElement, "should be HTMLBodyElement"); 154 assert_equals(new_body.tagName, "BODY"); 155 156 doc.body = new_body; 157 assert_equals(doc.body, new_body); 158 }, "Setting document.body to a new body element."); 159 test(function() { 160 var doc = document.implementation.createHTMLDocument(); 161 162 var new_frameset = doc.createElement("frameset"); 163 assert_true(new_frameset instanceof HTMLFrameSetElement, "should be HTMLFrameSetElement"); 164 assert_equals(new_frameset.tagName, "FRAMESET"); 165 166 doc.body = new_frameset; 167 assert_equals(doc.body, new_frameset, "test6-3, append frameset to a new document"); 168 }, "Setting document.body to a new frameset element."); 169 170 test(function() { 171 var doc = createDocument(); 172 var html = doc.appendChild(doc.createElement("html")); 173 var f = 174 html.appendChild(doc.createElement("frameset")); 175 assert_equals(doc.body, f); 176 177 var b = doc.createElement("body"); 178 doc.body = b; 179 180 assert_equals(f.parentNode, null, 181 "Frameset should have been removed from the tree"); 182 assert_equals(doc.body, b, "Body should be the new doc.body"); 183 }, "Setting document.body to a body will replace an existing frameset if there is one."); 184 185 test(function() { 186 var doc = createDocument(); 187 var html = doc.appendChild(doc.createElement("html")); 188 var b = 189 html.appendChild(doc.createElement("body")); 190 assert_equals(doc.body, b); 191 192 var f = doc.createElement("frameset"); 193 doc.body = f; 194 195 assert_equals(b.parentNode, null, 196 "Body should have been removed from the tree"); 197 assert_equals(doc.body, f, "Frameset should be the new doc.body"); 198 }, "Setting document.body to a frameset will replace an existing body if there is one."); 199 200 test(function() { 201 var doc = createDocument(); 202 var html = doc.appendChild(doc.createElement("html")); 203 var b = 204 html.appendChild(doc.createElement("body")); 205 var f1 = html.appendChild(doc.createElement("frameset")); 206 assert_equals(doc.body, b); 207 208 var f2 = doc.createElement("frameset"); 209 doc.body = f2; 210 211 assert_equals(b.parentNode, null, 212 "Body should have been removed from the tree"); 213 assert_equals(f1.parentNode, html, 214 "Frameset following body should still be in the tree."); 215 assert_equals(doc.body, f2, "New frameset should be the new doc.body"); 216 assert_equals(f2.nextSibling, f1, "New frameset should have replaced the body"); 217 }, "Setting document.body to a frameset will replace the first existing body/frameset."); 218 219 test(function() { 220 var doc = createDocument(); 221 doc.appendChild(doc.createElement("test")); 222 var new_body = doc.createElement("body"); 223 doc.body = new_body; 224 assert_equals(doc.documentElement.firstChild, new_body, "new_body should be inserted"); 225 assert_equals(doc.body, null, "Getter should return null when the root is not html"); 226 }, "Setting document.body to a new body element when the root element is a test element."); 227 </script>