private static void setupStyle(JavaScriptObject newNode, Style oldStyle) { Style newStyle = JavaScriptObjects.getObject(newNode, JsoProperties.STYLE_OBJECT_FIELD); for (Map.Entry<String, Object> entry : JavaScriptObjects.entrySet(oldStyle)) { if (!JsoProperties.STYLE_TARGET_ELEMENT.equals(entry.getKey())) { JavaScriptObjects.setProperty(newStyle, entry.getKey(), entry.getValue()); } } }
@PatchMethod static void setPropertyObject(Element element, String name, Object value) { PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); properties.put(name, value); }
@PatchMethod static void setInnerHTML(Element element, String html) { List<Node> innerList = JavaScriptObjects.getObject(element.getChildNodes(), JsoProperties.NODE_LIST_INNER_LIST); innerList.clear(); NodeList<Node> nodes = GwtHtmlParser.parse(html); for (int i = 0; i < nodes.getLength(); i++) { innerList.add(nodes.getItem(i)); } }
private static void copyJSOProperties( JavaScriptObject newJso, JavaScriptObject oldJso, boolean deep) { for (Map.Entry<String, Object> entry : JavaScriptObjects.entrySet(oldJso)) { if (JsoProperties.PARENT_NODE_FIELD.equals(entry.getKey())) { // Nothing to do : new cloned node does not have any parent } else if (JsoProperties.STYLE_OBJECT_FIELD.equals(entry.getKey())) { setupStyle(newJso, (Style) entry.getValue()); } else if (JsoProperties.NODE_LIST_FIELD.equals(entry.getKey())) { Node newNode = newJso.cast(); Node oldNode = oldJso.cast(); setupChildNodes(newNode, oldNode, deep); } else if (JsoProperties.ELEM_PROPERTIES.equals(entry.getKey())) { PropertyContainer newPc = JavaScriptObjects.getObject(newJso, JsoProperties.ELEM_PROPERTIES); PropertyContainer oldPc = JavaScriptObjects.getObject(oldJso, JsoProperties.ELEM_PROPERTIES); for (Map.Entry<String, Object> entry2 : oldPc.entrySet()) { newPc.put(entry2.getKey(), entry2.getValue()); } } else if (JavaScriptObject.class.isInstance(entry.getValue())) { JavaScriptObject oldChildJso = (JavaScriptObject) entry.getValue(); JavaScriptObject newChildJso = JavaScriptObjects.newObject(oldChildJso.getClass()); copyJSOProperties(newChildJso, oldChildJso, deep); JavaScriptObjects.setProperty(newJso, entry.getKey(), newChildJso); } else { // copy the property, which should be a String or a primitive type (or // corresponding wrapper object) JavaScriptObjects.setProperty(newJso, entry.getKey(), entry.getValue()); } } }
@PatchMethod static Object getPropertyObject(Element element, String propertyName) { if ("tagName".equals(propertyName)) { return element.getTagName().toUpperCase(); } else if ("style".equals(propertyName)) { return element.getStyle(); } PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); return properties.getObject(propertyName); }
@PatchMethod static void removeAttribute(Element element, String name) { PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); String standardDOMPropertyName = JsoProperties.get().getStandardDOMPropertyName(name); if (standardDOMPropertyName != null) { properties.remove(standardDOMPropertyName); } else { properties.remove(name.toLowerCase()); } }
@PatchMethod static void setAttribute(Element element, String attributeName, String value) { PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); String standardDOMPropertyName = JsoProperties.get().getStandardDOMPropertyName(attributeName); if (standardDOMPropertyName != null) { properties.put(standardDOMPropertyName, value); } else { properties.put(attributeName.toLowerCase(), value); } }
@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; }
@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()); } }
static Node insertAtIndex(Node parent, Node newChild, int index) { List<Node> list = getChildNodeList(parent); // First, remove from old parent Node oldParent = newChild.getParentNode(); if (oldParent != null) { oldParent.removeChild(newChild); } // Then, check parent doesn't contain newChild and remove it if necessary list.remove(newChild); // Finally, add if (index == -1 || index >= list.size()) { list.add(newChild); } else { list.add(index, newChild); } // Manage getParentNode() JavaScriptObjects.setProperty(newChild, JsoProperties.PARENT_NODE_FIELD, parent); return newChild; }
private static List<Node> getChildNodeList(Node node) { NodeList<Node> nodeList = JavaScriptObjects.getObject(node, JsoProperties.NODE_LIST_FIELD); return JavaScriptObjects.getObject(nodeList, JsoProperties.NODE_LIST_INNER_LIST); }
@PatchMethod static short getNodeType(Node node) { short nodeType = JavaScriptObjects.getShort(node, JsoProperties.NODE_TYPE_FIELD); return nodeType != 0 ? nodeType : -1; }
@PatchMethod static boolean getPropertyBoolean(Element element, String propertyName) { PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); return properties.getBoolean(propertyName); }
@PatchMethod static int getClientWidth(Element element) { return JavaScriptObjects.getInteger(element, JsoProperties.ELEMENT_CLIENT_WIDTH); }
@PatchMethod static int getClientHeight(Element element) { return JavaScriptObjects.getInteger(element, JsoProperties.ELEMENT_CLIENT_HEIGHT); }
@PatchMethod static int getPropertyInt(Element element, String propertyName) { PropertyContainer properties = JavaScriptObjects.getObject(element, JsoProperties.ELEM_PROPERTIES); return properties.getInteger(propertyName); }
@PatchMethod static Style getStyle(Element element) { return JavaScriptObjects.getObject(element, JsoProperties.STYLE_OBJECT_FIELD); }