Search in sources :

Example 1 with MouseEvent

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

the class HtmlImage method fireEvent.

/**
 * {@inheritDoc}
 */
@Override
public ScriptResult fireEvent(final Event event) {
    if (event instanceof MouseEvent) {
        final MouseEvent mouseEvent = (MouseEvent) event;
        final HTMLElement scriptableObject = (HTMLElement) getScriptableObject();
        if (lastClickX_ >= 0) {
            mouseEvent.setClientX(scriptableObject.getPosX() + lastClickX_);
        }
        if (lastClickY_ >= 0) {
            mouseEvent.setClientY(scriptableObject.getPosX() + lastClickY_);
        }
    }
    return super.fireEvent(event);
}
Also used : MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)

Example 2 with MouseEvent

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

the class NamedAttrNodeMapImpl method click.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Simulates clicking on this element, returning the page in the window that has the focus
 * after the element has been clicked. Note that the returned page may or may not be the same
 * as the original page, depending on the type of element being clicked, the presence of JavaScript
 * action listeners, etc.
 *
 * @param shiftKey {@code true} if SHIFT is pressed during the click
 * @param ctrlKey {@code true} if CTRL is pressed during the click
 * @param altKey {@code true} if ALT is pressed during the click
 * @param triggerMouseEvents if true trigger the mouse events also
 * @param handleFocus if true set the focus (and trigger the event)
 * @param ignoreVisibility whether to ignore visibility or not
 * @param disableProcessLabelAfterBubbling ignore label processing
 * @param <P> the page type
 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
 * @exception IOException if an IO error occurs
 */
@SuppressWarnings("unchecked")
public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean triggerMouseEvents, final boolean handleFocus, final boolean ignoreVisibility, final boolean disableProcessLabelAfterBubbling) throws IOException {
    // make enclosing window the current one
    final SgmlPage page = getPage();
    page.getWebClient().setCurrentWindow(page.getEnclosingWindow());
    if (!ignoreVisibility) {
        if (!(page instanceof HtmlPage)) {
            return (P) page;
        }
        if (!isDisplayed()) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Calling click() ignored because the target element '" + this + "' is not displayed.");
            }
            return (P) page;
        }
        if (isDisabledElementAndDisabled()) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Calling click() ignored because the target element '" + this + "' is disabled.");
            }
            return (P) page;
        }
    }
    synchronized (page) {
        if (triggerMouseEvents) {
            mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
        }
        if (handleFocus) {
            // give focus to current element (if possible) or only remove it from previous one
            DomElement elementToFocus = null;
            if (this instanceof SubmittableElement || this instanceof HtmlAnchor && ATTRIBUTE_NOT_DEFINED != ((HtmlAnchor) this).getHrefAttribute() || this instanceof HtmlArea && (ATTRIBUTE_NOT_DEFINED != ((HtmlArea) this).getHrefAttribute() || getPage().getWebClient().getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) || this instanceof HtmlElement && ((HtmlElement) this).getTabIndex() != null) {
                elementToFocus = this;
            } else if (this instanceof HtmlOption) {
                elementToFocus = ((HtmlOption) this).getEnclosingSelect();
            }
            if (elementToFocus == null) {
                ((HtmlPage) page).setFocusedElement(null);
            } else {
                elementToFocus.focus();
            }
        }
        if (triggerMouseEvents) {
            mouseUp(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
        }
        MouseEvent event = null;
        if (page.getWebClient().isJavaScriptEnabled()) {
            final BrowserVersion browser = page.getWebClient().getBrowserVersion();
            if (browser.hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
                if (browser.hasFeature(EVENT_ONCLICK_POINTEREVENT_DETAIL_0)) {
                    event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
                } else {
                    event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 1);
                }
            } else {
                event = new MouseEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
            }
            if (disableProcessLabelAfterBubbling) {
                event.disableProcessLabelAfterBubbling();
            }
        }
        return click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
    }
}
Also used : PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 3 with MouseEvent

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

the class NamedAttrNodeMapImpl method dblClick.

