Search in sources :

Example 11 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class WebWindowImpl method setEnclosedPage.

/**
 * {@inheritDoc}
 */
@Override
public void setEnclosedPage(final Page page) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("setEnclosedPage: " + page);
    }
    if (page == enclosedPage_) {
        return;
    }
    destroyChildren();
    if (isJavaScriptInitializationNeeded(page)) {
        webClient_.initialize(this, page);
    }
    if (webClient_.isJavaScriptEngineEnabled()) {
        final Window window = getScriptableObject();
        window.initialize(page);
    }
    // has to be done after page initialization to make sure
    // the enclosedPage has a scriptable object
    enclosedPage_ = page;
    history_.addPage(page);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow)

Example 12 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class MutationObserver method attributeReplaced.

/**
 * {@inheritDoc}
 */
@Override
public void attributeReplaced(final HtmlAttributeChangeEvent event) {
    final HtmlElement target = event.getHtmlElement();
    if (subtree_ || target == node_.getDomNodeOrDie()) {
        final String attributeName = event.getName();
        if (attributeFilter_ == null || attributeFilter_.contains(attributeName)) {
            final MutationRecord mutationRecord = new MutationRecord();
            final Scriptable scope = getParentScope();
            mutationRecord.setParentScope(scope);
            mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
            mutationRecord.setAttributeName(attributeName);
            mutationRecord.setType("attributes");
            mutationRecord.setTarget(target.getScriptableObject());
            if (attributeOldValue_) {
                mutationRecord.setOldValue(event.getValue());
            }
            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.attributeReplaced") {

                @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) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) 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)

Example 13 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class HTMLStyleElement method getSheet.

/**
 * Gets the associated sheet.
 * @see <a href="http://www.xulplanet.com/references/objref/HTMLStyleElement.html">Mozilla doc</a>
 * @return the sheet
 */
@JsxGetter
public CSSStyleSheet getSheet() {
    if (sheet_ != null) {
        return sheet_;
    }
    final HtmlStyle style = (HtmlStyle) getDomNodeOrDie();
    final String css = style.getTextContent();
    final Window window = getWindow();
    final Cache cache = window.getWebWindow().getWebClient().getCache();
    final CSSStyleSheetImpl cached = cache.getCachedStyleSheet(css);
    final String uri = getDomNodeOrDie().getPage().getWebResponse().getWebRequest().getUrl().toExternalForm();
    if (cached != null) {
        sheet_ = new CSSStyleSheet(this, window, cached, uri);
    } else {
        sheet_ = new CSSStyleSheet(this, css, uri);
        cache.cache(css, sheet_.getWrappedSheet());
    }
    return sheet_;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlStyle(com.gargoylesoftware.htmlunit.html.HtmlStyle) CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) CSSStyleSheet(com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet) Cache(com.gargoylesoftware.htmlunit.Cache) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 14 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window 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 15 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class DateTimeFormat method jsConstructor.

/**
 * JavaScript constructor.
 * @param cx the current context
 * @param args the arguments to the WebSocket constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    final String[] locales;
    if (args.length != 0) {
        if (args[0] instanceof NativeArray) {
            final NativeArray array = (NativeArray) args[0];
            locales = new String[(int) array.getLength()];
            for (int i = 0; i < locales.length; i++) {
                locales[i] = Context.toString(array.get(i));
            }
        } else {
            locales = new String[] { Context.toString(args[0]) };
        }
    } else {
        locales = new String[] { "" };
    }
    final Window window = getWindow(ctorObj);
    final DateTimeFormat format = new DateTimeFormat(locales, window.getBrowserVersion());
    format.setParentScope(window);
    format.setPrototype(window.getPrototype(format.getClass()));
    return format;
}
Also used : NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

Window (com.gargoylesoftware.htmlunit.javascript.host.Window)33 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)13 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)9 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)7 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 FrameWindow (com.gargoylesoftware.htmlunit.html.FrameWindow)5 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)5 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)5 IOException (java.io.IOException)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)4 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)3 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)3 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)3 AjaxController (com.gargoylesoftware.htmlunit.AjaxController)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2