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