/**
 * Simulates double-clicking on this element, returning the page in the window that has the focus
 * after the element has been clicked. Note that the returned page may or may not be the same
 * as the original page, depending on the type of element being clicked, the presence of JavaScript
 * action listeners, etc. Note also that {@link #click(boolean, boolean, boolean)} is automatically
 * called first.
 *
 * @param shiftKey {@code true} if SHIFT is pressed during the double-click
 * @param ctrlKey {@code true} if CTRL is pressed during the double-click
 * @param altKey {@code true} if ALT is pressed during the double-click
 * @param <P> the page type
 * @return the page that occupies this element's window after the element has been double-clicked
 * @exception IOException if an IO error occurs
 */
@SuppressWarnings("unchecked")
public <P extends Page> P dblClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey) throws IOException {
    if (isDisabledElementAndDisabled()) {
        return (P) getPage();
    }
    // call click event first
    P clickPage = click(shiftKey, ctrlKey, altKey);
    if (clickPage != getPage()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("dblClick() is ignored, as click() loaded a different page.");
        }
        return clickPage;
    }
    // call click event a second time
    clickPage = click(shiftKey, ctrlKey, altKey);
    if (clickPage != getPage()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("dblClick() is ignored, as click() loaded a different page.");
        }
        return clickPage;
    }
    final Event event;
    final WebClient webClient = getPage().getWebClient();
    if (webClient.getBrowserVersion().hasFeature(EVENT_ONDOUBLECLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
    } else {
        event = new MouseEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
    }
    final ScriptResult scriptResult = fireEvent(event);
    if (scriptResult == null) {
        return clickPage;
    }
    return (P) webClient.getCurrentWindow().getEnclosedPage();
}
Also used : PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 4 with MouseEvent

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

the class NamedAttrNodeMapImpl method doMouseEvent.

/**
 * Simulates the specified mouse event, returning the page which this element's window contains after the event.
 * The returned page may or may not be the same as the original page, depending on JavaScript event handlers, etc.
 *
 * @param eventType the mouse event type to simulate
 * @param shiftKey {@code true} if SHIFT is pressed during the mouse event
 * @param ctrlKey {@code true} if CTRL is pressed during the mouse event
 * @param altKey {@code true} if ALT is pressed during the mouse event
 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
 *        or {@link MouseEvent#BUTTON_RIGHT}
 * @return the page which this element's window contains after the event
 */
private Page doMouseEvent(final String eventType, final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
    final SgmlPage page = getPage();
    if (!page.getWebClient().isJavaScriptEnabled()) {
        return page;
    }
    final ScriptResult scriptResult;
    final Event event;
    if (MouseEvent.TYPE_CONTEXT_MENU.equals(eventType) && getPage().getWebClient().getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 0);
    } else {
        event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
    }
    scriptResult = fireEvent(event);
    final Page currentPage;
    if (scriptResult == null) {
        currentPage = page;
    } else {
        currentPage = page.getWebClient().getCurrentWindow().getEnclosedPage();
    }
    final boolean mouseOver = !MouseEvent.TYPE_MOUSE_OUT.equals(eventType);
    if (mouseOver_ != mouseOver) {
        mouseOver_ = mouseOver;
        page.clearComputedStyles();
    }
    return currentPage;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) Page(com.gargoylesoftware.htmlunit.Page) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 5 with MouseEvent

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

the class HTMLTableCellElement method getOffsetHeight.

/**
 * {@inheritDoc}
 */
@Override
public int getOffsetHeight() {
    final MouseEvent event = MouseEvent.getCurrentMouseEvent();
    if (isAncestorOfEventTarget(event)) {
        return super.getOffsetHeight();
    }
    if (isDisplayNone()) {
        return 0;
    }
    final ComputedCSSStyleDeclaration style = getWindow().getComputedStyle(this, null);
    final boolean includeBorder = getBrowserVersion().hasFeature(JS_TABLE_CELL_OFFSET_INCLUDES_BORDER);
    return style.getCalculatedHeight(includeBorder, true);
}
Also used : MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) ComputedCSSStyleDeclaration(com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration)

Aggregations

MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)8 ComputedCSSStyleDeclaration (com.gargoylesoftware.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration)4 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)3 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)2 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlTableCell (com.gargoylesoftware.htmlunit.html.HtmlTableCell)1 HtmlTableRow (com.gargoylesoftware.htmlunit.html.HtmlTableRow)1 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)1