Search in sources :

Example 1 with PointerEvent

use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent 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 2 with PointerEvent

use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent 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 3 with PointerEvent

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

the class Document method createEvent.

/**
 * Implementation of the {@link org.w3c.dom.events.DocumentEvent} interface's
 * {@link org.w3c.dom.events.DocumentEvent#createEvent(String)} method. The method creates an
 * uninitialized event of the specified type.
 *
 * @see <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-DocumentEvent">DocumentEvent</a>
 * @param eventType the event type to create
 * @return an event object for the specified type
 * @throws DOMException if the event type is not supported (will have a type of
 *         DOMException.NOT_SUPPORTED_ERR)
 */
@JsxFunction
public Event createEvent(final String eventType) throws DOMException {
    Class<? extends Event> clazz = SUPPORTED_DOM2_EVENT_TYPE_MAP.get(eventType);
    if (clazz == null) {
        clazz = SUPPORTED_DOM3_EVENT_TYPE_MAP.get(eventType);
        if (CloseEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONCLOSE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
            clazz = null;
        }
    }
    if (clazz == null && ("Events".equals(eventType) || "HashChangeEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_HASHCHANGEEVENT) || "BeforeUnloadEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_BEFOREUNLOADEVENT) || "MouseWheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_MOUSEWHEELEVENT) || "PointerEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_POINTEREVENT) || "PopStateEvent".equals(eventType) || "ProgressEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_PROGRESSEVENT) || "FocusEvent".equals(eventType) || "WheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_WHEELEVENT))) {
        clazz = SUPPORTED_VENDOR_EVENT_TYPE_MAP.get(eventType);
        if (PopStateEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONPOPSTATE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
            clazz = null;
        }
    }
    if (clazz == null) {
        Context.throwAsScriptRuntimeEx(new DOMException(DOMException.NOT_SUPPORTED_ERR, "Event Type is not supported: " + eventType));
        // to stop eclipse warning
        return null;
    }
    try {
        final Event event = clazz.newInstance();
        event.setParentScope(getWindow());
        event.setPrototype(getPrototype(clazz));
        event.eventCreated();
        return event;
    } catch (final InstantiationException | IllegalAccessException e) {
        throw Context.reportRuntimeError("Failed to instantiate event: class ='" + clazz.getName() + "' for event type of '" + eventType + "': " + e.getMessage());
    }
}
Also used : PopStateEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PopStateEvent) CloseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CloseEvent) DOMException(org.w3c.dom.DOMException) CustomEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CustomEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) PopStateEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PopStateEvent) SVGZoomEvent(com.gargoylesoftware.htmlunit.javascript.host.event.SVGZoomEvent) ProgressEvent(com.gargoylesoftware.htmlunit.javascript.host.event.ProgressEvent) FocusEvent(com.gargoylesoftware.htmlunit.javascript.host.event.FocusEvent) CompositionEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CompositionEvent) WheelEvent(com.gargoylesoftware.htmlunit.javascript.host.event.WheelEvent) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) TextEvent(com.gargoylesoftware.htmlunit.javascript.host.event.TextEvent) CloseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CloseEvent) MouseWheelEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseWheelEvent) HashChangeEvent(com.gargoylesoftware.htmlunit.javascript.host.event.HashChangeEvent) MutationEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MutationEvent) UIEvent(com.gargoylesoftware.htmlunit.javascript.host.event.UIEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) DragEvent(com.gargoylesoftware.htmlunit.javascript.host.event.DragEvent) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with PointerEvent

use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent 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)

Aggregations

MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)4 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)3 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)2 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 BeforeUnloadEvent (com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent)1 CloseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.CloseEvent)1 CompositionEvent (com.gargoylesoftware.htmlunit.javascript.host.event.CompositionEvent)1 CustomEvent (com.gargoylesoftware.htmlunit.javascript.host.event.CustomEvent)1 DragEvent (com.gargoylesoftware.htmlunit.javascript.host.event.DragEvent)1 FocusEvent (com.gargoylesoftware.htmlunit.javascript.host.event.FocusEvent)1 HashChangeEvent (com.gargoylesoftware.htmlunit.javascript.host.event.HashChangeEvent)1 KeyboardEvent (com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent)1 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)1 MouseWheelEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseWheelEvent)1