/** appendChild, insertBefore, removeChild, replaceChild. */ public void testAppendRemoveReplace() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); // appendChild, insertBefore ButtonElement btn1 = doc.createButtonElement(); // <div>foo<btn0/>bar<btn1/></div> div.appendChild(btn1); assertEquals(btn1, div.getLastChild()); // <div>foo<button/>bar<button/></div> div.insertBefore(btn1, txt1); assertEquals(4, div.getChildNodes().getLength()); assertEquals(btn1, div.getChildNodes().getItem(2)); // removeChild // <div>foo<btn0/>bar</div> (back to original) div.removeChild(btn1); assertEquals(3, div.getChildNodes().getLength()); // replaceChild // <div>foo<btn1/>bar</div> div.replaceChild(btn1, btn0); assertEquals(btn1, txt0.getNextSibling()); assertEquals(btn1, txt1.getPreviousSibling()); // insertAfter // <div>foo<btn1/><btn0/>bar</div> div.insertAfter(btn0, btn1); assertEquals(btn0, btn1.getNextSibling()); // insertFirst // <div><btn1/>foo<btn0/>bar</div> div.insertFirst(btn1); assertEquals(btn1, div.getFirstChild()); // removeFromParent // <div>foo<btn0/>bar</div> btn1.removeFromParent(); assertNull(btn1.getParentElement()); assertEquals(txt0, div.getFirstChild()); }
/** childNodes, hasChildNodes. */ public void testChildNodeList() { Document doc = Document.get(); BodyElement body = doc.getBody(); // <div>foo<button/>bar</div> DivElement div = doc.createDivElement(); Text txt0 = doc.createTextNode("foo"); ButtonElement btn0 = doc.createButtonElement(); Text txt1 = doc.createTextNode("bar"); body.appendChild(div); div.appendChild(txt0); div.appendChild(btn0); div.appendChild(txt1); NodeList<Node> children = div.getChildNodes(); assertEquals(3, children.getLength()); assertEquals(txt0, children.getItem(0)); assertEquals(btn0, children.getItem(1)); assertEquals(txt1, children.getItem(2)); assertEquals(3, div.getChildCount()); assertEquals(txt0, div.getChild(0)); assertEquals(btn0, div.getChild(1)); assertEquals(txt1, div.getChild(2)); assertFalse(txt0.hasChildNodes()); assertTrue(div.hasChildNodes()); }