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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<!DOCTYPE html>
<title>Node.insertBefore</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<!-- First test shared pre-insertion checks that work similarly for replaceChild
and insertBefore -->
<script>
var insertFunc = Node.prototype.insertBefore;
</script>
<script src="pre-insertion-validation-notfound.js"></script>
<script src="pre-insertion-validation-hierarchy.js"></script>
<script>
preInsertionValidateHierarchy("insertBefore");
function testLeafNode(nodeName, createNodeFunction) {
test(function() {
var node = createNodeFunction();
assert_throws_js(TypeError, function() { node.insertBefore(null, null) })
}, "Calling insertBefore with a non-Node first argument on a leaf node " + nodeName + " must throw TypeError.")
test(function() {
var node = createNodeFunction();
assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(document.createTextNode("fail"), null) })
// Would be step 2.
assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node, null) })
// Would be step 3.
assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node, document.createTextNode("child")) })
}, "Calling insertBefore an a leaf node " + nodeName + " must throw HIERARCHY_REQUEST_ERR.")
}
test(function() {
// WebIDL: first argument.
assert_throws_js(TypeError, function() { document.body.insertBefore(null, null) })
assert_throws_js(TypeError, function() { document.body.insertBefore(null, document.body.firstChild) })
assert_throws_js(TypeError, function() { document.body.insertBefore({'a':'b'}, document.body.firstChild) })
}, "Calling insertBefore with a non-Node first argument must throw TypeError.")
test(function() {
// WebIDL: second argument.
assert_throws_js(TypeError, function() { document.body.insertBefore(document.createTextNode("child")) })
assert_throws_js(TypeError, function() { document.body.insertBefore(document.createTextNode("child"), {'a':'b'}) })
}, "Calling insertBefore with second argument missing, or other than Node, null, or undefined, must throw TypeError.")
testLeafNode("DocumentType", function () { return document.doctype; } )
testLeafNode("Text", function () { return document.createTextNode("Foo") })
testLeafNode("Comment", function () { return document.createComment("Foo") })
testLeafNode("ProcessingInstruction", function () { return document.createProcessingInstruction("foo", "bar") })
test(function() {
// Step 2.
assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore(document.body, document.getElementById("log")) })
assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore(document.documentElement, document.getElementById("log")) })
}, "Calling insertBefore with an inclusive ancestor of the context object must throw HIERARCHY_REQUEST_ERR.")
// Step 3.
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
assert_throws_dom("NotFoundError", function() {
a.insertBefore(b, c);
});
}, "Calling insertBefore with a reference child whose parent is not the context node must throw a NotFoundError.")
// Step 4.1.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var doc2 = document.implementation.createHTMLDocument("title2");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doc2, doc.documentElement);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doc.createTextNode("text"), doc.documentElement);
});
}, "If the context node is a document, inserting a document or text node should throw a HierarchyRequestError.")
// Step 4.2.1.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
doc.removeChild(doc.documentElement);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
df.appendChild(doc.createElement("b"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.firstChild);
});
df = doc.createDocumentFragment();
df.appendChild(doc.createTextNode("text"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.firstChild);
});
df = doc.createDocumentFragment();
df.appendChild(doc.createComment("comment"));
df.appendChild(doc.createTextNode("text"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.firstChild);
});
}, "If the context node is a document, inserting a DocumentFragment that contains a text node or too many elements should throw a HierarchyRequestError.")
// Step 4.2.2.
test(function() {
// The context node has an element child.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.doctype);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.documentElement);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, comment);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, null);
});
}, "If the context node is a document, inserting a DocumentFragment with an element if there already is an element child should throw a HierarchyRequestError.")
test(function() {
// /child/ is a doctype.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, doc.doctype);
});
}, "If the context node is a document and a doctype is following the reference child, inserting a DocumentFragment with an element should throw a HierarchyRequestError.")
test(function() {
// /child/ is not null and a doctype is following /child/.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var df = doc.createDocumentFragment();
df.appendChild(doc.createElement("a"));
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(df, comment);
});
}, "If the context node is a document, inserting a DocumentFragment with an element before the doctype should throw a HierarchyRequestError.")
// Step 4.3.
test(function() {
// The context node has an element child.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
var a = doc.createElement("a");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, doc.doctype);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, doc.documentElement);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, comment);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, null);
});
}, "If the context node is a document, inserting an element if there already is an element child should throw a HierarchyRequestError.")
test(function() {
// /child/ is a doctype.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var a = doc.createElement("a");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, doc.doctype);
});
}, "If the context node is a document, inserting an element before the doctype should throw a HierarchyRequestError.")
test(function() {
// /child/ is not null and a doctype is following /child/.
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
doc.removeChild(doc.documentElement);
assert_array_equals(doc.childNodes, [comment, doc.doctype]);
var a = doc.createElement("a");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(a, comment);
});
}, "If the context node is a document and a doctype is following the reference child, inserting an element should throw a HierarchyRequestError.")
// Step 4.4.
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
assert_array_equals(doc.childNodes, [comment, doc.doctype, doc.documentElement]);
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, comment);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, doc.doctype);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, doc.documentElement);
});
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, null);
});
}, "If the context node is a document, inserting a doctype if there already is a doctype child should throw a HierarchyRequestError.")
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
doc.removeChild(doc.doctype);
assert_array_equals(doc.childNodes, [doc.documentElement, comment]);
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, comment);
});
}, "If the context node is a document, inserting a doctype after the document element should throw a HierarchyRequestError.")
test(function() {
var doc = document.implementation.createHTMLDocument("title");
var comment = doc.appendChild(doc.createComment("foo"));
doc.removeChild(doc.doctype);
assert_array_equals(doc.childNodes, [doc.documentElement, comment]);
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws_dom("HierarchyRequestError", function() {
doc.insertBefore(doctype, null);
});
}, "If the context node is a document with and element child, appending a doctype should throw a HierarchyRequestError.")
// Step 5.
test(function() {
var df = document.createDocumentFragment();
var a = df.appendChild(document.createElement("a"));
var doc = document.implementation.createHTMLDocument("title");
assert_throws_dom("HierarchyRequestError", function() {
df.insertBefore(doc, a);
});
assert_throws_dom("HierarchyRequestError", function() {
df.insertBefore(doc, null);
});
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws_dom("HierarchyRequestError", function() {
df.insertBefore(doctype, a);
});
assert_throws_dom("HierarchyRequestError", function() {
df.insertBefore(doctype, null);
});
}, "If the context node is a DocumentFragment, inserting a document or a doctype should throw a HierarchyRequestError.")
test(function() {
var el = document.createElement("div");
var a = el.appendChild(document.createElement("a"));
var doc = document.implementation.createHTMLDocument("title");
assert_throws_dom("HierarchyRequestError", function() {
el.insertBefore(doc, a);
});
assert_throws_dom("HierarchyRequestError", function() {
el.insertBefore(doc, null);
});
var doctype = document.implementation.createDocumentType("html", "", "");
assert_throws_dom("HierarchyRequestError", function() {
el.insertBefore(doctype, a);
});
assert_throws_dom("HierarchyRequestError", function() {
el.insertBefore(doctype, null);
});
}, "If the context node is an element, inserting a document or a doctype should throw a HierarchyRequestError.")
// Step 7.
test(function() {
var a = document.createElement("div");
var b = document.createElement("div");
var c = document.createElement("div");
a.appendChild(b);
a.appendChild(c);
assert_array_equals(a.childNodes, [b, c]);
assert_equals(a.insertBefore(b, b), b);
assert_array_equals(a.childNodes, [b, c]);
assert_equals(a.insertBefore(c, c), c);
assert_array_equals(a.childNodes, [b, c]);
}, "Inserting a node before itself should not move the node");
</script>
|