OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Node.insertBefore</title> |
| 3 <script src="../../../../resources/testharness.js"></script> |
| 4 <script src="../../../../resources/testharnessreport.js"></script> |
| 5 <div id="log"></div> |
| 6 <script> |
| 7 function testLeafNode(nodeName, createNodeFunction) { |
| 8 test(function() { |
| 9 var node = createNodeFunction(); |
| 10 assert_throws(new TypeError(), function() { node.insertBefore(null, null) }) |
| 11 }, "Calling insertBefore with a non-Node first argument on a leaf node " + nod
eName + " must throw TypeError.") |
| 12 test(function() { |
| 13 var node = createNodeFunction(); |
| 14 assert_throws("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(docume
nt.createTextNode("fail"), null) }) |
| 15 // Would be step 2. |
| 16 assert_throws("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node,
null) }) |
| 17 // Would be step 3. |
| 18 assert_throws("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node,
document.createTextNode("child")) }) |
| 19 }, "Calling insertBefore an a leaf node " + nodeName + " must throw HIERARCHY_
REQUEST_ERR.") |
| 20 } |
| 21 |
| 22 test(function() { |
| 23 // WebIDL. |
| 24 assert_throws(new TypeError(), function() { document.body.insertBefore(null, n
ull) }) |
| 25 assert_throws(new TypeError(), function() { document.body.insertBefore(null, d
ocument.body.firstChild) }) |
| 26 assert_throws(new TypeError(), function() { document.body.insertBefore({'a':'b
'}, document.body.firstChild) }) |
| 27 }, "Calling insertBefore with a non-Node first argument must throw TypeError.") |
| 28 |
| 29 testLeafNode("DocumentType", function () { return document.doctype; } ) |
| 30 testLeafNode("Text", function () { return document.createTextNode("Foo") }) |
| 31 testLeafNode("Comment", function () { return document.createComment("Foo") }) |
| 32 testLeafNode("ProcessingInstruction", function () { return document.createProces
singInstruction("foo", "bar") }) |
| 33 |
| 34 test(function() { |
| 35 // Step 2. |
| 36 assert_throws("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore
(document.body, document.getElementById("log")) }) |
| 37 assert_throws("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore
(document.documentElement, document.getElementById("log")) }) |
| 38 }, "Calling insertBefore with an inclusive ancestor of the context object must t
hrow HIERARCHY_REQUEST_ERR.") |
| 39 |
| 40 // Step 3. |
| 41 test(function() { |
| 42 var a = document.createElement("div"); |
| 43 var b = document.createElement("div"); |
| 44 var c = document.createElement("div"); |
| 45 assert_throws("NotFoundError", function() { |
| 46 a.insertBefore(b, c); |
| 47 }); |
| 48 }, "Calling insertBefore with a reference child whose parent is not the context
node must throw a NotFoundError.") |
| 49 |
| 50 // Step 4.1. |
| 51 test(function() { |
| 52 var doc = document.implementation.createHTMLDocument("title"); |
| 53 var doc2 = document.implementation.createHTMLDocument("title2"); |
| 54 assert_throws("HierarchyRequestError", function() { |
| 55 doc.insertBefore(doc2, doc.documentElement); |
| 56 }); |
| 57 |
| 58 assert_throws("HierarchyRequestError", function() { |
| 59 doc.insertBefore(doc.createTextNode("text"), doc.documentElement); |
| 60 }); |
| 61 }, "If the context node is a document, inserting a document or text node should
throw a HierarchyRequestError.") |
| 62 |
| 63 // Step 4.2.1. |
| 64 test(function() { |
| 65 var doc = document.implementation.createHTMLDocument("title"); |
| 66 doc.removeChild(doc.documentElement); |
| 67 |
| 68 var df = doc.createDocumentFragment(); |
| 69 df.appendChild(doc.createElement("a")); |
| 70 df.appendChild(doc.createElement("b")); |
| 71 assert_throws("HierarchyRequestError", function() { |
| 72 doc.insertBefore(df, null); |
| 73 }); |
| 74 |
| 75 df = doc.createDocumentFragment(); |
| 76 df.appendChild(doc.createTextNode("text")); |
| 77 assert_throws("HierarchyRequestError", function() { |
| 78 doc.insertBefore(df, null); |
| 79 }); |
| 80 |
| 81 df = doc.createDocumentFragment(); |
| 82 df.appendChild(doc.createComment("comment")); |
| 83 df.appendChild(doc.createTextNode("text")); |
| 84 assert_throws("HierarchyRequestError", function() { |
| 85 doc.insertBefore(df, null); |
| 86 }); |
| 87 }, "If the context node is a document, appending a DocumentFragment that contain
s a text node or too many elements should throw a HierarchyRequestError.") |
| 88 test(function() { |
| 89 var doc = document.implementation.createHTMLDocument("title"); |
| 90 doc.removeChild(doc.documentElement); |
| 91 |
| 92 var df = doc.createDocumentFragment(); |
| 93 df.appendChild(doc.createElement("a")); |
| 94 df.appendChild(doc.createElement("b")); |
| 95 assert_throws("HierarchyRequestError", function() { |
| 96 doc.insertBefore(df, doc.firstChild); |
| 97 }); |
| 98 |
| 99 df = doc.createDocumentFragment(); |
| 100 df.appendChild(doc.createTextNode("text")); |
| 101 assert_throws("HierarchyRequestError", function() { |
| 102 doc.insertBefore(df, doc.firstChild); |
| 103 }); |
| 104 |
| 105 df = doc.createDocumentFragment(); |
| 106 df.appendChild(doc.createComment("comment")); |
| 107 df.appendChild(doc.createTextNode("text")); |
| 108 assert_throws("HierarchyRequestError", function() { |
| 109 doc.insertBefore(df, doc.firstChild); |
| 110 }); |
| 111 }, "If the context node is a document, inserting a DocumentFragment that contain
s a text node or too many elements should throw a HierarchyRequestError.") |
| 112 |
| 113 // Step 4.2.2. |
| 114 test(function() { |
| 115 // The context node has an element child. |
| 116 var doc = document.implementation.createHTMLDocument("title"); |
| 117 var comment = doc.appendChild(doc.createComment("foo")); |
| 118 assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment
]); |
| 119 |
| 120 var df = doc.createDocumentFragment(); |
| 121 df.appendChild(doc.createElement("a")); |
| 122 assert_throws("HierarchyRequestError", function() { |
| 123 doc.insertBefore(df, doc.doctype); |
| 124 }); |
| 125 assert_throws("HierarchyRequestError", function() { |
| 126 doc.insertBefore(df, doc.documentElement); |
| 127 }); |
| 128 assert_throws("HierarchyRequestError", function() { |
| 129 doc.insertBefore(df, comment); |
| 130 }); |
| 131 assert_throws("HierarchyRequestError", function() { |
| 132 doc.insertBefore(df, null); |
| 133 }); |
| 134 }, "If the context node is a document, inserting a DocumentFragment with an elem
ent if there already is an element child should throw a HierarchyRequestError.") |
| 135 test(function() { |
| 136 // /child/ is a doctype. |
| 137 var doc = document.implementation.createHTMLDocument("title"); |
| 138 var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild); |
| 139 doc.removeChild(doc.documentElement); |
| 140 assert_array_equals(doc.childNodes, [comment, doc.doctype]); |
| 141 |
| 142 var df = doc.createDocumentFragment(); |
| 143 df.appendChild(doc.createElement("a")); |
| 144 assert_throws("HierarchyRequestError", function() { |
| 145 doc.insertBefore(df, doc.doctype); |
| 146 }); |
| 147 }, "If the context node is a document and a doctype is following the reference c
hild, inserting a DocumentFragment with an element should throw a HierarchyReque
stError.") |
| 148 test(function() { |
| 149 // /child/ is not null and a doctype is following /child/. |
| 150 var doc = document.implementation.createHTMLDocument("title"); |
| 151 var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild); |
| 152 doc.removeChild(doc.documentElement); |
| 153 assert_array_equals(doc.childNodes, [comment, doc.doctype]); |
| 154 |
| 155 var df = doc.createDocumentFragment(); |
| 156 df.appendChild(doc.createElement("a")); |
| 157 assert_throws("HierarchyRequestError", function() { |
| 158 doc.insertBefore(df, comment); |
| 159 }); |
| 160 }, "If the context node is a document, inserting a DocumentFragment with an elem
ent before the doctype should throw a HierarchyRequestError.") |
| 161 |
| 162 // Step 4.3. |
| 163 test(function() { |
| 164 // The context node has an element child. |
| 165 var doc = document.implementation.createHTMLDocument("title"); |
| 166 var comment = doc.appendChild(doc.createComment("foo")); |
| 167 assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment
]); |
| 168 |
| 169 var a = doc.createElement("a"); |
| 170 assert_throws("HierarchyRequestError", function() { |
| 171 doc.insertBefore(a, doc.doctype); |
| 172 }); |
| 173 assert_throws("HierarchyRequestError", function() { |
| 174 doc.insertBefore(a, doc.documentElement); |
| 175 }); |
| 176 assert_throws("HierarchyRequestError", function() { |
| 177 doc.insertBefore(a, comment); |
| 178 }); |
| 179 assert_throws("HierarchyRequestError", function() { |
| 180 doc.insertBefore(a, null); |
| 181 }); |
| 182 }, "If the context node is a document, inserting an element if there already is
an element child should throw a HierarchyRequestError.") |
| 183 test(function() { |
| 184 // /child/ is a doctype. |
| 185 var doc = document.implementation.createHTMLDocument("title"); |
| 186 var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild); |
| 187 doc.removeChild(doc.documentElement); |
| 188 assert_array_equals(doc.childNodes, [comment, doc.doctype]); |
| 189 |
| 190 var a = doc.createElement("a"); |
| 191 assert_throws("HierarchyRequestError", function() { |
| 192 doc.insertBefore(a, doc.doctype); |
| 193 }); |
| 194 }, "If the context node is a document, inserting an element before the doctype s
hould throw a HierarchyRequestError.") |
| 195 test(function() { |
| 196 // /child/ is not null and a doctype is following /child/. |
| 197 var doc = document.implementation.createHTMLDocument("title"); |
| 198 var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild); |
| 199 doc.removeChild(doc.documentElement); |
| 200 assert_array_equals(doc.childNodes, [comment, doc.doctype]); |
| 201 |
| 202 var a = doc.createElement("a"); |
| 203 assert_throws("HierarchyRequestError", function() { |
| 204 doc.insertBefore(a, comment); |
| 205 }); |
| 206 }, "If the context node is a document and a doctype is following the reference c
hild, inserting an element should throw a HierarchyRequestError.") |
| 207 |
| 208 // Step 4.4. |
| 209 test(function() { |
| 210 var doc = document.implementation.createHTMLDocument("title"); |
| 211 var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild); |
| 212 assert_array_equals(doc.childNodes, [comment, doc.doctype, doc.documentElement
]); |
| 213 |
| 214 var doctype = document.implementation.createDocumentType("html", "", ""); |
| 215 assert_throws("HierarchyRequestError", function() { |
| 216 doc.insertBefore(doctype, comment); |
| 217 }); |
| 218 assert_throws("HierarchyRequestError", function() { |
| 219 doc.insertBefore(doctype, doc.doctype); |
| 220 }); |
| 221 assert_throws("HierarchyRequestError", function() { |
| 222 doc.insertBefore(doctype, doc.documentElement); |
| 223 }); |
| 224 assert_throws("HierarchyRequestError", function() { |
| 225 doc.insertBefore(doctype, null); |
| 226 }); |
| 227 }, "If the context node is a document, inserting a doctype if there already is a
doctype child should throw a HierarchyRequestError.") |
| 228 test(function() { |
| 229 var doc = document.implementation.createHTMLDocument("title"); |
| 230 var comment = doc.appendChild(doc.createComment("foo")); |
| 231 doc.removeChild(doc.doctype); |
| 232 assert_array_equals(doc.childNodes, [doc.documentElement, comment]); |
| 233 |
| 234 var doctype = document.implementation.createDocumentType("html", "", ""); |
| 235 assert_throws("HierarchyRequestError", function() { |
| 236 doc.insertBefore(doctype, comment); |
| 237 }); |
| 238 }, "If the context node is a document, inserting a doctype after the document el
ement should throw a HierarchyRequestError.") |
| 239 test(function() { |
| 240 var doc = document.implementation.createHTMLDocument("title"); |
| 241 var comment = doc.appendChild(doc.createComment("foo")); |
| 242 doc.removeChild(doc.doctype); |
| 243 assert_array_equals(doc.childNodes, [doc.documentElement, comment]); |
| 244 |
| 245 var doctype = document.implementation.createDocumentType("html", "", ""); |
| 246 assert_throws("HierarchyRequestError", function() { |
| 247 doc.insertBefore(doctype, null); |
| 248 }); |
| 249 }, "If the context node is a document with and element child, appending a doctyp
e should throw a HierarchyRequestError.") |
| 250 |
| 251 // Step 5. |
| 252 test(function() { |
| 253 var df = document.createDocumentFragment(); |
| 254 var a = df.appendChild(document.createElement("a")); |
| 255 |
| 256 var doc = document.implementation.createHTMLDocument("title"); |
| 257 assert_throws("HierarchyRequestError", function() { |
| 258 df.insertBefore(doc, a); |
| 259 }); |
| 260 assert_throws("HierarchyRequestError", function() { |
| 261 df.insertBefore(doc, null); |
| 262 }); |
| 263 |
| 264 var doctype = document.implementation.createDocumentType("html", "", ""); |
| 265 assert_throws("HierarchyRequestError", function() { |
| 266 df.insertBefore(doctype, a); |
| 267 }); |
| 268 assert_throws("HierarchyRequestError", function() { |
| 269 df.insertBefore(doctype, null); |
| 270 }); |
| 271 }, "If the context node is a DocumentFragment, inserting a document or a doctype
should throw a HierarchyRequestError.") |
| 272 test(function() { |
| 273 var el = document.createElement("div"); |
| 274 var a = el.appendChild(document.createElement("a")); |
| 275 |
| 276 var doc = document.implementation.createHTMLDocument("title"); |
| 277 assert_throws("HierarchyRequestError", function() { |
| 278 el.insertBefore(doc, a); |
| 279 }); |
| 280 assert_throws("HierarchyRequestError", function() { |
| 281 el.insertBefore(doc, null); |
| 282 }); |
| 283 |
| 284 var doctype = document.implementation.createDocumentType("html", "", ""); |
| 285 assert_throws("HierarchyRequestError", function() { |
| 286 el.insertBefore(doctype, a); |
| 287 }); |
| 288 assert_throws("HierarchyRequestError", function() { |
| 289 el.insertBefore(doctype, null); |
| 290 }); |
| 291 }, "If the context node is an element, inserting a document or a doctype should
throw a HierarchyRequestError.") |
| 292 |
| 293 // Step 7. |
| 294 test(function() { |
| 295 var a = document.createElement("div"); |
| 296 var b = document.createElement("div"); |
| 297 var c = document.createElement("div"); |
| 298 a.appendChild(b); |
| 299 a.appendChild(c); |
| 300 assert_array_equals(a.childNodes, [b, c]); |
| 301 assert_equals(a.insertBefore(b, b), b); |
| 302 assert_array_equals(a.childNodes, [b, c]); |
| 303 assert_equals(a.insertBefore(c, c), c); |
| 304 assert_array_equals(a.childNodes, [b, c]); |
| 305 }, "Inserting a node before itself should not move the node"); |
| 306 </script> |
OLD | NEW |