Search in sources :

Example 6 with Element

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

the class ComputedCSSStyleDeclaration method getCalculatedWidth.

private int getCalculatedWidth() {
    if (width_ != null) {
        return width_.intValue();
    }
    final Element element = getElement();
    final DomNode node = element.getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        width_ = Integer.valueOf(0);
        return 0;
    }
    final String display = getDisplay();
    if (NONE.equals(display)) {
        width_ = Integer.valueOf(0);
        return 0;
    }
    final int width;
    final String styleWidth = super.getWidth();
    final DomNode parent = node.getParentNode();
    // width is ignored for inline elements
    if (("inline".equals(display) || StringUtils.isEmpty(styleWidth)) && parent instanceof HtmlElement) {
        // hack: TODO find a way to specify default values for different tags
        if (element instanceof HTMLCanvasElement) {
            return 300;
        }
        // Width not explicitly set.
        final String cssFloat = getCssFloat();
        if ("right".equals(cssFloat) || "left".equals(cssFloat) || ABSOLUTE.equals(getStyleAttribute(POSITION, true))) {
            // We're floating; simplistic approximation: text content * pixels per character.
            width = node.getVisibleText().length() * getBrowserVersion().getPixesPerChar();
        } else if (BLOCK.equals(display)) {
            final int windowWidth = element.getWindow().getWebWindow().getInnerWidth();
            if (element instanceof HTMLBodyElement) {
                width = windowWidth - 16;
            } else {
                // Block elements take up 100% of the parent's width.
                final HTMLElement parentJS = parent.getScriptableObject();
                width = pixelValue(parentJS, new CssValue(0, windowWidth) {

                    @Override
                    public String get(final ComputedCSSStyleDeclaration style) {
                        return style.getWidth();
                    }
                }) - (getBorderHorizontal() + getPaddingHorizontal());
            }
        } else if (node instanceof HtmlSubmitInput || node instanceof HtmlResetInput || node instanceof HtmlButtonInput || node instanceof HtmlButton || node instanceof HtmlFileInput) {
            // use asNormalizedText() here because getVisibleText() returns an empty string
            // for submit and reset buttons
            final String text = node.asNormalizedText();
            // default font for buttons is a bit smaller than the body font size
            width = 10 + (int) (text.length() * getBrowserVersion().getPixesPerChar() * 0.9);
        } else if (node instanceof HtmlTextInput || node instanceof HtmlPasswordInput) {
            final BrowserVersion browserVersion = getBrowserVersion();
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_143)) {
                return 143;
            }
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_173)) {
                return 173;
            }
            // FF
            width = 145;
        } else if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            final BrowserVersion browserVersion = getBrowserVersion();
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_RADIO_CHECKBOX_10)) {
                width = 10;
            } else {
                width = 13;
            }
        } else if (node instanceof HtmlTextArea) {
            // wild guess
            width = 100;
        } else if (node instanceof HtmlImage) {
            width = ((HtmlImage) node).getWidthOrDefault();
        } else {
            // Inline elements take up however much space is required by their children.
            width = getContentWidth();
        }
    } else if (AUTO.equals(styleWidth)) {
        width = element.getWindow().getWebWindow().getInnerWidth();
    } else {
        // Width explicitly set in the style attribute, or there was no parent to provide guidance.
        width = pixelValue(element, new CssValue(0, element.getWindow().getWebWindow().getInnerWidth()) {

            @Override
            public String get(final ComputedCSSStyleDeclaration style) {
                return style.getStyleAttribute(WIDTH, true);
            }
        });
    }
    width_ = Integer.valueOf(width);
    return width;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPasswordInput(com.gargoylesoftware.htmlunit.html.HtmlPasswordInput) AttributedString(java.text.AttributedString) HtmlResetInput(com.gargoylesoftware.htmlunit.html.HtmlResetInput) HtmlButtonInput(com.gargoylesoftware.htmlunit.html.HtmlButtonInput) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput) HtmlTextArea(com.gargoylesoftware.htmlunit.html.HtmlTextArea)

Example 7 with Element

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

the class ComputedCSSStyleDeclaration method getCalculatedHeight.

/**
 * Returns the element's calculated height, taking both relevant CSS and the element's children into account.
 * @return the element's calculated height, taking both relevant CSS and the element's children into account
 */
private int getCalculatedHeight() {
    if (height_ != null) {
        return height_.intValue();
    }
    final Element element = getElement();
    if (element instanceof HTMLImageElement) {
        height_ = ((HtmlImage) element.getDomNodeOrDie()).getHeightOrDefault();
        return height_;
    }
    final boolean isInline = "inline".equals(getDisplay()) && !(element instanceof HTMLIFrameElement);
    // height is ignored for inline elements
    if (isInline || super.getHeight().isEmpty()) {
        final int contentHeight = getContentHeight();
        if (contentHeight > 0) {
            height_ = Integer.valueOf(contentHeight);
            return height_;
        }
    }
    height_ = Integer.valueOf(getEmptyHeight());
    return height_;
}
Also used : HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)

