Search in sources :

Example 1 with NativeObject

use of net.sourceforge.htmlunit.corejs.javascript.NativeObject in project htmlunit by HtmlUnit.

the class EventListenersContainer method executeEventListeners.

private void executeEventListeners(final int eventPhase, final Event event, final Object[] args) {
    final DomNode node = jsNode_.getDomNodeOrNull();
    // some event don't apply on all kind of nodes, for instance "blur"
    if (node != null && !node.handles(event)) {
        return;
    }
    final TypeContainer container = getTypeContainer(event.getType());
    final List<Scriptable> listeners = container.getListeners(eventPhase);
    if (!listeners.isEmpty()) {
        event.setCurrentTarget(jsNode_);
        final HtmlPage page;
        if (jsNode_ instanceof Window) {
            page = (HtmlPage) jsNode_.getDomNodeOrDie();
        } else {
            final Scriptable parentScope = jsNode_.getParentScope();
            if (parentScope instanceof Window) {
                page = (HtmlPage) ((Window) parentScope).getDomNodeOrDie();
            } else if (parentScope instanceof HTMLDocument) {
                page = ((HTMLDocument) parentScope).getPage();
            } else {
                page = ((HTMLElement) parentScope).getDomNodeOrDie().getHtmlPageOrNull();
            }
        }
        // no need for a copy, listeners are copy on write
        for (Scriptable listener : listeners) {
            boolean isPropertyHandler = false;
            if (listener == TypeContainer.EVENT_HANDLER_PLACEHOLDER) {
                listener = container.handler_;
                isPropertyHandler = true;
            }
            Function function = null;
            Scriptable thisObject = null;
            if (listener instanceof Function) {
                function = (Function) listener;
                thisObject = jsNode_;
            } else if (listener instanceof NativeObject) {
                final Object handleEvent = ScriptableObject.getProperty(listener, "handleEvent");
                if (handleEvent instanceof Function) {
                    function = (Function) handleEvent;
                    thisObject = listener;
                }
            }
            if (function != null) {
                final ScriptResult result = page.executeJavaScriptFunction(function, thisObject, args, node);
                // Return value is only honored for property handlers (Tested in Chrome/FF/IE11)
                if (isPropertyHandler && !ScriptResult.isUndefined(result)) {
                    event.handlePropertyHandlerReturnValue(result.getJavaScriptResult());
                }
            }
            if (event.isImmediatePropagationStopped()) {
                return;
            }
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) Function(net.sourceforge.htmlunit.corejs.javascript.Function) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 2 with NativeObject

use of net.sourceforge.htmlunit.corejs.javascript.NativeObject in project htmlunit by HtmlUnit.

the class PointerEvent method jsConstructor.

/**
 * JavaScript constructor.
 * @param cx the current context
 * @param args the arguments to the WebSocket constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    final PointerEvent event = new PointerEvent();
    if (args.length != 0) {
        event.setType(Context.toString(args[0]));
        event.setBubbles(false);
        event.setCancelable(false);
        event.width_ = 1;
        event.height_ = 1;
    }
    if (args.length > 1) {
        final NativeObject object = (NativeObject) args[1];
        event.setBubbles((boolean) getValue(object, "bubbles", event.isBubbles()));
        event.pointerId_ = (int) getValue(object, "pointerId", event.pointerId_);
        event.width_ = (int) getValue(object, "width", event.width_);
        event.height_ = (int) getValue(object, "height", event.height_);
        event.pressure_ = (double) getValue(object, "pressure", event.pressure_);
        event.tiltX_ = (int) getValue(object, "tiltX", event.tiltX_);
        event.tiltY_ = (int) getValue(object, "tiltY", event.tiltY_);
        event.pointerType_ = (String) getValue(object, "pointerType", event.pointerType_);
        event.isPrimary_ = (boolean) getValue(object, "isPrimary", event.isPrimary_);
    }
    return event;
}
Also used : NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)2 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1 Function (net.sourceforge.htmlunit.corejs.javascript.Function)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1