Search in sources :

Example 16 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 17 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)

Example 18 with DomAttr

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

the class HtmlUnitPrefixResolver method getNamespace.

private String getNamespace(final DomElement element, final String prefix) {
    final Map<String, DomAttr> attributes = element.getAttributesMap();
    final String xmlns = "xmlns:";
    final int xmlnsLength = xmlns.length();
    for (final Map.Entry<String, DomAttr> entry : attributes.entrySet()) {
        final String name = entry.getKey();
        if (name.startsWith(xmlns) && name.regionMatches(xmlnsLength, prefix, 0, prefix.length())) {
            return entry.getValue().getValue();
        }
    }
    for (final DomNode child : element.getChildren()) {
        if (child instanceof DomElement) {
            final String namespace = getNamespace((DomElement) child, prefix);
            if (namespace != null) {
                return namespace;
            }
        }
    }
    return null;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) Map(java.util.Map)

Example 19 with DomAttr

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

the class DOMTokenList method updateAttribute.

private void updateAttribute(final String value) {
    final DomElement domNode = (DomElement) getDomNodeOrDie();
    DomAttr attr = (DomAttr) domNode.getAttributes().getNamedItem(attributeName_);
    if (null == attr) {
        attr = domNode.getPage().createAttribute(attributeName_);
    }
    attr.setValue(value);
    domNode.setAttributeNode(attr);
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr)

Aggregations

DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)19 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 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)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