Search in sources :

Example 66 with JsxFunction

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

the class AudioContext method decodeAudioData.

/**
 * The decodeAudioData() method of the BaseAudioContext Interface is used to asynchronously
 * decode audio file data contained in an ArrayBuffer. In this case the ArrayBuffer is
 * loaded from XMLHttpRequest and FileReader.
 * The decoded AudioBuffer is resampled to the AudioContext's sampling rate,
 * then passed to a callback or promise.
 * @param buffer An ArrayBuffer containing the audio data to be decoded, usually grabbed
 * from XMLHttpRequest, WindowOrWorkerGlobalScope.fetch() or FileReader
 * @param success A callback function to be invoked when the decoding successfully finishes.
 * The single argument to this callback is an AudioBuffer representing the decodedData
 * (the decoded PCM audio data). Usually you'll want to put the decoded data into
 * an AudioBufferSourceNode, from which it can be played and manipulated how you want.
 * @param error An optional error callback, to be invoked if an error occurs
 * when the audio data is being decoded.
 * @return the promise or null
 */
@JsxFunction
public Object decodeAudioData(final NativeArrayBuffer buffer, final Function success, final Function error) {
    final Window window = getWindow();
    final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
    final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
    if (error != null) {
        jsEngine.addPostponedAction(new PostponedAction(owningPage, "AudioContext.decodeAudioData") {

            @Override
            public void execute() throws Exception {
                jsEngine.callFunction(owningPage, error, getParentScope(), AudioContext.this, new Object[] {});
            }
        });
        return null;
    }
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
    return reject.call(Context.getCurrentContext(), this, ctor, new Object[] {});
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 67 with JsxFunction

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

the class HTMLOptionsCollection method add.

/**
 * Adds a new item to the option collection.
 *
 * <p><b><i>Implementation Note:</i></b> The specification for the JavaScript add() method
 * actually calls for the optional newIndex parameter to be an integer. However, the
 * newIndex parameter is specified as an Object here rather than an int because of the
 * way Rhino and HtmlUnit process optional parameters for the JavaScript method calls.
 * If the newIndex parameter were specified as an int, then the Undefined value for an
 * integer is specified as NaN (Not A Number, which is a Double value), but Rhino
 * translates this value into 0 (perhaps correctly?) when converting NaN into an int.
 * As a result, when the newIndex parameter is not specified, it is impossible to make
 * a distinction between a caller of the form add(someObject) and add (someObject, 0).
 * Since the behavior of these two call forms is different, the newIndex parameter is
 * specified as an Object. If the newIndex parameter is not specified by the actual
 * JavaScript code being run, then newIndex is of type net.sourceforge.htmlunit.corejs.javascript.Undefined.
 * If the newIndex parameter is specified, then it should be of type java.lang.Number and
 * can be converted into an integer value.</p>
 *
 * <p>This method will call the {@link #put(int, Scriptable, Object)} method for actually
 * adding the element to the collection.</p>
 *
 * <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms535921.aspx">the
 * Microsoft DHTML reference page for the JavaScript add() method of the options collection</a>,
 * the index parameter is specified as follows:
 * <p>
 * <i>Optional. Integer that specifies the index position in the collection where the element is
 * placed. If no value is given, the method places the element at the end of the collection.</i>
 *
 * @param newOptionObject the DomNode to insert in the collection
 * @param beforeOptionObject An optional parameter which specifies the index position in the
 * collection where the element is placed. If no value is given, the method places
 * the element at the end of the collection.
 *
 * @see #put(int, Scriptable, Object)
 */
@JsxFunction
public void add(final Object newOptionObject, final Object beforeOptionObject) {
    final HtmlOption htmlOption = (HtmlOption) ((HTMLOptionElement) newOptionObject).getDomNodeOrNull();
    HtmlOption beforeOption = null;
    // If newIndex was specified, then use it
    if (beforeOptionObject instanceof Number) {
        final int index = ((Integer) Context.jsToJava(beforeOptionObject, Integer.class)).intValue();
        if (index < 0 || index >= getLength()) {
            // Add a new option at the end.
            htmlSelect_.appendOption(htmlOption);
            return;
        }
        beforeOption = (HtmlOption) ((HTMLOptionElement) item(index)).getDomNodeOrDie();
    } else if (beforeOptionObject instanceof HTMLOptionElement) {
        beforeOption = (HtmlOption) ((HTMLOptionElement) beforeOptionObject).getDomNodeOrDie();
        if (beforeOption.getParentNode() != htmlSelect_) {
            throw new EvaluatorException("Unknown option.");
        }
    }
    if (null == beforeOption) {
        htmlSelect_.appendOption(htmlOption);
        return;
    }
    beforeOption.insertBefore(htmlOption);
}
Also used : EvaluatorException(net.sourceforge.htmlunit.corejs.javascript.EvaluatorException) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 68 with JsxFunction

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

the class RowContainer method deleteRow.

/**
 * Deletes the row at the specified index.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536408.aspx">MSDN Documentation</a>
 * @param rowIndex the zero-based index of the row to delete
 */
@JsxFunction
public void deleteRow(int rowIndex) {
    final HTMLCollection rows = (HTMLCollection) getRows();
    final int rowCount = rows.getLength();
    if (rowIndex == -1) {
        rowIndex = rowCount - 1;
    }
    final boolean rowIndexValid = rowIndex >= 0 && rowIndex < rowCount;
    if (rowIndexValid) {
        final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(rowIndex));
        row.getDomNodeOrDie().remove();
    }
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 69 with JsxFunction

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

the class HTMLDocument method getElementById.

/**
 * Returns the element with the specified ID, or {@code null} if that element could not be found.
 * @param id the ID to search for
 * @return the element, or {@code null} if it could not be found
 */
@JsxFunction
@Override
public Object getElementById(final String id) {
    implicitCloseIfNecessary();
    Object result = null;
    final DomElement domElement = getPage().getElementById(id);
    if (null == domElement) {
        // Just fall through - result is already set to null
        if (LOG.isDebugEnabled()) {
            LOG.debug("getElementById(" + id + "): no DOM node found with this id");
        }
    } else {
        final Object jsElement = getScriptableFor(domElement);
        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("getElementById(" + id + ") cannot return a result as there isn't a JavaScript object for the HTML element " + domElement.getClass().getName());
            }
        } else {
            result = jsElement;
        }
    }
    return result;
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 70 with JsxFunction

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

