Search in sources :

Example 31 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XMLDOMNodeList method nextNode.

/**
 * Returns the next node in the collection.
 * @return the next node in the collection
 */
@JsxFunction
public Object nextNode() {
    final Object nextNode;
    final List<DomNode> elements = getElements();
    if (currentIndex_ >= 0 && currentIndex_ < elements.size()) {
        nextNode = elements.get(currentIndex_).getScriptableObject();
    } else {
        nextNode = null;
    }
    currentIndex_++;
    return nextNode;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 32 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XMLHTTPRequest method send.

/**
 * Sends an HTTP request to the server and receives a response.
 * @param body the body of the message being sent with the request.
 */
@JsxFunction
public void send(final Object body) {
    if (webRequest_ == null) {
        setState(STATE_DONE, Context.getCurrentContext());
        return;
    }
    if (sent_) {
        throw Context.reportRuntimeError("Unspecified error (request already sent).");
    }
    sent_ = true;
    prepareRequest(body);
    // quite strange but IE seems to fire state loading twice
    setState(STATE_OPENED, Context.getCurrentContext());
    final Window w = getWindow();
    final WebClient client = w.getWebWindow().getWebClient();
    final AjaxController ajaxController = client.getAjaxController();
    final HtmlPage page = (HtmlPage) w.getWebWindow().getEnclosedPage();
    final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
    if (synchron) {
        doSend(Context.getCurrentContext());
    } else {
        // Create and start a thread in which to execute the request.
        final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
        final ContextAction<Object> action = cx -> {
            // KEY_STARTING_SCOPE maintains a stack of scopes
            @SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
            if (null == stack) {
                stack = new ArrayDeque<>();
                cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
            }
            stack.push(w);
            try {
                doSend(cx);
            } finally {
                stack.pop();
            }
            return null;
        };
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting XMLHTTPRequest thread for asynchronous request");
        }
        jobID_ = w.getWebWindow().getJobManager().addJob(job, page);
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Arrays(java.util.Arrays) URL(java.net.URL) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Deque(java.util.Deque) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) StringUtils(org.apache.commons.lang3.StringUtils) FormEncodingType(com.gargoylesoftware.htmlunit.FormEncodingType) BackgroundJavaScriptFactory(com.gargoylesoftware.htmlunit.javascript.background.BackgroundJavaScriptFactory) Charset(java.nio.charset.Charset) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Locale(java.util.Locale) FormData(com.gargoylesoftware.htmlunit.javascript.host.xml.FormData) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) ScriptRuntime(net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime) MalformedURLException(java.net.MalformedURLException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpHeader(com.gargoylesoftware.htmlunit.HttpHeader) Collection(java.util.Collection) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) Entry(java.util.Map.Entry) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) ArrayDeque(java.util.ArrayDeque) Function(net.sourceforge.htmlunit.corejs.javascript.Function) Collections(java.util.Collections) InputStream(java.io.InputStream) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) ArrayDeque(java.util.ArrayDeque) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) AjaxController(com.gargoylesoftware.htmlunit.AjaxController) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 33 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XSLProcessor method transform.

/**
 * Starts the transformation process or resumes a previously failed transformation.
 */
@JsxFunction
public void transform() {
    final XMLDOMNode input = input_;
    final SgmlPage page = input.getDomNodeOrDie().getPage();
    if (output_ == null || !(output_ instanceof XMLDOMNode)) {
        final DomDocumentFragment fragment = page.createDocumentFragment();
        final XMLDOMDocumentFragment node = new XMLDOMDocumentFragment();
        node.setParentScope(getParentScope());
        node.setPrototype(getPrototype(node.getClass()));
        node.setDomNode(fragment);
        output_ = fragment.getScriptableObject();
    }
    transform(input_, ((XMLDOMNode) output_).getDomNodeOrDie());
    final XMLSerializer serializer = new XMLSerializer(false);
    final StringBuilder output = new StringBuilder();
    for (final DomNode child : ((XMLDOMNode) output_).getDomNodeOrDie().getChildren()) {
        if (child instanceof DomText) {
            // See XMLDocumentTest.testLoadXML_XMLSpaceAttribute()
            if (StringUtils.isNotBlank(((DomText) child).getData())) {
                output.append(((DomText) child).getData());
            }
        } else {
            // remove trailing "\r\n"
            final String serializedString = serializer.serializeToString(child.getScriptableObject());
            output.append(serializedString, 0, serializedString.length() - 2);
        }
    }
    output_ = output.toString();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 34 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XMLDOMCharacterData method appendData.

/**
 * Appends the supplied string to the existing string data.
 * @param data the data that is to be appended to the existing string
 */
@JsxFunction
public void appendData(final String data) {
    if (data == null || "null".equals(data)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    final DomCharacterData domCharacterData = getDomNodeOrDie();
    domCharacterData.appendData(data);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 35 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XMLDOMDocument method createElement.

/**
 * Creates an element node using the specified name.
 * @param tagName the name for the new element node
 * @return the new element object or <code>NOT_FOUND</code> if the tag is not supported
 */
@JsxFunction
public Object createElement(final String tagName) {
    if (tagName == null || "null".equals(tagName)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    if (StringUtils.isBlank(tagName) || tagName.indexOf('<') >= 0 || tagName.indexOf('>') >= 0) {
        throw Context.reportRuntimeError("To create a node of type ELEMENT a valid name must be given.");
    }
    Object result = NOT_FOUND;
    try {
        final DomElement domElement = (DomElement) getPage().createElement(tagName);
        final Object jsElement = getScriptableFor(domElement);
        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createElement(" + tagName + ") cannot return a result as there isn't a JavaScript object for the element " + domElement.getClass().getName());
            }
        } else {
            result = jsElement;
        }
    } catch (final ElementNotFoundException e) {
    // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) 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