Search in sources :

Example 1 with BeforeUnloadEvent

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

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

the class HtmlPage method executeEventHandlersIfNeeded.

/**
 * Looks for and executes any appropriate event handlers. Looks for body and frame tags.
 * @param eventType either {@link Event#TYPE_LOAD}, {@link Event#TYPE_UNLOAD}, or {@link Event#TYPE_BEFORE_UNLOAD}
 * @return {@code true} if user accepted <tt>onbeforeunload</tt> (not relevant to other events)
 */
private boolean executeEventHandlersIfNeeded(final String eventType) {
    // If JavaScript isn't enabled, there's nothing for us to do.
    if (!getWebClient().isJavaScriptEnabled()) {
        return true;
    }
    // Execute the specified event on the document element.
    final WebWindow window = getEnclosingWindow();
    if (window.getScriptableObject() instanceof Window) {
        final Event event;
        if (eventType.equals(Event.TYPE_BEFORE_UNLOAD)) {
            event = new BeforeUnloadEvent(this, eventType);
        } else {
            event = new Event(this, eventType);
        }
        // here so it could be used with HtmlPage.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Firing " + event);
        }
        final EventTarget jsNode;
        if (Event.TYPE_DOM_DOCUMENT_LOADED.equals(eventType)) {
            jsNode = this.getScriptableObject();
        } else {
            // The load/beforeunload/unload events target Document but paths Window only (tested in Chrome/FF)
            jsNode = window.getScriptableObject();
        }
        final HtmlUnitContextFactory cf = ((JavaScriptEngine) getWebClient().getJavaScriptEngine()).getContextFactory();
        cf.callSecured(cx -> jsNode.fireEvent(event), this);
        if (!isOnbeforeunloadAccepted(this, event)) {
            return false;
        }
    }
    // If this page was loaded in a frame, execute the version of the event specified on the frame tag.
    if (window instanceof FrameWindow) {
        final FrameWindow fw = (FrameWindow) window;
        final BaseFrameElement frame = fw.getFrameElement();
        // if part of a document fragment, then the load event is not triggered
        if (Event.TYPE_LOAD.equals(eventType) && frame.getParentNode() instanceof DomDocumentFragment) {
            return true;
        }
        if (frame.hasEventHandlers("on" + eventType)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Executing on" + eventType + " handler for " + frame);
            }
            if (window.getScriptableObject() instanceof Window) {
                final Event event;
                if (Event.TYPE_BEFORE_UNLOAD.equals(eventType)) {
                    event = new BeforeUnloadEvent(frame, eventType);
                } else {
                    // ff does not trigger the onload event in this case
                    if (PageDenied.BY_CONTENT_SECURIRY_POLICY == fw.getPageDenied() && hasFeature(JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY)) {
                        return true;
                    }
                    event = new Event(frame, eventType);
                }
                // This fires the "load" event for the <frame> element which, like all non-window
                // load events, propagates up to Document but not Window.  The "load" event for
                // <frameset> on the other hand, like that of <body>, is handled above where it is
                // fired against Document and directed to Window.
                frame.fireEvent(event);
                if (!isOnbeforeunloadAccepted((HtmlPage) frame.getPage(), event)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) AbstractJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

BeforeUnloadEvent (com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent)2 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)2 TopLevelWindow (com.gargoylesoftware.htmlunit.TopLevelWindow)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)1 AbstractJavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine)1 HtmlUnitContextFactory (com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory)1 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)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 EventTarget (com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget)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 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)1