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);
}
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 });
}
});
}
}
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;
}
Aggregations