Search in sources :

Example 31 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement 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)

Example 32 with HTMLElement

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

the class ComputedCSSStyleDeclaration method getContentHeight.

/**
 * Returns the total height of the element's children.
 * @return the total height of the element's children
 */
public int getContentHeight() {
    // There are two different kinds of elements that might determine the content height:
    // - elements with position:static or position:relative (elements that flow and build on each other)
    // - elements with position:absolute (independent elements)
    final DomNode node = getElement().getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        return 0;
    }
    ComputedCSSStyleDeclaration lastFlowing = null;
    final Set<ComputedCSSStyleDeclaration> styles = new HashSet<>();
    for (final DomNode child : node.getChildren()) {
        if (child.mayBeDisplayed()) {
            final Object scriptObj = child.getScriptableObject();
            if (scriptObj instanceof HTMLElement) {
                final HTMLElement e = (HTMLElement) scriptObj;
                final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
                final String pos = style.getPositionWithInheritance();
                if (STATIC.equals(pos) || RELATIVE.equals(pos)) {
                    lastFlowing = style;
                } else if (ABSOLUTE.equals(pos)) {
                    styles.add(style);
                }
            }
        }
    }
    if (lastFlowing != null) {
        styles.add(lastFlowing);
    }
    int max = 0;
    for (final ComputedCSSStyleDeclaration style : styles) {
        final int h = style.getTop(true, false, false) + style.getCalculatedHeight(true, true);
        if (h > max) {
            max = h;
        }
    }
    return max;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) AttributedString(java.text.AttributedString) HashSet(java.util.HashSet)

Example 33 with HTMLElement

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

the class HTMLCollectionFrames method scrollByPages.

/**
 * Scrolls the window content down by the specified number of pages.
 * @param pages the number of pages to scroll down
 */
@JsxFunction({ FF, FF_ESR })
public void scrollByPages(final int pages) {
    final HTMLElement body = document_.getBody();
    if (body != null) {
        body.setScrollTop(body.getScrollTop() + (getInnerHeight() * pages));
        final Event event = new Event(body, Event.TYPE_SCROLL);
        body.fireEvent(event);
    }
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 34 with HTMLElement

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

the class Range method getBoundingClientRect.

/**
 * Returns an object that bounds the contents of the range.
 * this a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
 * @return an object the bounds the contents of the range
 */
@JsxFunction
public ClientRect getBoundingClientRect() {
    final ClientRect rect = new ClientRect();
    rect.setParentScope(getWindow());
    rect.setPrototype(getPrototype(rect.getClass()));
    // simple impl for now
    for (final DomNode node : toW3C().containedNodes()) {
        final ScriptableObject scriptable = node.getScriptableObject();
        if (scriptable instanceof HTMLElement) {
            final ClientRect childRect = ((HTMLElement) scriptable).getBoundingClientRect();
            rect.setTop(Math.min(rect.getTop(), childRect.getTop()));
            rect.setLeft(Math.min(rect.getLeft(), childRect.getLeft()));
            rect.setRight(Math.max(rect.getRight(), childRect.getRight()));
            rect.setBottom(Math.max(rect.getBottom(), childRect.getBottom()));
        }
    }
    return rect;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) ClientRect(com.gargoylesoftware.htmlunit.javascript.host.ClientRect) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 35 with HTMLElement

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

the class MouseEvent method getScreenX.

/**
 * The horizontal coordinate at which the event occurred relative to the origin of the screen
 * coordinate system. The value of this attribute is initialized lazily, in order to optimize
 * performance (it requires CSS parsing).
 *
 * @return the horizontal coordinate
 */
@JsxGetter
public int getScreenX() {
    if (screenX_ == null) {
        final HTMLElement target = (HTMLElement) getTarget();
        screenX_ = Integer.valueOf(target.getPosX() + 10);
    }
    return screenX_.intValue();
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Aggregations

HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)35 AttributedString (java.text.AttributedString)10 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)7 Test (org.junit.Test)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)5 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)5 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)4 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)4 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)4 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)4 HTMLUnknownElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)4 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)3 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)3 HTMLBodyElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement)3 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)3