@Test public void clone_Deep() { // Arrange AnchorElement child = Document.get().createAnchorElement(); child.setInnerText("child inner text"); child.getStyle().setBackgroundColor("black"); n.appendChild(child); // Act DivElement newNode = n.cloneNode(true).cast(); // Assert assertEquals(Node.ELEMENT_NODE, newNode.getNodeType()); DivElement source = n.cast(); assertEquals(source.getInnerText(), newNode.getInnerText()); assertEquals(source.getInnerHTML(), newNode.getInnerHTML()); assertEquals(source.toString(), newNode.toString()); assertNull(newNode.getParentNode()); assertEquals(n.getChildNodes().getLength(), newNode.getChildNodes().getLength()); assertEquals(Node.ELEMENT_NODE, newNode.getChildNodes().getItem(0).getNodeType()); AnchorElement childElement = newNode.getChildNodes().getItem(0).cast(); assertEquals("child inner text", childElement.getInnerText()); Style newStyle = childElement.getStyle(); assertTrue(newStyle != child.getStyle()); assertEquals("black", newStyle.getBackgroundColor()); }
@PatchMethod static Element getFirstChild(Element elem) { Node firstChild = elem.getFirstChildElement(); if (firstChild != null) { return firstChild.cast(); } else { return null; } }
@PatchMethod static void setNodeValue(Node node, String nodeValue) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // nothing to do break; case Node.ELEMENT_NODE: // nothing to do break; case Node.TEXT_NODE: Text text = node.cast(); text.setData(nodeValue); break; } }
@PatchMethod static String getNodeValue(Node node) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: return null; case Node.ELEMENT_NODE: return null; case Node.TEXT_NODE: Text text = node.cast(); return text.getData(); default: throw new GwtTestDomException( "Invalid Node type (not a Document / Element / Text : " + node.getNodeType()); } }
/** * Specific function which does not inspect deep. * * @param tag * @return */ private static NodeList<Element> getElementByTagName(TableElement e, String tagName) { NodeList<Node> childs = e.getChildNodes(); List<Element> list = new ArrayList<Element>(); for (int i = 0; i < childs.getLength(); i++) { Node n = childs.getItem(i); if (Element.is(n)) { Element childElement = n.cast(); if (tagName.equalsIgnoreCase(childElement.getTagName())) { list.add(childElement); } } } return JavaScriptObjects.newNodeList(list); }
@PatchMethod static String getNodeName(Node node) { switch (node.getNodeType()) { case Node.DOCUMENT_NODE: return "#document"; case Node.ELEMENT_NODE: Element e = node.cast(); return e.getTagName(); case Node.TEXT_NODE: return "#text"; case com.google.gwt.xml.client.Node.ATTRIBUTE_NODE: return JavaScriptObjects.getString(node, JsoProperties.XML_ATTR_NAME); default: throw new GwtTestDomException( "Invalid Node type (not a Document / Element / Text / Attribute) : " + node.getNodeType()); } }
@PatchMethod static Node cloneNode(Node node, boolean deep) { Node newNode; switch (node.getNodeType()) { case Node.ELEMENT_NODE: Element e = node.cast(); newNode = JavaScriptObjects.newElement(e.getTagName()); break; case Node.DOCUMENT_NODE: newNode = JavaScriptObjects.newObject(Document.class); break; case Node.TEXT_NODE: newNode = JavaScriptObjects.newObject(Text.class); break; default: throw new GwtTestDomException( "Cannot create a Node of type [" + node.getClass().getName() + "]"); } copyJSOProperties(newNode, node, deep); return newNode; }
@Test public void clone_NotDeep() { // Arrange Element e = n.cast(); e.setInnerText("text"); e.getStyle().setBackgroundColor("black"); AnchorElement child = Document.get().createAnchorElement(); child.setInnerText("child inner text"); n.appendChild(child); // Act DivElement newNode = n.cloneNode(false).cast(); // Assert assertEquals(Node.ELEMENT_NODE, newNode.getNodeType()); assertTrue(e.getStyle() != newNode.getStyle()); assertEquals("black", newNode.getStyle().getBackgroundColor()); assertEquals("text", newNode.getInnerText()); assertNull(newNode.getParentNode()); assertEquals(2, n.getChildNodes().getLength()); assertEquals(1, newNode.getChildNodes().getLength()); }