Search in sources :

Example 1 with ProgressEvent

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

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

the class XMLHttpRequest method fireJavascriptEventIgnoreAbort.

private void fireJavascriptEventIgnoreAbort(final String eventName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Firing javascript XHR event: " + eventName);
    }
    final boolean isReadyStateChange = Event.TYPE_READY_STATE_CHANGE.equalsIgnoreCase(eventName);
    final Event event;
    if (isReadyStateChange) {
        event = new Event(this, Event.TYPE_READY_STATE_CHANGE);
    } else {
        final ProgressEvent progressEvent = new ProgressEvent(this, eventName);
        final boolean lengthComputable = getBrowserVersion().hasFeature(XHR_LENGTH_COMPUTABLE);
        if (lengthComputable) {
            progressEvent.setLengthComputable(true);
        }
        if (webResponse_ != null) {
            final long contentLength = webResponse_.getContentLength();
            progressEvent.setLoaded(contentLength);
            if (lengthComputable) {
                progressEvent.setTotal(contentLength);
            }
        }
        event = progressEvent;
    }
    executeEventLocally(event);
}
Also used : ProgressEvent(com.gargoylesoftware.htmlunit.javascript.host.event.ProgressEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) ProgressEvent(com.gargoylesoftware.htmlunit.javascript.host.event.ProgressEvent)

Aggregations

Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)2 ProgressEvent (com.gargoylesoftware.htmlunit.javascript.host.event.ProgressEvent)2 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 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)1 MouseWheelEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseWheelEvent)1 MutationEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MutationEvent)1 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)1 PopStateEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PopStateEvent)1 SVGZoomEvent (com.gargoylesoftware.htmlunit.javascript.host.event.SVGZoomEvent)1 TextEvent (com.gargoylesoftware.htmlunit.javascript.host.event.TextEvent)1