Search in sources :

Example 11 with HTMLElement

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);
    }
}
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 12 with HTMLElement

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());
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) NodeList(org.w3c.dom.NodeList) Test(org.junit.Test)

Example 13 with HTMLElement

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);
                }
            }
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) XHtmlPage(com.gargoylesoftware.htmlunit.html.XHtmlPage) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame)

Example 14 with HTMLElement

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;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Page(com.gargoylesoftware.htmlunit.Page) Text(com.gargoylesoftware.htmlunit.javascript.host.dom.Text)

Example 15 with HTMLElement

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;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPasswordInput(com.gargoylesoftware.htmlunit.html.HtmlPasswordInput) AttributedString(java.text.AttributedString) HtmlResetInput(com.gargoylesoftware.htmlunit.html.HtmlResetInput) HtmlButtonInput(com.gargoylesoftware.htmlunit.html.HtmlButtonInput) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput) HtmlTextArea(com.gargoylesoftware.htmlunit.html.HtmlTextArea)

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