Search in sources :

Example 1 with CSSStyleDeclaration

use of com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration 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 2 with CSSStyleDeclaration

use of com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration in project htmlunit by HtmlUnit.

the class DomNode method isDisplayed.

/**
 * <p>Returns {@code true} if this node is displayed and can be visible to the user
 * (ignoring screen size, scrolling limitations, color, font-size, or overlapping nodes).</p>
 *
 * <p><b>NOTE:</b> If CSS is
 * {@link com.gargoylesoftware.htmlunit.WebClientOptions#setCssEnabled(boolean) disabled}, this method
 * does <b>not</b> take this element's style into consideration!</p>
 *
 * @see <a href="http://www.w3.org/TR/CSS2/visufx.html#visibility">CSS2 Visibility</a>
 * @see <a href="http://www.w3.org/TR/CSS2/visuren.html#propdef-display">CSS2 Display</a>
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms531180.aspx">MSDN Documentation</a>
 * @return {@code true} if the node is visible to the user, {@code false} otherwise
 * @see #mayBeDisplayed()
 */
public boolean isDisplayed() {
    if (!mayBeDisplayed()) {
        return false;
    }
    final Page page = getPage();
    final WebWindow window = page.getEnclosingWindow();
    final WebClient webClient = window.getWebClient();
    if (webClient.getOptions().isCssEnabled() && webClient.isJavaScriptEnabled()) {
        // display: iterate top to bottom, because if a parent is display:none,
        // there's nothing that a child can do to override it
        final List<Node> ancestors = getAncestors();
        final ArrayList<CSSStyleDeclaration> styles = new ArrayList<>(ancestors.size());
        for (final Node node : ancestors) {
            if (node instanceof HtmlElement && ((HtmlElement) node).isHidden()) {
                return false;
            }
            if (node instanceof HtmlElement) {
                final CSSStyleDeclaration style = window.getComputedStyle((HtmlElement) node, null);
                if (DisplayStyle.NONE.value().equals(style.getDisplay())) {
                    return false;
                }
                styles.add(style);
            }
        }
        // the visibility used by parent nodes
        for (int i = styles.size() - 1; i >= 0; i--) {
            final CSSStyleDeclaration style = styles.get(i);
            final String visibility = style.getStyleAttribute(StyleAttributes.Definition.VISIBILITY);
            if (visibility.length() > 5) {
                if ("visible".equals(visibility)) {
                    return true;
                }
                if ("hidden".equals(visibility) || "collapse".equals(visibility)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Page(com.gargoylesoftware.htmlunit.Page) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) CSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration) WebClient(com.gargoylesoftware.htmlunit.WebClient) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 3 with CSSStyleDeclaration

use of com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration in project htmlunit by HtmlUnit.

the class Element method isDisplayNone.

/**
 * Returns whether the {@code display} is {@code none} or not.
 * @return whether the {@code display} is {@code none} or not
 */
protected final boolean isDisplayNone() {
    Element element = this;
    while (element != null) {
        final CSSStyleDeclaration style = element.getWindow().getComputedStyle(element, null);
        final String display = style.getDisplay();
        if (DisplayStyle.NONE.value().equals(display)) {
            return true;
        }
        element = element.getParentElement();
    }
    return false;
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLTemplateElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) ComputedCSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration) CSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration)

Aggregations

CSSStyleDeclaration (com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 ComputedCSSStyleDeclaration (com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration)2 Page (com.gargoylesoftware.htmlunit.Page)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1 HTMLScriptElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement)1 HTMLStyleElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement)1 HTMLTemplateElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement)1 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)1 ArrayList (java.util.ArrayList)1 Node (org.w3c.dom.Node)1