Search in sources :

Example 11 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class XMLSerializer method toXml.

private void toXml(final int indent, final DomNode node, final StringBuilder builder) {
    final String nodeName = node.getNodeName();
    builder.append('<').append(nodeName);
    final String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement; parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;
                break;
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    }
    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        builder.append(' ').append(attrib.getQualifiedName()).append("=\"").append(attrib.getValue()).append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            builder.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch(child.getNodeType()) {
            case Node.ELEMENT_NODE:
                toXml(indent + 1, child, builder);
                break;
            case Node.TEXT_NODE:
                String value = child.getNodeValue();
                value = StringUtils.escapeXmlChars(value);
                if (preserveWhiteSpace_) {
                    builder.append(value.replace("\n", "\r\n"));
                } else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
                    builder.append("\r\n");
                    final DomNode sibling = child.getNextSibling();
                    if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
                        for (int i = 0; i < indent; i++) {
                            builder.append('\t');
                        }
                    }
                } else {
                    builder.append(value.replace("\n", "\r\n"));
                }
                break;
            case Node.CDATA_SECTION_NODE:
            case Node.COMMENT_NODE:
                if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
                    for (int i = 0; i < indent; i++) {
                        builder.append('\t');
                    }
                }
                builder.append(child.asXml());
                break;
            default:
        }
    }
    if (startTagClosed) {
        if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
            for (int i = 0; i < indent - 1; i++) {
                builder.append('\t');
            }
        }
        builder.append("</").append(nodeName).append('>');
    } else {
        builder.append(optionalPrefix).append("/>");
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) NamedNodeMap(org.w3c.dom.NamedNodeMap) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr)

Example 12 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class XMLDOMAttribute method detachFromParent.

/**
 * Detaches this attribute from the parent HTML element after caching the attribute value.
 */
public void detachFromParent() {
    final DomAttr domNode = getDomNodeOrDie();
    final DomElement parent = (DomElement) domNode.getParentNode();
    if (parent != null) {
        domNode.setValue(parent.getAttribute(getName()));
    }
    domNode.remove();
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomElement(com.gargoylesoftware.htmlunit.html.DomElement)

Example 13 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class XMLDocument method makeScriptableFor.

/**
 * {@inheritDoc}
 */
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
    final HtmlUnitScriptable scriptable;
    // TODO: cleanup, getScriptObject() should be used!!!
    if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) {
        if (domNode instanceof SvgElement) {
            final Class<? extends HtmlUnitScriptable> javaScriptClass = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine()).getJavaScriptClass(domNode.getClass());
            try {
                scriptable = javaScriptClass.newInstance();
            } catch (final Exception e) {
                throw Context.throwAsScriptRuntimeEx(e);
            }
        } else {
            scriptable = new Element();
        }
    } else if (domNode instanceof DomAttr) {
        scriptable = new Attr();
    } else {
        return super.makeScriptableFor(domNode);
    }
    scriptable.setPrototype(getPrototype(scriptable.getClass()));
    scriptable.setParentScope(getParentScope());
    scriptable.setDomNode(domNode);
    return scriptable;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) IOException(java.io.IOException) Attr(com.gargoylesoftware.htmlunit.javascript.host.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 14 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class XmlUtils method lookupPrefix.

/**
 * Search for the prefix associated with specified namespace URI.
 * @param element the element to start searching from
 * @param namespace the namespace prefix
 * @return the prefix bound to the namespace URI; or null if there is no such namespace
 */
public static String lookupPrefix(final DomElement element, final String namespace) {
    final Map<String, DomAttr> attributes = element.getAttributesMap();
    for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) {
        final String name = entry.getKey();
        final DomAttr value = entry.getValue();
        if (name.startsWith("xmlns:") && value.getValue().equals(namespace)) {
            return name.substring(6);
        }
    }
    for (final DomNode child : element.getChildren()) {
        if (child instanceof DomElement) {
            final String prefix = lookupPrefix((DomElement) child, namespace);
            if (prefix != null) {
                return prefix;
            }
        }
    }
    return null;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 15 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class SvgElementFactory method toMap.

private static Map<String, DomAttr> toMap(final SgmlPage page, final Attributes attributes) {
    Map<String, DomAttr> attributeMap = null;
    if (attributes != null) {
        attributeMap = new LinkedHashMap<>(attributes.getLength());
        for (int i = 0; i < attributes.getLength(); i++) {
            final String qName = attributes.getQName(i);
            // browsers consider only first attribute (ex: <div id='foo' id='something'>...</div>)
            if (!attributeMap.containsKey(qName)) {
                String namespaceURI = attributes.getURI(i);
                if (namespaceURI != null && namespaceURI.isEmpty()) {
                    namespaceURI = null;
                }
                final DomAttr newAttr = new DomAttr(page, namespaceURI, qName, attributes.getValue(i), true);
                attributeMap.put(qName, newAttr);
            }
        }
    }
    return attributeMap;
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr)

Aggregations

DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)17 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)11 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)2 DomText (com.gargoylesoftware.htmlunit.html.DomText)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 Attr (com.gargoylesoftware.htmlunit.javascript.host.dom.Attr)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 DomCharacterData (com.gargoylesoftware.htmlunit.html.DomCharacterData)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlDivision (com.gargoylesoftware.htmlunit.html.HtmlDivision)1