<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script>

description("Tests ParentNode.append((Node or DOMString)...)");

debug("Test Element.append((Node or DOMString)...).");
debug("");
debug("Test single node.");
var container = document.createElement("div");
var d1 = document.createElement("div");
container.append(d1);
shouldBe("d1.parentNode", "container");
debug("");

debug("Test multiple nodes.");
container = document.createElement("div");
d1 = document.createElement("div");
var d2 = document.createElement("div");
container.append(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.append("hello");
shouldBeType("container.firstChild", "Text");
shouldBe("container.firstChild.data", "'hello'");
debug("");

debug("Test multiple strings.");
container = document.createElement("div");
container.append("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.append("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.append(d2, "hello");
shouldBeType("container.childNodes[0]", "Element");
shouldBe("container.childNodes[0]", "d1");
shouldBeType("container.childNodes[1]", "Element");
shouldBe("container.childNodes[1]", "d2");
shouldBeType("container.childNodes[2]", "Text");
shouldBe("container.childNodes[2].data", "'hello'");
debug("");

debug("Test conversion to string");
container = document.createElement("div");
container.append(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.append()");
shouldBe("container.childNodes.length", "0");
debug("");

debug("Test error condition of self-appending.");
container = document.createElement("div");
shouldThrow("container.append(container)");
debug("");

debug("Test DocumentFragment.append((Node or DOMString)...).");
var fragment = document.createDocumentFragment();
d1 = document.createElement("div");
fragment.append(d1);
shouldBe("d1.parentNode", "fragment");
debug("");


debug("Test Document.append((Node or DOMString)...).");
debug("")
debug("Test simple case of single child append.")
var doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
    doc.firstChild.remove();
d1 = doc.createElement("div");
doc.append(d1);
shouldBe("d1.parentNode", "doc");
debug("");

debug("Test case appending 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.append(d1, c1);
shouldBe("d1.parentNode", "doc");
shouldBe("c1.parentNode", "doc");
debug("");

debug("Test error condition appending 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.append(d2)");
debug("");

debug("Test error condition appending Text node as the root element.")
doc = document.implementation.createHTMLDocument();
while (doc.firstChild)
    doc.firstChild.remove();
shouldThrow("doc.append('hello')");

</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
