Search in sources :

Example 1 with Text

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

the class ComputedCSSStyleDeclaration method getContentWidth.

/**
 * Returns the total width of the element's children.
 * @return the total width of the element's children
 */
public int getContentWidth() {
    int width = 0;
    final DomNode domNode = getDomNodeOrDie();
    Iterable<DomNode> children = domNode.getChildren();
    if (domNode instanceof BaseFrameElement) {
        final Page enclosedPage = ((BaseFrameElement) domNode).getEnclosedPage();
        if (enclosedPage != null && enclosedPage.isHtmlPage()) {
            children = ((DomNode) enclosedPage).getChildren();
        }
    }
    for (final DomNode child : children) {
        if (child.getScriptableObject() instanceof HTMLElement) {
            final HTMLElement e = child.getScriptableObject();
            final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
            final int w = style.getCalculatedWidth(true, true);
            width += w;
        } else if (child.getScriptableObject() instanceof Text) {
            final DomNode parent = child.getParentNode();
            if (parent instanceof HtmlElement) {
                final HTMLElement e = child.getParentNode().getScriptableObject();
                final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
                final int height = getBrowserVersion().getFontHeight(style.getFontSize());
                width += child.getVisibleText().length() * (int) (height / 1.8f);
            } else {
                width += child.getVisibleText().length() * getBrowserVersion().getPixesPerChar();
            }
        }
    }
    return width;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Page(com.gargoylesoftware.htmlunit.Page) Text(com.gargoylesoftware.htmlunit.javascript.host.dom.Text)

Example 2 with Text

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

the class ComputedCSSStyleDeclaration method getLeft.

/**
 * Returns the computed left (X coordinate), relative to the node's parent's left 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 left (X coordinate), relative to the node's parent's left edge
 */
public int getLeft(final boolean includeMargin, final boolean includeBorder, final boolean includePadding) {
    final String p = getPositionWithInheritance();
    final String l = getLeftWithInheritance();
    final String r = getRightWithInheritance();
    int left;
    if (ABSOLUTE.equals(p) && !AUTO.equals(l)) {
        // No need to calculate displacement caused by sibling nodes.
        left = pixelValue(l);
    } else if (ABSOLUTE.equals(p) && !AUTO.equals(r)) {
        // Need to calculate the horizontal displacement caused by *all* siblings.
        final HTMLElement parent = (HTMLElement) getElement().getParentElement();
        final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null);
        final int parentWidth = style.getCalculatedWidth(false, false);
        left = parentWidth - pixelValue(r);
    } else if (FIXED.equals(p) && !AUTO.equals(r)) {
        final HTMLElement parent = (HTMLElement) getElement().getParentElement();
        final ComputedCSSStyleDeclaration style = getWindow().getComputedStyle(getElement(), null);
        final ComputedCSSStyleDeclaration parentStyle = parent.getWindow().getComputedStyle(parent, null);
        left = pixelValue(parentStyle.getWidth()) - pixelValue(style.getWidth()) - pixelValue(r);
    } else if (FIXED.equals(p) && AUTO.equals(l)) {
        // Fixed to the location at which the browser puts it via normal element flowing.
        final HTMLElement parent = (HTMLElement) getElement().getParentElement();
        final ComputedCSSStyleDeclaration style = parent.getWindow().getComputedStyle(parent, null);
        left = pixelValue(style.getLeftWithInheritance());
    } else if (STATIC.equals(p)) {
        // We need to calculate the horizontal displacement caused by *previous* siblings.
        left = 0;
        DomNode prev = getElement().getDomNodeOrDie().getPreviousSibling();
        while (prev != null) {
            final Scriptable prevScriptable = prev.getScriptableObject();
            if (prevScriptable instanceof HTMLElement) {
                final HTMLElement e = (HTMLElement) prevScriptable;
                final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
                final String d = style.getDisplay();
                if (isBlock(d)) {
                    break;
                } else if (!NONE.equals(d)) {
                    left += style.getCalculatedWidth(true, true);
                }
            } else if (prevScriptable instanceof Text) {
                final String content = prev.getVisibleText();
                if (content != null) {
                    left += content.trim().length() * getBrowserVersion().getPixesPerChar();
                }
            }
            prev = prev.getPreviousSibling();
        }
    } else {
        // Just use the CSS specified value.
        left = pixelValue(l);
    }
    if (includeMargin) {
        final int margin = getMarginLeftValue();
        left += margin;
    }
    if (includeBorder) {
        final int border = pixelValue(getBorderLeftWidth());
        left += border;
    }
    if (includePadding) {
        final int padding = getPaddingLeftValue();
        left += padding;
    }
    return left;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) Text(com.gargoylesoftware.htmlunit.javascript.host.dom.Text) AttributedString(java.text.AttributedString) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 Text (com.gargoylesoftware.htmlunit.javascript.host.dom.Text)2 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)2 Page (com.gargoylesoftware.htmlunit.Page)1 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 AttributedString (java.text.AttributedString)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1