use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method scrollBy.
/**
* Scrolls the window content the specified distance.
* @param x the horizontal distance to scroll by
* @param y the vertical distance to scroll by
*/
@JsxFunction
public void scrollBy(final int x, final int y) {
final HTMLElement body = document_.getBody();
if (body != null) {
body.setScrollLeft(body.getScrollLeft() + x);
body.setScrollTop(body.getScrollTop() + y);
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 CSSStyleSheet2Test method brokenExternalCSS.
/**
* Test for #1300.
* @throws Exception if the test fails
*/
@Test
public void brokenExternalCSS() throws Exception {
final String html = "<html><head>\n" + "<link rel='stylesheet' type='text/css' href='" + URL_SECOND + "'/></head></html>";
getMockWebConnection().setResponse(URL_SECOND, "body { font-weight: 900\\9; }");
final HtmlPage htmlPage = loadPage(html);
final NodeList list = htmlPage.getElementsByTagName("body");
final HtmlElement element = (HtmlElement) list.item(0);
final ComputedCSSStyleDeclaration style = ((HTMLElement) element.getScriptableObject()).getCurrentStyle();
assertEquals("CSSStyleDeclaration for ''", style.toString());
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class WebClient method setCurrentWindow.
/**
* Sets the "current" window for this client. This is the window that will be used when
* <tt>getPage(...)</tt> is called without specifying a window.
* @param window the new "current" window for this client
*/
public void setCurrentWindow(final WebWindow window) {
WebAssert.notNull("window", window);
if (currentWindow_ == window) {
return;
}
// onBlur event is triggered for focused element of old current window
if (currentWindow_ != null && !currentWindow_.isClosed()) {
final Page enclosedPage = currentWindow_.getEnclosedPage();
if (enclosedPage != null && enclosedPage.isHtmlPage()) {
final DomElement focusedElement = ((HtmlPage) enclosedPage).getFocusedElement();
if (focusedElement != null) {
focusedElement.fireEvent(Event.TYPE_BLUR);
}
}
}
currentWindow_ = window;
// when marking an iframe window as current we have no need to move the focus
final boolean isIFrame = currentWindow_ instanceof FrameWindow && ((FrameWindow) currentWindow_).getFrameElement() instanceof HtmlInlineFrame;
if (!isIFrame) {
// 1. activeElement becomes focused element for new current window
// 2. onFocus event is triggered for focusedElement of new current window
final Page enclosedPage = currentWindow_.getEnclosedPage();
if (enclosedPage != null && enclosedPage.isHtmlPage()) {
final Window jsWindow = currentWindow_.getScriptableObject();
if (jsWindow != null) {
final HTMLElement activeElement = ((HTMLDocument) jsWindow.getDocument()).getActiveElement();
if (activeElement != null) {
((HtmlPage) enclosedPage).setFocusedElement(activeElement.getDomNodeOrDie(), true);
}
}
}
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement 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;
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement 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;
}
Aggregations