blob: 62106faa486af333837c904eac2bcba99fad0c31 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description("Tests ParentNode.prepend((Node or DOMString)...)");
debug("Test Element.prepend((Node or DOMString)...).");
debug("");
debug("Test single node.");
var container = document.createElement("div");
var d1 = document.createElement("div");
container.prepend(d1);
shouldBe("d1.parentNode", "container");
debug("");
debug("Test multiple nodes.");
container = document.createElement("div");
d1 = document.createElement("div");
var d2 = document.createElement("div");
container.prepend(d1, d2);
shouldBe("d1.parentNode", "container");
shouldBe("d2.parentNode", "container");
shouldBe("d1.nextSibling", "d2");
shouldBe("d2.previousSibling", "d1");
debug("");
debug("Test a single string.");
container = document.createElement("div");
container.prepend("hello");
shouldBeType("container.firstChild", "Text");
shouldBe("container.firstChild.data", "'hello'");
debug("");
debug("Test multiple strings.");
container = document.createElement("div");
container.prepend("hello", "world");
shouldBeType("container.childNodes[0]", "Text");
shouldBe("container.childNodes[0].data", "'hello'");
shouldBeType("container.childNodes[1]", "Text");
shouldBe("container.childNodes[1].data", "'world'");
debug("");
debug("Test nodes and strings.");
container = document.createElement("div");
d1 = document.createElement("div");
container.prepend("hello", d1, "world");
shouldBeType("container.childNodes[0]", "Text");
shouldBeType("container.childNodes[1]", "Element");
shouldBeType("container.childNodes[2]", "Text");
debug("");
debug("Test when container already has children.");
container = document.createElement("div");
d1 = document.createElement("div");
d2 = document.createElement("div");
container.appendChild(d1);
container.prepend(d2, "hello");
shouldBeType("container.childNodes[0]", "Element");
shouldBe("container.childNodes[0]", "d2");
shouldBeType("container.childNodes[1]", "Text");
shouldBe("container.childNodes[1].data", "'hello'");
shouldBeType("container.childNodes[2]", "Element");
shouldBe("container.childNodes[2]", "d1");
debug("");
debug("Test conversion to string");
container = document.createElement("div");
container.prepend(null, undefined, 7);
shouldBeType("container.childNodes[0]", "Text");
shouldBe("container.childNodes[0].data", "'null'");
shouldBeType("container.childNodes[1]", "Text");
shouldBe("container.childNodes[1].data", "'undefined'");
shouldBeType("container.childNodes[2]", "Text");
shouldBe("container.childNodes[2].data", "'7'");
debug("");
debug("Test no parameters");
container = document.createElement("div");
shouldNotThrow("container.prepend()");
shouldBe("container.childNodes.length", "0");
debug("");
debug("Test error condition of self-prepending.");
container = document.createElement("div");
shouldThrow("container.prepend(container)");
debug("");
debug("Test DocumentFragment.prepend((Node or DOMString)...).");
var fragment = document.createDocumentFragment();
d1 = document.createElement("div");
fragment.prepend(d1);
shouldBe("d1.parentNode", "fragment");
debug("");
debug("Test Document.prepend((Node or DOMString)...).");
debug("")
debug("Test simple case of single child prepend.")
var doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
doc.firstChild.remove();
d1 = doc.createElement("div");
doc.prepend(d1);
shouldBe("d1.parentNode", "doc");
debug("");
debug("Test case prepending a root element and a comment.")
var doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
doc.firstChild.remove();
d1 = doc.createElement("div");
c1 = doc.createComment("comment text");
doc.prepend(d1, c1);
shouldBe("d1.parentNode", "doc");
shouldBe("c1.parentNode", "doc");
debug("");
debug("Test error condition prepending a second root element.")
doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
doc.firstChild.remove();
d1 = doc.createElement("div");
d2 = doc.createElement("div");
doc.appendChild(d1);
shouldThrow("doc.prepend(d2)");
debug("");
debug("Test error condition prepending Text node as the root element.")
doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
doc.firstChild.remove();
shouldThrow("doc.prepend('hello')");
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>