Search in sources :

Example 1 with ClientRect

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

the class Range method getClientRects.

/**
 * Retrieves a collection of rectangles that describes the layout of the contents of an object
 * or range within the client. Each rectangle describes a single line.
 * @return a collection of rectangles that describes the layout of the contents
 */
@JsxFunction
public ClientRectList getClientRects() {
    final Window w = getWindow();
    final ClientRectList rectList = new ClientRectList();
    rectList.setParentScope(w);
    rectList.setPrototype(getPrototype(rectList.getClass()));
    // simple impl for now
    for (final DomNode node : toW3C().containedNodes()) {
        final ScriptableObject scriptable = node.getScriptableObject();
        if (scriptable instanceof HTMLElement) {
            final ClientRect rect = new ClientRect(0, 0, 1, 1);
            rect.setParentScope(w);
            rect.setPrototype(getPrototype(rect.getClass()));
            rectList.add(rect);
        }
    }
    return rectList;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) 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) ClientRectList(com.gargoylesoftware.htmlunit.javascript.host.ClientRectList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with ClientRect

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

the class HTMLElement method getBoundingClientRect.

/**
 * {@inheritDoc}
 */
@Override
public ClientRect getBoundingClientRect() {
    final ClientRect textRectangle = super.getBoundingClientRect();
    int left = getPosX();
    int top = getPosY();
    // account for any scrolled ancestors
    Object parentNode = getOffsetParentInternal(false);
    while ((parentNode instanceof HTMLElement) && !(parentNode instanceof HTMLBodyElement)) {
        final HTMLElement elem = (HTMLElement) parentNode;
        left -= elem.getScrollLeft();
        top -= elem.getScrollTop();
        parentNode = elem.getParentNode();
    }
    textRectangle.setBottom(top + getOffsetHeight());
    textRectangle.setLeft(left);
    textRectangle.setRight(left + getOffsetWidth());
    textRectangle.setTop(top);
    return textRectangle;
}
Also used : ClientRect(com.gargoylesoftware.htmlunit.javascript.host.ClientRect) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlBidirectionalOverride(com.gargoylesoftware.htmlunit.html.HtmlBidirectionalOverride)

Example 3 with ClientRect

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

Aggregations

ClientRect (com.gargoylesoftware.htmlunit.javascript.host.ClientRect)3 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)3 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)2 HtmlBidirectionalOverride (com.gargoylesoftware.htmlunit.html.HtmlBidirectionalOverride)1 ClientRectList (com.gargoylesoftware.htmlunit.javascript.host.ClientRectList)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1