Search in sources :

Example 16 with JsxFunction

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;
}
Also used : NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) PrefixResolver(org.apache.xml.utils.PrefixResolver) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 17 with JsxFunction

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() });
    }
}
Also used : LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) IOException(java.io.IOException) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 18 with JsxFunction

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;
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 19 with JsxFunction

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);
    }
}
Also used : StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 20 with JsxFunction

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);
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) 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