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;
}
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;
}
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);
}
}
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;
}
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();
}
Aggregations