Search in sources :

Example 31 with Window

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

the class Node method asJavaScriptException.

/**
 * Encapsulates the given {@link DOMException} into a Rhino-compatible exception.
 *
 * @param exception the exception to encapsulate
 * @return the created exception
 */
protected RhinoException asJavaScriptException(final DOMException exception) {
    final Window w = getWindow();
    exception.setPrototype(w.getPrototype(exception.getClass()));
    exception.setParentScope(w);
    // get current line and file name
    // this method can only be used in interpreted mode. If one day we choose to use compiled mode,
    // then we'll have to find an other way here.
    final String fileName;
    final int lineNumber;
    if (Context.getCurrentContext().getOptimizationLevel() == -1) {
        final int[] linep = new int[1];
        final String sourceName = new Interpreter().getSourcePositionFromStack(Context.getCurrentContext(), linep);
        fileName = sourceName.replaceFirst("script in (.*) from .*", "$1");
        lineNumber = linep[0];
    } else {
        throw new Error("HtmlUnit not ready to run in compiled mode");
    }
    exception.setLocation(fileName, lineNumber);
    return new JavaScriptException(exception, fileName, lineNumber);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Interpreter(net.sourceforge.htmlunit.corejs.javascript.Interpreter) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 32 with Window

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

the class MutationObserver method characterDataChanged.

/**
 * {@inheritDoc}
 */
@Override
public void characterDataChanged(final CharacterDataChangeEvent event) {
    final ScriptableObject target = event.getCharacterData().getScriptableObject();
    if (subtree_ || target == node_) {
        final MutationRecord mutationRecord = new MutationRecord();
        final Scriptable scope = getParentScope();
        mutationRecord.setParentScope(scope);
        mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
        mutationRecord.setType("characterData");
        mutationRecord.setTarget(target);
        if (characterDataOldValue_) {
            mutationRecord.setOldValue(event.getOldValue());
        }
        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.characterDataChanged") {

            @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) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) 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 33 with Window

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

the class DOMParser method parseFromString.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Parses the given Unicode string into a DOM document.
 * @param scriptable the ScriptableObject this belongs to
 * @param str the Unicode string to be parsed
 * @param type the MIME type of the string -
 *        <code>text/html</code>, <code>text/xml</code>, <code>application/xml</code>,
 *        <code>application/xhtml+xml</code>, <code>image/svg+xml</code>. Must not be {@code null}.
 * @return the generated document
 * @throws IOException in case of error
 */
public static Document parseFromString(final HtmlUnitScriptable scriptable, final String str, final Object type) throws IOException {
    if (type == null || Undefined.isUndefined(type)) {
        throw Context.reportRuntimeError("Missing 'type' parameter");
    }
    if (MimeType.TEXT_XML.equals(type) || MimeType.APPLICATION_XML.equals(type) || MimeType.APPLICATION_XHTML.equals(type) || "image/svg+xml".equals(type)) {
        final XMLDocument document = new XMLDocument();
        document.setParentScope(scriptable.getParentScope());
        document.setPrototype(scriptable.getPrototype(XMLDocument.class));
        document.loadXML(str);
        return document;
    }
    if (MimeType.TEXT_HTML.equals(type)) {
        final WebWindow webWindow = scriptable.getWindow().getWebWindow();
        final WebClient webClient = webWindow.getWebClient();
        final WebResponse webResponse = new StringWebResponse(str, webWindow.getEnclosedPage().getUrl());
        // a similar impl is in
        // com.gargoylesoftware.htmlunit.javascript.host.dom.DOMImplementation.createHTMLDocument(Object)
        final HtmlPage page = new HtmlPage(webResponse, webWindow);
        page.setEnclosingWindow(null);
        final Window window = webWindow.getScriptableObject();
        // document knows the window but is not the windows document
        final HTMLDocument document = new HTMLDocument();
        document.setParentScope(window);
        document.setPrototype(window.getPrototype(document.getClass()));
        // document.setWindow(window);
        document.setDomNode(page);
        final HTMLParser htmlParser = webClient.getPageCreator().getHtmlParser();
        htmlParser.parse(webResponse, page, false, true);
        return page.getScriptableObject();
    }
    return null;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) WebClient(com.gargoylesoftware.htmlunit.WebClient) XMLDocument(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

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