use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XPathEvaluator method evaluate.
/**
* Evaluates an XPath expression string and returns a result of the specified type if possible.
* @param expression the XPath expression string to be parsed and evaluated
* @param contextNodeObj the context node for the evaluation of this XPath expression
* @param resolver the resolver permits translation of all prefixes, including the XML namespace prefix,
* within the XPath expression into appropriate namespace URIs.
* @param type If a specific type is specified, then the result will be returned as the corresponding type
* @param result the result object which may be reused and returned by this method
* @return the result of the evaluation of the XPath expression
*/
@JsxFunction
public XPathResult evaluate(final String expression, final Object contextNodeObj, final Object resolver, final int type, final Object result) {
XPathResult xPathResult = (XPathResult) result;
if (xPathResult == null) {
xPathResult = new XPathResult();
xPathResult.setParentScope(getParentScope());
xPathResult.setPrototype(getPrototype(xPathResult.getClass()));
}
// contextNodeObj can be either a node or an array with the node as the first element.
if (!(contextNodeObj instanceof Node)) {
throw Context.reportRuntimeError("Illegal value for parameter 'context'");
}
final Node contextNode = (Node) contextNodeObj;
PrefixResolver prefixResolver = null;
if (resolver instanceof PrefixResolver) {
prefixResolver = (PrefixResolver) resolver;
} else if (resolver instanceof NativeFunction) {
prefixResolver = new NativeFunctionPrefixResolver((NativeFunction) resolver, contextNode.getParentScope());
}
xPathResult.init(contextNode.getDomNodeOrDie().getByXPath(expression, prefixResolver), type);
return xPathResult;
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class Blob method text.
/**
* @return a Promise that resolves with a string containing the
* contents of the blob, interpreted as UTF-8.
*/
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public Object text() {
final Scriptable scope = ScriptableObject.getTopLevelScope(this);
final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
try {
final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
return resolve.call(Context.getCurrentContext(), this, ctor, new Object[] { getBackend().getText() });
} catch (final IOException e) {
final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
return reject.call(Context.getCurrentContext(), this, ctor, new Object[] { e.getMessage() });
}
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class HTMLDocument method getElementsByName.
/**
* {@inheritDoc}
*/
@Override
@JsxFunction({ FF, FF_ESR })
public HTMLCollection getElementsByName(final String elementName) {
implicitCloseIfNecessary();
if ("null".equals(elementName) || (elementName.isEmpty() && getBrowserVersion().hasFeature(HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY))) {
return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
}
final HtmlPage page = getPage();
return new HTMLCollection(page, true) {
@Override
protected List<DomNode> computeElements() {
return new ArrayList<>(page.getElementsByName(elementName));
}
@Override
protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
if ("name".equals(event.getName())) {
return EffectOnCache.RESET;
}
return EffectOnCache.NONE;
}
};
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class HTMLDocument method close.
/**
* {@inheritDoc}
*/
@Override
@JsxFunction({ FF, FF_ESR })
public void close() throws IOException {
if (writeInCurrentDocument_) {
LOG.warn("close() called when document is not open.");
} else {
final HtmlPage page = getPage();
final URL url = page.getUrl();
final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
webResponse.setFromJavascript(true);
writeInCurrentDocument_ = true;
writeBuilder_.setLength(0);
final WebClient webClient = page.getWebClient();
final WebWindow window = page.getEnclosingWindow();
// reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
if (window instanceof FrameWindow) {
final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
final ScriptableObject scriptable = frame.getScriptableObject();
if (scriptable instanceof HTMLIFrameElement) {
((HTMLIFrameElement) scriptable).onRefresh();
}
}
webClient.loadWebResponseInto(webResponse, window);
}
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class HTMLDocument method dispatchEvent.
/**
* Dispatches an event into the event system (standards-conformant browsers only). See
* <a href="https://developer.mozilla.org/en-US/docs/DOM/element.dispatchEvent">the Gecko
* DOM reference</a> for more information.
*
* @param event the event to be dispatched
* @return {@code false} if at least one of the event handlers which handled the event
* called <tt>preventDefault</tt>; {@code true} otherwise
*/
@Override
@JsxFunction
public boolean dispatchEvent(final Event event) {
event.setTarget(this);
final ScriptResult result = fireEvent(event);
return !event.isAborted(result);
}
Aggregations