Search in sources :

Example 71 with JsxFunction

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

the class HTMLElement method clearAttributes.

/**
 * An IE-only method which clears all custom attributes.
 */
@JsxFunction(IE)
public void clearAttributes() {
    final HtmlElement node = getDomNodeOrDie();
    // Remove custom attributes defined directly in HTML.
    final List<String> removals = new ArrayList<>();
    for (final String attributeName : node.getAttributesMap().keySet()) {
        // May not be 100% correct.
        if (!ScriptableObject.hasProperty(getPrototype(), attributeName)) {
            removals.add(attributeName);
        }
    }
    for (final String attributeName : removals) {
        node.removeAttribute(attributeName);
    }
    // Remove custom attributes defined at runtime via JavaScript.
    for (final Object id : getAllIds()) {
        if (id instanceof Integer) {
            final int i = ((Integer) id).intValue();
            delete(i);
        } else if (id instanceof String) {
            delete((String) id);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 72 with JsxFunction

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

the class HTMLFormElement method requestSubmit.

/**
 * Submits the form by submitted using a specific submit button.
 * @param submitter The submit button whose attributes describe the method
 * by which the form is to be submitted. This may be either
 * an &lt;input&gt; or &lt;button&gt; element whose type attribute is submit.
 * If you omit the submitter parameter, the form element itself is used as the submitter.
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void requestSubmit(final Object submitter) {
    if (Undefined.isUndefined(submitter)) {
        submit();
        return;
    }
    SubmittableElement submittable = null;
    if (submitter instanceof HTMLElement) {
        final HTMLElement subHtmlElement = (HTMLElement) submitter;
        if (subHtmlElement instanceof HTMLButtonElement) {
            if ("submit".equals(((HTMLButtonElement) subHtmlElement).getType())) {
                submittable = (SubmittableElement) subHtmlElement.getDomNodeOrDie();
            }
        } else if (subHtmlElement instanceof HTMLInputElement) {
            if ("submit".equals(((HTMLInputElement) subHtmlElement).getType())) {
                submittable = (SubmittableElement) subHtmlElement.getDomNodeOrDie();
            }
        }
        if (submittable != null && subHtmlElement.getForm() != this) {
            throw ScriptRuntime.typeError("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + "The specified element is not owned by this form element.");
        }
    }
    if (submittable == null) {
        throw ScriptRuntime.typeError("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + "The specified element is not a submit button.");
    }
    this.getHtmlForm().submit(submittable);
}
Also used : SubmittableElement(com.gargoylesoftware.htmlunit.html.SubmittableElement) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 73 with JsxFunction

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

the class Geolocation method getCurrentPosition.

/**
 * Gets the current position.
 * @param successCallback success callback
 * @param errorCallback optional error callback
 * @param options optional options
 */
@JsxFunction
public void getCurrentPosition(final Function successCallback, final Object errorCallback, final Object options) {
    successHandler_ = successCallback;
    final WebWindow webWindow = getWindow().getWebWindow();
    if (webWindow.getWebClient().getOptions().isGeolocationEnabled()) {
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(0, null, () -> doGetPosition());
        webWindow.getJobManager().addJob(job, webWindow.getEnclosedPage());
    }
}
Also used : JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 74 with JsxFunction

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

the class HTMLCanvasElement method getContext.

/**
 * Gets the context.
 * @param contextId the context id
 * @return Returns an object that exposes an API for drawing on the canvas,
 * or null if the given context ID is not supported
 */
@JsxFunction
public Object getContext(final String contextId) {
    if ("2d".equals(contextId)) {
        if (context2d_ == null) {
            final CanvasRenderingContext2D context = new CanvasRenderingContext2D(this);
            context.setParentScope(getParentScope());
            context.setPrototype(getPrototype(context.getClass()));
            context2d_ = context;
        }
        return context2d_;
    }
    return null;
}
Also used : CanvasRenderingContext2D(com.gargoylesoftware.htmlunit.javascript.host.canvas.CanvasRenderingContext2D) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 75 with JsxFunction

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

the class HTMLSubCollection method namedItem.

/**
 * Retrieves the item or items corresponding to the specified name (checks ids, and if
 * that does not work, then names).
 * @param name the name or id the element or elements to return
 * @return the element or elements corresponding to the specified name or id
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536634.aspx">MSDN doc</a>
 */
@JsxFunction
public Object namedItem(final String name) {
    final List<DomNode> elements = getElements();
    final BrowserVersion browserVersion = getBrowserVersion();
    if (browserVersion.hasFeature(HTMLCOLLECTION_NAMED_ITEM_ID_FIRST)) {
        for (final Object next : elements) {
            if (next instanceof DomElement) {
                final DomElement elem = (DomElement) next;
                final String id = elem.getId();
                if (name.equals(id)) {
                    return getScriptableForElement(elem);
                }
            }
        }
    }
    for (final Object next : elements) {
        if (next instanceof DomElement) {
            final DomElement elem = (DomElement) next;
            final String nodeName = elem.getAttributeDirect("name");
            if (name.equals(nodeName)) {
                return getScriptableForElement(elem);
            }
            final String id = elem.getId();
            if (name.equals(id)) {
                return getScriptableForElement(elem);
            }
        }
    }
    return null;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) 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