Search in sources :

Example 91 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class Console method assertMethod.

/**
 * This method writes an error message to the console if the
 * assertion is false. If the assertion is true, nothing happens.
 * @param cx the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param funObj the function
 */
@JsxFunction(functionName = "assert")
public static void assertMethod(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
    if (args.length < 1 || ScriptRuntime.toBoolean(args[0])) {
        return;
    }
    if (args.length == 1) {
        log(cx, thisObj, new String[] { "Assertion failed" }, null);
        return;
    }
    final Object[] data;
    final Object first = args[1];
    if (first instanceof CharSequence || first instanceof ScriptableObject && ("String".equals(((Scriptable) first).getClassName()))) {
        data = new Object[args.length - 1];
        data[0] = "Assertion failed: " + first;
        System.arraycopy(args, 2, data, 1, data.length - 1);
    } else {
        data = new Object[args.length];
        data[0] = "Assertion failed:";
        System.arraycopy(args, 1, data, 1, data.length - 1);
    }
    log(cx, thisObj, data, null);
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 92 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class ApplicationCache method dispatchEvent.

/**
 * Dispatches an event into the event system (standards-conformant browsers only). See
 * <a href="https://developer.mozilla.org/en-US/docs/DOM/element.dispatchEvent">the Gecko
 * DOM reference</a> for more information.
 *
 * @param event the event to be dispatched
 * @return {@code false} if at least one of the event handlers which handled the event
 *         called <tt>preventDefault</tt>; {@code true} otherwise
 */
@Override
@JsxFunction
public boolean dispatchEvent(final Event event) {
    event.setTarget(this);
    final ScriptResult result = fireEvent(event);
    return !event.isAborted(result);
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 93 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class URLSearchParams method append.

/**
 * The append() method of the URLSearchParams interface appends a specified
 * key/value pair as a new search parameter.
 *
 * @param name  The name of the parameter to append.
 * @param value The value of the parameter to append.
 */
@JsxFunction
public void append(final String name, final String value) {
    String search = url_.getSearch();
    final List<NameValuePair> pairs;
    if (search == null || search.isEmpty()) {
        pairs = new ArrayList<>(1);
    } else {
        search = UrlUtils.decode(search);
        pairs = splitQuery(search);
    }
    pairs.add(new NameValuePair(name, value));
    try {
        url_.setSearch(pairs);
    } catch (final MalformedURLException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) MalformedURLException(java.net.MalformedURLException) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 94 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class URLSearchParams method getAll.

/**
 * The getAll() method of the URLSearchParams interface returns all the values
 * associated with a given search parameter as an array.
 *
 * @param name The name of the parameter to return.
 * @return An array of USVStrings.
 */
@JsxFunction
public Scriptable getAll(final String name) {
    final List<NameValuePair> splitted = splitQuery();
    final List<String> result = new ArrayList<>(splitted.size());
    for (final NameValuePair param : splitted) {
        if (param.getName().equals(name)) {
            result.add(param.getValue());
        }
    }
    return Context.getCurrentContext().newArray(getWindow(this), result.toArray());
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) ArrayList(java.util.ArrayList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 95 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method scrollTo.

/**
 * Scrolls to the specified location on the page.
 * @param x the horizontal position to scroll to
 * @param y the vertical position to scroll to
 */
@JsxFunction
public void scrollTo(final int x, final int y) {
    final HTMLElement body = document_.getBody();
    if (body != null) {
        body.setScrollLeft(x);
        body.setScrollTop(y);
        final Event event = new Event(body, Event.TYPE_SCROLL);
        body.fireEvent(event);
    }
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)133 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)30 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)20 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)19 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)16 URL (java.net.URL)14 IOException (java.io.IOException)11 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)11 WebClient (com.gargoylesoftware.htmlunit.WebClient)10 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)10 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)10 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)10 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)8 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)8 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)8 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)8 Range (org.w3c.dom.ranges.Range)8 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)7