Search in sources :

Example 1 with ScriptableObject

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

the class ScriptableWrapper method get.

/**
 * {@inheritDoc}
 * @see ScriptableObject#get(java.lang.String,net.sourceforge.htmlunit.corejs.javascript.Scriptable)
 */
@Override
public Object get(final String name, final Scriptable start) {
    final Method propertyGetter = properties_.get(name);
    final Object response;
    if (propertyGetter != null) {
        response = invoke(propertyGetter);
    } else {
        final Object fromSuper = super.get(name, start);
        if (fromSuper != Scriptable.NOT_FOUND) {
            response = fromSuper;
        } else {
            final Object byName = invoke(getByNameFallback_, new Object[] { name });
            if (byName != null) {
                response = byName;
            } else {
                response = Scriptable.NOT_FOUND;
            }
        }
    }
    return Context.javaToJS(response, ScriptableObject.getTopLevelScope(start));
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) Method(java.lang.reflect.Method)

Example 2 with ScriptableObject

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

the class DebugFrameImpl method getFunctionName.

/**
 * Returns the name of the function corresponding to this frame, if it is a function and it has
 * a name. If the function does not have a name, this method will try to return the name under
 * which it was referenced. See <a
 * href="http://www.digital-web.com/articles/scope_in_javascript/">this page</a> for a good
 * explanation of how the <tt>thisObj</tt> plays into this guess.
 *
 * @param thisObj the object via which the function was referenced, used to try to guess a
 *        function name if the function is anonymous
 * @return the name of the function corresponding to this frame
 */
private String getFunctionName(final Scriptable thisObj) {
    if (functionOrScript_.isFunction()) {
        final String name = functionOrScript_.getFunctionName();
        if (name != null && !name.isEmpty()) {
            // A named function -- we can just return the name.
            return name;
        }
        // on our HtmlUnitScriptable we need to avoid looking at the properties we have defined => TODO: improve it
        if (thisObj instanceof HtmlUnitScriptable) {
            return "[anonymous]";
        }
        Scriptable obj = thisObj;
        while (obj != null) {
            for (final Object id : obj.getIds()) {
                if (id instanceof String) {
                    final String s = (String) id;
                    if (obj instanceof ScriptableObject) {
                        Object o = ((ScriptableObject) obj).getGetterOrSetter(s, 0, thisObj, false);
                        if (o == null) {
                            o = ((ScriptableObject) obj).getGetterOrSetter(s, 0, thisObj, true);
                            if (o != null) {
                                return "__defineSetter__ " + s;
                            }
                        } else {
                            return "__defineGetter__ " + s;
                        }
                    }
                    final Object o;
                    try {
                        // within a try block as this sometimes throws (not sure why)
                        o = obj.get(s, obj);
                    } catch (final Exception e) {
                        return "[anonymous]";
                    }
                    if (o instanceof NativeFunction) {
                        final NativeFunction f = (NativeFunction) o;
                        if (f.getDebuggableView() == functionOrScript_) {
                            return s;
                        }
                    }
                }
            }
            obj = obj.getPrototype();
        }
        // Unable to intuit a name -- doh!
        return "[anonymous]";
    }
    // Just a script -- no function name.
    return "[script]";
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) IdFunctionObject(net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 3 with ScriptableObject

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

the class Range method getClientRects.

/**
 * Retrieves a collection of rectangles that describes the layout of the contents of an object
 * or range within the client. Each rectangle describes a single line.
 * @return a collection of rectangles that describes the layout of the contents
 */
@JsxFunction
public ClientRectList getClientRects() {
    final Window w = getWindow();
    final ClientRectList rectList = new ClientRectList();
    rectList.setParentScope(w);
    rectList.setPrototype(getPrototype(rectList.getClass()));
    // simple impl for now
    for (final DomNode node : toW3C().containedNodes()) {
        final ScriptableObject scriptable = node.getScriptableObject();
        if (scriptable instanceof HTMLElement) {
            final ClientRect rect = new ClientRect(0, 0, 1, 1);
            rect.setParentScope(w);
            rect.setPrototype(getPrototype(rect.getClass()));
            rectList.add(rect);
        }
    }
    return rectList;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) ClientRect(com.gargoylesoftware.htmlunit.javascript.host.ClientRect) ClientRectList(com.gargoylesoftware.htmlunit.javascript.host.ClientRectList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with ScriptableObject

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

the class MessageEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (getBrowserVersion().hasFeature(EVENT_ONMESSAGE_DEFAULT_DATA_NULL)) {
        data_ = null;
    }
    String origin = "";
    String lastEventId = "";
    if (details != null && !Undefined.isUndefined(details)) {
        data_ = details.get("data");
        final String detailOrigin = (String) details.get(HttpHeader.ORIGIN_LC);
        if (detailOrigin != null) {
            origin = detailOrigin;
        }
        final Object detailLastEventId = details.get("lastEventId");
        if (detailLastEventId != null) {
            lastEventId = Context.toString(detailLastEventId);
        }
        source_ = null;
        final Object detailSource = details.get("source");
        if (detailSource instanceof Window) {
            source_ = (Window) detailSource;
        } else if (detailSource instanceof WindowProxy) {
            source_ = ((WindowProxy) detailSource).getDelegee();
        }
        ports_ = details.get("ports");
    }
    origin_ = origin;
    lastEventId_ = lastEventId;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) WindowProxy(com.gargoylesoftware.htmlunit.javascript.host.WindowProxy) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 5 with ScriptableObject

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

the class MouseEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
@Override
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(ScriptRuntime.toString(type), details);
    if (details != null && !Undefined.isUndefined(details)) {
        final Object screenX = details.get("screenX", details);
        if (NOT_FOUND != screenX) {
            screenX_ = ScriptRuntime.toInt32(screenX);
        }
        final Object screenY = details.get("screenY", details);
        if (NOT_FOUND != screenX) {
            screenY_ = ScriptRuntime.toInt32(screenY);
        }
        final Object clientX = details.get("clientX", details);
        if (NOT_FOUND != clientX) {
            clientX_ = ScriptRuntime.toInt32(clientX);
        }
        final Object clientY = details.get("clientY", details);
        if (NOT_FOUND != clientX) {
            clientY_ = ScriptRuntime.toInt32(clientY);
        }
        final Object button = details.get("button", details);
        if (NOT_FOUND != button) {
            button_ = ScriptRuntime.toInt32(button);
        }
        final Object buttons = details.get("buttons", details);
        if (NOT_FOUND != buttons) {
            buttons_ = ScriptRuntime.toInt32(buttons);
        }
        setAltKey(ScriptRuntime.toBoolean(details.get("altKey")));
        setCtrlKey(ScriptRuntime.toBoolean(details.get("ctrlKey")));
        setMetaKey(ScriptRuntime.toBoolean(details.get("metaKey")));
        setShiftKey(ScriptRuntime.toBoolean(details.get("shiftKey")));
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)28 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)7 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)5 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)5 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)4 Context (net.sourceforge.htmlunit.corejs.javascript.Context)4 Test (org.junit.Test)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)3 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)3 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)3 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)3 Method (java.lang.reflect.Method)3 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)3 IdFunctionObject (net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject)3 JavaScriptException (net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)3 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)3 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2