Search in sources :

Example 26 with JsxFunction

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

the class HTMLOptionsCollection method remove.

/**
 * Removes the option at the specified index.
 * @param index the option index
 */
@JsxFunction
public void remove(final int index) {
    int idx = index;
    final BrowserVersion browser = getBrowserVersion();
    if (idx < 0) {
        if (browser.hasFeature(JS_SELECT_OPTIONS_REMOVE_IGNORE_IF_INDEX_NEGATIVE)) {
            return;
        }
        if (index < 0 && getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_REMOVE_THROWS_IF_NEGATIV)) {
            throw Context.reportRuntimeError("Invalid index for option collection: " + index);
        }
    }
    idx = Math.max(idx, 0);
    if (idx >= getLength()) {
        return;
    }
    htmlSelect_.removeOption(idx);
}
Also used : BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 27 with JsxFunction

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

the class HTMLTableRowElement method insertCell.

/**
 * Inserts a new cell at the specified index in the element's cells collection. If the index
 * is -1 or there is no index specified, then the cell is appended at the end of the
 * element's cells collection.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536455.aspx">MSDN Documentation</a>
 * @param index specifies where to insert the cell in the tr.
 *        The default value is -1, which appends the new cell to the end of the cells collection
 * @return the newly-created cell
 */
@JsxFunction
public Object insertCell(final Object index) {
    int position = -1;
    if (!Undefined.isUndefined(index)) {
        position = (int) Context.toNumber(index);
    }
    final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
    final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
    if (indexValid) {
        final DomElement newCell = ((HtmlPage) htmlRow.getPage()).createElement("td");
        if (position == -1 || position == htmlRow.getCells().size()) {
            htmlRow.appendChild(newCell);
        } else {
            htmlRow.getCell(position).insertBefore(newCell);
        }
        return getScriptableFor(newCell);
    }
    throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount");
}
Also used : HtmlTableRow(com.gargoylesoftware.htmlunit.html.HtmlTableRow) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 28 with JsxFunction

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

the class XMLDOMNode method appendChild.

/**
 * Appends a new child as the last child of the node.
 * @param newChild the new child node to be appended at the end of the list of children belonging to this node
 * @return the new child node successfully appended to the list
 */
@JsxFunction
public Object appendChild(final Object newChild) {
    if (newChild == null || "null".equals(newChild)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    Object appendedChild = null;
    if (newChild instanceof XMLDOMNode) {
        final XMLDOMNode childNode = (XMLDOMNode) newChild;
        // Get XML node for the DOM node passed in
        final DomNode childDomNode = childNode.getDomNodeOrDie();
        // Get the parent XML node that the child should be added to.
        final DomNode parentNode = getDomNodeOrDie();
        // Append the child to the parent node
        parentNode.appendChild(childDomNode);
        appendedChild = newChild;
        // create a DocumentFragment to be the parentNode's parentNode.
        if (!(parentNode instanceof SgmlPage) && !(this instanceof XMLDOMDocumentFragment) && parentNode.getParentNode() == null) {
            final DomDocumentFragment fragment = parentNode.getPage().createDocumentFragment();
            fragment.appendChild(parentNode);
        }
    }
    return appendedChild;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 29 with JsxFunction

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

the class XMLDOMNode method selectNodes.

/**
 * Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes.
 * @param expression a string specifying an XPath expression
 * @return the collection of nodes selected by applying the given pattern-matching operation;
 *     if no nodes are selected, returns an empty collection
 */
@JsxFunction
public XMLDOMSelection selectNodes(final String expression) {
    final DomNode domNode = getDomNodeOrDie();
    final boolean attributeChangeSensitive = expression.contains("@");
    return new XMLDOMSelection(domNode, attributeChangeSensitive, "XMLDOMNode.selectNodes('" + expression + "')") {

        @Override
        protected List<DomNode> computeElements() {
            return new ArrayList<>(domNode.getByXPath(expression));
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ArrayList(java.util.ArrayList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 30 with JsxFunction

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

the class XMLDOMNode method replaceChild.

/**
 * Replaces the specified old child node with the supplied new child node.
 * @param newChild the new child that is to replace the old child; if {@code null},
 * <code>oldChild</code> is removed without a replacement
 * @param oldChild the old child that is to be replaced by the new child
 * @return the old child that is replaced
 */
@JsxFunction
public Object replaceChild(final Object newChild, final Object oldChild) {
    Object removedChild = null;
    if (newChild instanceof XMLDOMDocumentFragment) {
        final XMLDOMDocumentFragment fragment = (XMLDOMDocumentFragment) newChild;
        XMLDOMNode firstNode = null;
        final XMLDOMNode refChildObject = ((XMLDOMNode) oldChild).getNextSibling();
        for (final DomNode node : fragment.getDomNodeOrDie().getChildren()) {
            if (firstNode == null) {
                replaceChild(node.getScriptableObject(), oldChild);
                firstNode = node.getScriptableObject();
            } else {
                insertBeforeImpl(new Object[] { node.getScriptableObject(), refChildObject });
            }
        }
        if (firstNode == null) {
            removeChild(oldChild);
        }
        removedChild = oldChild;
    } else if (newChild instanceof XMLDOMNode && oldChild instanceof XMLDOMNode) {
        final XMLDOMNode newChildNode = (XMLDOMNode) newChild;
        // Get XML nodes for the DOM nodes passed in
        final DomNode newChildDomNode = newChildNode.getDomNodeOrDie();
        final DomNode oldChildDomNode = ((XMLDOMNode) oldChild).getDomNodeOrDie();
        // Replace the old child with the new child.
        oldChildDomNode.replace(newChildDomNode);
        removedChild = oldChild;
    }
    return removedChild;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) 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