Search in sources :

Example 1 with DomAttr

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

the class HTMLElement method readAttributes.

/**
 * Gets the attributes of the element in the form of a {@link org.xml.sax.Attributes}.
 * @param element the element to read the attributes from
 * @return the attributes
 */
protected AttributesImpl readAttributes(final HtmlElement element) {
    final AttributesImpl attributes = new AttributesImpl();
    for (final DomAttr entry : element.getAttributesMap().values()) {
        final String name = entry.getName();
        final String value = entry.getValue();
        attributes.addAttribute(null, name, name, null, value);
    }
    return attributes;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr)

Example 2 with DomAttr

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

the class WindowConcurrencyTest method concurrentModificationException_computedStyles.

/**
 * Regression test for
 * <a href="http://sourceforge.net/support/tracker.php?aid=2820051">bug 2820051</a>.
 * @throws Exception if the test fails
 */
@Test
public void concurrentModificationException_computedStyles() throws Exception {
    final String html = "<html><head><script>\n" + "function test() {\n" + "  getComputedStyle(document.body, null);\n" + "}\n" + "</script></head><body onload='test()'>\n" + "<iframe src='foo.html' name='myFrame' id='myFrame'></iframe>\n" + "</body></html>";
    final String html2 = "<html><head><script>\n" + "function forceStyleComputationInParent() {\n" + "  var newNode = parent.document.createElement('span');\n" + "  parent.document.body.appendChild(newNode);\n" + "  parent.getComputedStyle(newNode, null);\n" + "}\n" + "setInterval(forceStyleComputationInParent, 10);\n" + "</script></head></body></html>";
    try (WebClient client = new WebClient(BrowserVersion.FIREFOX)) {
        final MockWebConnection webConnection = new MockWebConnection();
        webConnection.setResponse(URL_FIRST, html);
        webConnection.setDefaultResponse(html2);
        client.setWebConnection(webConnection);
        final HtmlPage page1 = client.getPage(URL_FIRST);
        // Recreating what can occur with two threads requires
        // to know a bit about the style invalidation used in Window.DomHtmlAttributeChangeListenerImpl
        final HtmlElement elt = new HtmlDivision("div", page1, new HashMap<String, DomAttr>()) {

            @Override
            public DomNode getParentNode() {
                // this gets called by CSS invalidation logic
                try {
                    // enough to let setInterval run
                    Thread.sleep(1000);
                } catch (final InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return super.getParentNode();
            }
        };
        page1.getBody().appendChild(elt);
    }
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlDivision(com.gargoylesoftware.htmlunit.html.HtmlDivision) MockWebConnection(com.gargoylesoftware.htmlunit.MockWebConnection) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 3 with DomAttr

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

the class XMLDOMElement method setAttributeNode.

/**
 * Sets or updates the supplied attribute node on this element.
 * @param newAtt the attribute node to be associated with this element
 * @return the replaced attribute node, if any, {@code null} otherwise
 */
@JsxFunction
public XMLDOMAttribute setAttributeNode(final XMLDOMAttribute newAtt) {
    if (newAtt == null) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    final String name = newAtt.getBaseName();
    final XMLDOMNamedNodeMap nodes = (XMLDOMNamedNodeMap) getAttributes();
    final XMLDOMAttribute replacedAtt = (XMLDOMAttribute) nodes.getNamedItemWithoutSyntheticClassAttr(name);
    if (replacedAtt != null) {
        replacedAtt.detachFromParent();
    }
    final DomAttr newDomAttr = newAtt.getDomNodeOrDie();
    getDomNodeOrDie().setAttributeNode(newDomAttr);
    return replacedAtt;
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with DomAttr

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

the class Element method setDomNode.

/**
 * Sets the DOM node that corresponds to this JavaScript object.
 * @param domNode the DOM node
 */
@Override
public void setDomNode(final DomNode domNode) {
    super.setDomNode(domNode);
    setParentScope(getWindow().getDocument());
    // CSSStyleDeclaration uses the parent scope
    style_ = new CSSStyleDeclaration(this);
    // Convert JavaScript snippets defined in the attribute map to executable event handlers.
    // Should be called only on construction.
    final DomElement htmlElt = (DomElement) domNode;
    for (final DomAttr attr : htmlElt.getAttributesMap().values()) {
        final String eventName = attr.getName().toLowerCase(Locale.ROOT);
        if (eventName.startsWith("on")) {
            createEventHandler(eventName.substring(2), attr.getValue());
        }
    }
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) ComputedCSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration) CSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration)

Example 5 with DomAttr

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

the class XmlUtils method createFrom.

private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    if (source.getNodeType() == Node.TEXT_NODE) {
        return new DomText(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
    }
    if (source.getNodeType() == Node.COMMENT_NODE) {
        return new DomComment(page, source.getNodeValue());
    }
    if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        final DocumentType documentType = (DocumentType) source;
        return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
    }
    final String ns = source.getNamespaceURI();
    String localName = source.getLocalName();
    if (handleXHTMLAsHTML && Html.XHTML_NAMESPACE.equals(ns)) {
        final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(localName);
        return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes(), attributesOrderMap, source));
    }
    final NamedNodeMap nodeAttributes = source.getAttributes();
    if (page != null && page.isHtmlPage()) {
        localName = localName.toUpperCase(Locale.ROOT);
    }
    final String qualifiedName;
    if (source.getPrefix() == null) {
        qualifiedName = localName;
    } else {
        qualifiedName = source.getPrefix() + ':' + localName;
    }
    final String namespaceURI = source.getNamespaceURI();
    if (Html.SVG_NAMESPACE.equals(namespaceURI)) {
        return page.getWebClient().getPageCreator().getHtmlParser().getSvgFactory().createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes, attributesOrderMap, source));
    }
    final Map<String, DomAttr> attributes = new LinkedHashMap<>();
    for (int i = 0; i < nodeAttributes.getLength(); i++) {
        final int orderedIndex = getIndex(nodeAttributes, attributesOrderMap, source, i);
        final Attr attribute = (Attr) nodeAttributes.item(orderedIndex);
        final String attributeNamespaceURI = attribute.getNamespaceURI();
        final String attributeQualifiedName;
        if (attribute.getPrefix() == null) {
            attributeQualifiedName = attribute.getLocalName();
        } else {
            attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
        }
        final String value = attribute.getNodeValue();
        final boolean specified = attribute.getSpecified();
        final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified);
        attributes.put(attribute.getNodeName(), xmlAttribute);
    }
    return new DomElement(namespaceURI, qualifiedName, page, attributes);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomDocumentType(com.gargoylesoftware.htmlunit.html.DomDocumentType) DocumentType(org.w3c.dom.DocumentType) Attr(org.w3c.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomDocumentType(com.gargoylesoftware.htmlunit.html.DomDocumentType) LinkedHashMap(java.util.LinkedHashMap) DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomText(com.gargoylesoftware.htmlunit.html.DomText) DomProcessingInstruction(com.gargoylesoftware.htmlunit.html.DomProcessingInstruction) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory)

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