Search in sources :

Example 41 with JsxFunction

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

the class Element method closest.

@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public static Element closest(final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    if (!(thisObj instanceof Element)) {
        throw ScriptRuntime.typeError("Illegal invocation");
    }
    final String selectorString = (String) args[0];
    try {
        final DomNode domNode = ((Element) thisObj).getDomNodeOrNull();
        if (domNode == null) {
            return null;
        }
        final DomElement elem = domNode.closest(selectorString);
        if (elem == null) {
            return null;
        }
        return elem.getScriptableObject();
    } catch (final CSSException e) {
        throw ScriptRuntime.constructError("SyntaxError", "An invalid or illegal selector was specified (selector: '" + selectorString + "' error: " + e.getMessage() + ").");
    }
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLTemplateElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) CSSException(com.gargoylesoftware.css.parser.CSSException) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 42 with JsxFunction

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

the class FontFaceSet method load.

/**
 * @param font a font specification using the CSS value syntax, e.g. "italic bold 16px Roboto"
 * @param text limit the font faces to those whose Unicode range contains at least one
 *          of the characters in text. This does not check for individual glyph coverage.
 * @return a Promise of an Array of FontFace loaded. The promise is fulfilled
 *          when all the fonts are loaded; it is rejected if one of the fonts failed to load.
 */
@JsxFunction
public Object load(final String font, final String text) {
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
    return resolve.call(Context.getCurrentContext(), this, ctor, new Object[] { "" });
}
Also used : LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 43 with JsxFunction

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

the class URLSearchParams method set.

/**
 * The set() method of the URLSearchParams interface sets the value associated with a
 * given search parameter to the given value. If there were several matching values,
 * this method deletes the others. If the search parameter doesn't exist, this method
 * creates it.
 *
 * @param name  The name of the parameter to set.
 * @param value The value of the parameter to set.
 */
@JsxFunction
public void set(final String name, final String value) {
    final List<NameValuePair> splitted = splitQuery();
    boolean change = true;
    final ListIterator<NameValuePair> iter = splitted.listIterator();
    while (iter.hasNext()) {
        final NameValuePair entry = iter.next();
        if (entry.getName().equals(name)) {
            if (change) {
                iter.set(new NameValuePair(name, value));
                change = false;
            } else {
                iter.remove();
            }
        }
    }
    if (change) {
        splitted.add(new NameValuePair(name, value));
    }
    try {
        url_.setSearch(splitted);
    } 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 44 with JsxFunction

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

the class HTMLCollectionFrames method scrollBy.

/**
 * Scrolls the window content the specified distance.
 * @param x the horizontal distance to scroll by
 * @param y the vertical distance to scroll by
 */
@JsxFunction
public void scrollBy(final int x, final int y) {
    final HTMLElement body = document_.getBody();
    if (body != null) {
        body.setScrollLeft(body.getScrollLeft() + x);
        body.setScrollTop(body.getScrollTop() + 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)

Example 45 with JsxFunction

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

the class HTMLCollectionFrames method showModalDialog.

/**
 * Creates a modal dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return the value of the {@code returnValue} property as set by the modal dialog's window
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536759.aspx">MSDN Documentation</a>
 * @see <a href="https://developer.mozilla.org/en/DOM/window.showModalDialog">Mozilla Documentation</a>
 */
@JsxFunction(IE)
public Object showModalDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        // TODO: Theoretically, we shouldn't return until the dialog window has been close()'ed...
        // But we have to return so that the window can be close()'ed...
        // Maybe we can use Rhino's continuation support to save state and restart when
        // the dialog window is close()'ed? Would only work in interpreted mode, though.
        final ScriptableObject jsDialog = dialog.getScriptableObject();
        return jsDialog.get("returnValue", jsDialog);
    } catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) IOException(java.io.IOException) WebClient(com.gargoylesoftware.htmlunit.WebClient) DialogWindow(com.gargoylesoftware.htmlunit.DialogWindow) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) 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