Search in sources :

Example 21 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method animateAnimationsFrames.

/**
 * Invokes all the animation callbacks registered for this window by
 * calling {@link #requestAnimationFrame(Object)} once.
 * @return the number of pending animation callbacks
 */
public int animateAnimationsFrames() {
    final List<AnimationFrame> animationFrames = new ArrayList<>(animationFrames_);
    animationFrames_.clear();
    final double now = System.nanoTime() / 1_000_000d;
    final Object[] args = { now };
    final WebWindow ww = getWindow().getWebWindow();
    final JavaScriptEngine jsEngine = (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
    for (final AnimationFrame animationFrame : animationFrames) {
        jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(), animationFrame.callback_, this, getParentScope(), args);
    }
    return animationFrames_.size();
}
Also used : ArrayList(java.util.ArrayList) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 22 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class XMLDocument method makeScriptableFor.

/**
 * {@inheritDoc}
 */
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
    final HtmlUnitScriptable scriptable;
    // TODO: cleanup, getScriptObject() should be used!!!
    if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) {
        if (domNode instanceof SvgElement) {
            final Class<? extends HtmlUnitScriptable> javaScriptClass = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine()).getJavaScriptClass(domNode.getClass());
            try {
                scriptable = javaScriptClass.newInstance();
            } catch (final Exception e) {
                throw Context.throwAsScriptRuntimeEx(e);
            }
        } else {
            scriptable = new Element();
        }
    } else if (domNode instanceof DomAttr) {
        scriptable = new Attr();
    } else {
        return super.makeScriptableFor(domNode);
    }
    scriptable.setPrototype(getPrototype(scriptable.getClass()));
    scriptable.setParentScope(getParentScope());
    scriptable.setDomNode(domNode);
    return scriptable;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) IOException(java.io.IOException) Attr(com.gargoylesoftware.htmlunit.javascript.host.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 23 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class XMLHttpRequest method send.

/**
 * Sends the specified content to the server in an HTTP request and receives the response.
 * @param content the body of the message being sent with the request
 */
@JsxFunction
public void send(final Object content) {
    if (webRequest_ == null) {
        return;
    }
    if (!async_ && timeout_ > 0) {
        Context.throwAsScriptRuntimeEx(new RuntimeException("Synchronous requests must not set a timeout."));
        return;
    }
    prepareRequestContent(content);
    if (timeout_ > 0) {
        webRequest_.setTimeout(timeout_);
    }
    final Window w = getWindow();
    final WebWindow ww = w.getWebWindow();
    final WebClient client = ww.getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend();
    } else {
        // Create and start a thread in which to execute the request.
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = new ContextAction<Object>() {

            @Override
            public Object run(final Context cx) {
                // KEY_STARTING_SCOPE maintains a stack of scopes
                @SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
                if (null == stack) {
                    stack = new ArrayDeque<>();
                    cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
                }
                stack.push(w);
                try {
                    doSend();
                } finally {
                    stack.pop();
                }
                return null;
            }

            @Override
            public String toString() {
                return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
            }
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
        }
        jobID_ = ww.getJobManager().addJob(job, page);
        if (getBrowserVersion().hasFeature(XHR_FIRE_STATE_OPENED_AGAIN_IN_ASYNC_MODE)) {
            // quite strange but IE seems to fire state loading twice
            // in async mode (at least with HTML of the unit tests)
            fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
        }
        if (!getBrowserVersion().hasFeature(XHR_LOAD_START_ASYNC)) {
            fireJavascriptEvent(Event.TYPE_LOAD_START);
        }
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Context(net.sourceforge.htmlunit.corejs.javascript.Context) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 24 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class NodeList method forEach.

/**
 * Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
 * @param callback function to execute for each element
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void forEach(final Object callback) {
    final List<DomNode> nodes = getElements();
    final WebClient client = getWindow().getWebWindow().getWebClient();
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final ContextAction<Object> contextAction = cx -> {
        final Function function = (Function) callback;
        final Scriptable scope = getParentScope();
        for (int i = 0; i < nodes.size(); i++) {
            function.call(cx, scope, NodeList.this, new Object[] { nodes.get(i).getScriptableObject(), i, NodeList.this });
        }
        return null;
    };
    cf.call(contextAction);
}
Also used : Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSymbol(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSymbol) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ES6Iterator(net.sourceforge.htmlunit.corejs.javascript.ES6Iterator) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) NativeArrayIterator(net.sourceforge.htmlunit.corejs.javascript.NativeArrayIterator) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 25 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class MutationObserver method characterDataChanged.

/**
 * {@inheritDoc}
 */
@Override
public void characterDataChanged(final CharacterDataChangeEvent event) {
    final ScriptableObject target = event.getCharacterData().getScriptableObject();
    if (subtree_ || target == node_) {
        final MutationRecord mutationRecord = new MutationRecord();
        final Scriptable scope = getParentScope();
        mutationRecord.setParentScope(scope);
        mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
        mutationRecord.setType("characterData");
        mutationRecord.setTarget(target);
        if (characterDataOldValue_) {
            mutationRecord.setOldValue(event.getOldValue());
        }
        final Window window = getWindow();
        final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
        jsEngine.addPostponedAction(new PostponedAction(owningPage, "MutationObserver.characterDataChanged") {

            @Override
            public void execute() throws Exception {
                final NativeArray array = new NativeArray(new Object[] { mutationRecord });
                ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array);
                jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] { array });
            }
        });
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)25 WebClient (com.gargoylesoftware.htmlunit.WebClient)12 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)12 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)9 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)9 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)9 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 IOException (java.io.IOException)8 Context (net.sourceforge.htmlunit.corejs.javascript.Context)8 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)7 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)6 EventTarget (com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget)6 List (java.util.List)6 Function (net.sourceforge.htmlunit.corejs.javascript.Function)6 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)5 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)5