Search in sources :

Example 1 with SubmittableElement

use of com.gargoylesoftware.htmlunit.html.SubmittableElement in project htmlunit by HtmlUnit.

the class HtmlUnitNekoDOMBuilder method addNodeToRightParent.

/**
 * Adds the new node to the right parent that is not necessary the currentNode in case of
 * malformed HTML code. The method tries to emulate the behavior of Firefox.
 */
private void addNodeToRightParent(final DomNode currentNode, final DomElement newElement) {
    final String currentNodeName = currentNode.getNodeName();
    final String newNodeName = newElement.getNodeName();
    DomNode parent = currentNode;
    // correct parent.
    if ("tr".equals(newNodeName) && !isTableChild(currentNodeName)) {
        parent = findElementOnStack("tbody", "thead", "tfoot");
    } else if (isTableChild(newNodeName) && !"table".equals(currentNodeName)) {
        parent = findElementOnStack("table");
    } else if (isTableCell(newNodeName) && !"tr".equals(currentNodeName)) {
        parent = findElementOnStack("tr");
    }
    // If the parent changed and the old parent was a form it is now waiting for lost children.
    if (parent != currentNode && "form".equals(currentNodeName)) {
        formWaitingForLostChildren_ = (HtmlForm) currentNode;
    }
    final String parentNodeName = parent.getNodeName();
    if (// scripts are valid inside tables
    !"script".equals(newNodeName) && (("table".equals(parentNodeName) && !isTableChild(newNodeName)) || (!"tr".equals(newNodeName) && ("thead".equals(parentNodeName) || "tbody".equals(parentNodeName) || "tfoot".equals(parentNodeName))) || ("colgroup".equals(parentNodeName) && !"col".equals(newNodeName)) || ("tr".equals(parentNodeName) && !isTableCell(newNodeName)))) {
        // Otherwise insert the element before the table.
        if ("form".equals(newNodeName)) {
            formWaitingForLostChildren_ = (HtmlForm) newElement;
            appendChild(parent, newElement);
        } else if (newElement instanceof SubmittableElement) {
            if (formWaitingForLostChildren_ != null) {
                formWaitingForLostChildren_.addLostChild((HtmlElement) newElement);
            }
            appendChild(parent, newElement);
        } else {
            parent = findElementOnStack("table");
            parent.insertBefore(newElement);
        }
    } else if (formWaitingForLostChildren_ != null && "form".equals(parentNodeName)) {
        // everything else before the table.
        if (newElement instanceof SubmittableElement) {
            formWaitingForLostChildren_.addLostChild((HtmlElement) newElement);
            appendChild(parent.getParentNode(), newElement);
        } else {
            parent = findElementOnStack("table");
            parent.insertBefore(newElement);
        }
    } else if (formWaitingForLostChildren_ != null && newElement instanceof SubmittableElement) {
        formWaitingForLostChildren_.addLostChild((HtmlElement) newElement);
        appendChild(parent, newElement);
    } else {
        appendChild(parent, newElement);
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SubmittableElement(com.gargoylesoftware.htmlunit.html.SubmittableElement)

Example 2 with SubmittableElement

use of com.gargoylesoftware.htmlunit.html.SubmittableElement in project htmlunit by HtmlUnit.

the class HTMLFormElement method requestSubmit.

/**
 * Submits the form by submitted using a specific submit button.
 * @param submitter The submit button whose attributes describe the method
 * by which the form is to be submitted. This may be either
 * an <input> or <button> element whose type attribute is submit.
 * If you omit the submitter parameter, the form element itself is used as the submitter.
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void requestSubmit(final Object submitter) {
    if (Undefined.isUndefined(submitter)) {
        submit();
        return;
    }
    SubmittableElement submittable = null;
    if (submitter instanceof HTMLElement) {
        final HTMLElement subHtmlElement = (HTMLElement) submitter;
        if (subHtmlElement instanceof HTMLButtonElement) {
            if ("submit".equals(((HTMLButtonElement) subHtmlElement).getType())) {
                submittable = (SubmittableElement) subHtmlElement.getDomNodeOrDie();
            }
        } else if (subHtmlElement instanceof HTMLInputElement) {
            if ("submit".equals(((HTMLInputElement) subHtmlElement).getType())) {
                submittable = (SubmittableElement) subHtmlElement.getDomNodeOrDie();
            }
        }
        if (submittable != null && subHtmlElement.getForm() != this) {
            throw ScriptRuntime.typeError("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + "The specified element is not owned by this form element.");
        }
    }
    if (submittable == null) {
        throw ScriptRuntime.typeError("Failed to execute 'requestSubmit' on 'HTMLFormElement': " + "The specified element is not a submit button.");
    }
    this.getHtmlForm().submit(submittable);
}
Also used : SubmittableElement(com.gargoylesoftware.htmlunit.html.SubmittableElement) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

SubmittableElement (com.gargoylesoftware.htmlunit.html.SubmittableElement)2 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1