the class HTMLDocument method open.

/**
 * JavaScript function "open".
 *
 * See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for
 * a good description of the semantics of open(), write(), writeln() and close().
 *
 * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
 *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
 * @param name the name
 * @param features the features
 * @param replace whether to replace in the history list or no
 * @return a reference to the new document object.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536652.aspx">MSDN documentation</a>
 */
@JsxFunction
public Object open(final Object url, final Object name, final Object features, final Object replace) {
    // Any open() invocations are ignored during the parsing stage, because write() and
    // writeln() invocations will directly append content to the current insertion point.
    final HtmlPage page = getPage();
    if (page.isBeingParsed()) {
        LOG.warn("Ignoring call to open() during the parsing stage.");
        return null;
    }
    // We're not in the parsing stage; OK to continue.
    if (!writeInCurrentDocument_) {
        LOG.warn("Function open() called when document is already open.");
    }
    writeInCurrentDocument_ = false;
    final WebWindow ww = getWindow().getWebWindow();
    if (ww instanceof FrameWindow && (!Undefined.isUndefined(url) || getBrowserVersion().hasFeature(JS_DOCUMENT_OPEN_OVERWRITES_ABOUT_BLANK_LOCATION)) && UrlUtils.ABOUT_BLANK.equals(getPage().getUrl().toExternalForm())) {
        final URL enclosingUrl = ((FrameWindow) ww).getEnclosingPage().getUrl();
        getPage().getWebResponse().getWebRequest().setUrl(enclosingUrl);
    }
    return this;
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) 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