Example 8 with Element

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

the class ComputedCSSStyleDeclaration method getTop.

/**
 * Returns the computed top (Y coordinate), relative to the node's parent's top edge.
 * @param includeMargin whether or not to take the margin into account in the calculation
 * @param includeBorder whether or not to take the border into account in the calculation
 * @param includePadding whether or not to take the padding into account in the calculation
 * @return the computed top (Y coordinate), relative to the node's parent's top edge
 */
public int getTop(final boolean includeMargin, final boolean includeBorder, final boolean includePadding) {
    int top = 0;
    if (null == top_) {
        final String p = getPositionWithInheritance();
        if (ABSOLUTE.equals(p)) {
            top = getTopForAbsolutePositionWithInheritance();
        } else {
            // Calculate the vertical displacement caused by *previous* siblings.
            DomNode prev = getElement().getDomNodeOrDie().getPreviousSibling();
            boolean prevHadComputedTop = false;
            while (prev != null && !prevHadComputedTop) {
                if (prev instanceof HtmlElement) {
                    final Element e = prev.getScriptableObject();
                    final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
                    // only previous block elements are counting
                    final String display = style.getDisplay();
                    if (isBlock(display)) {
                        int prevTop = 0;
                        if (style.top_ == null) {
                            final String prevPosition = style.getPositionWithInheritance();
                            if (ABSOLUTE.equals(prevPosition)) {
                                prevTop += style.getTopForAbsolutePositionWithInheritance();
                            } else {
                                if (RELATIVE.equals(prevPosition)) {
                                    final String t = style.getTopWithInheritance();
                                    prevTop += pixelValue(t);
                                }
                            }
                        } else {
                            prevHadComputedTop = true;
                            prevTop += style.top_;
                        }
                        prevTop += style.getCalculatedHeight(true, true);
                        final int margin = pixelValue(style.getMarginTop());
                        prevTop += margin;
                        top += prevTop;
                    }
                }
                prev = prev.getPreviousSibling();
            }
            // If the position is relative, we also need to add the specified "top" displacement.
            if (RELATIVE.equals(p)) {
                final String t = getTopWithInheritance();
                top += pixelValue(t);
            }
        }
        top_ = Integer.valueOf(top);
    } else {
        top = top_.intValue();
    }
    if (includeMargin) {
        final int margin = pixelValue(getMarginTop());
        top += margin;
    }
    if (includeBorder) {
        final int border = pixelValue(getBorderTopWidth());
        top += border;
    }
    if (includePadding) {
        final int padding = getPaddingTopValue();
        top += padding;
    }
    return top;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) AttributedString(java.text.AttributedString)

Example 9 with Element

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

the class Node method getLastElementChild.

/**
 * Returns the last element child.
 * @return the last element child
 */
protected Element getLastElementChild() {
    final DomNode domNode = getDomNodeOrDie();
    if (domNode instanceof DomElement) {
        final DomElement child = ((DomElement) getDomNodeOrDie()).getLastElementChild();
        if (child != null) {
            return child.getScriptableObject();
        }
        return null;
    }
    Element result = null;
    for (final DomNode child : domNode.getChildren()) {
        final Scriptable scriptable = child.getScriptableObject();
        if (scriptable instanceof Element) {
            result = (Element) scriptable;
        }
    }
    return result;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLHtmlElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLHtmlElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 10 with Element

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

the class ComputedCSSStyleDeclaration method getLeft.

/**
 * {@inheritDoc}
 */
@Override
public String getLeft() {
    final String superLeft = super.getLeft();
    if (!superLeft.endsWith("%")) {
        return defaultIfEmpty(superLeft, AUTO, null);
    }
    final Element elem = getElement();
    return pixelString(elem, new CssValue(0, 0) {

        @Override
        public String get(final ComputedCSSStyleDeclaration style) {
            if (style.getElement() == elem) {
                return style.getStyleAttribute(LEFT, true);
            }
            return style.getStyleAttribute(WIDTH, true);
        }
    });
}
Also used : HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) AttributedString(java.text.AttributedString)

Aggregations

Element (com.gargoylesoftware.htmlunit.javascript.host.Element)15 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)13 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)11 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)11 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)11 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)10 HTMLBodyElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement)10 HTMLDataElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement)10 HTMLDivElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement)10 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)10 HTMLIFrameElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement)10 HTMLImageElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement)10 HTMLLegendElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement)10 HTMLOutputElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement)10 HTMLSlotElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement)10 HTMLTimeElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement)10 HTMLUnknownElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)10 AttributedString (java.text.AttributedString)9 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2