Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#remove()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Node.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a DOM node from this node.
 * @param childObject the node to remove from this node
 * @return the removed child node
 */
@JsxFunction
public Object removeChild(final Object childObject) {
    if (!(childObject instanceof Node)) {
        return null;
    }

    // Get XML node for the DOM node passed in
    final DomNode childNode = ((Node) childObject).getDomNodeOrDie();

    if (!getDomNodeOrDie().isAncestorOf(childNode)) {
        Context.throwAsScriptRuntimeEx(new Exception("NotFoundError: Failed to execute 'removeChild' on '"
                    + this + "': The node to be removed is not a child of this node."));
    }
    // Remove the child from the parent node
    childNode.remove();
    return childObject;
}
 
Example 2
Source File: SimpleRange.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static void deleteBefore(final DomNode node, int offset) {
    if (isOffsetChars(node)) {
        String text = getText(node);
        if (offset > -1 && offset < text.length()) {
            text = text.substring(offset);
        }
        else {
            text = "";
        }
        setText(node, text);
    }
    else {
        final DomNodeList<DomNode> children = node.getChildNodes();
        for (int i = 0; i < offset && i < children.getLength(); i++) {
            final DomNode child = children.get(i);
            child.remove();
            i--;
            offset--;
        }
    }
}
 
Example 3
Source File: SimpleRange.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static void deleteAfter(final DomNode node, final int offset) {
    if (isOffsetChars(node)) {
        String text = getText(node);
        if (offset > -1 && offset < text.length()) {
            text = text.substring(0, offset);
            setText(node, text);
        }
    }
    else {
        final DomNodeList<DomNode> children = node.getChildNodes();
        for (int i = offset; i < children.getLength(); i++) {
            final DomNode child = children.get(i);
            child.remove();
            i--;
        }
    }
}
 
Example 4
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a DOM node from this node.
 * @param childObject the node to remove from this node
 * @return the removed child node
 */
@JsxFunction
public Object removeChild(final Object childObject) {
    if (!(childObject instanceof Node)) {
        return null;
    }

    // Get XML node for the DOM node passed in
    final DomNode childNode = ((Node) childObject).getDomNodeOrDie();

    if (!getDomNodeOrDie().isAncestorOf(childNode)) {
        Context.throwAsScriptRuntimeEx(new Exception("NotFoundError: Failed to execute 'removeChild' on '"
                    + this + "': The node to be removed is not a child of this node."));
    }
    // Remove the child from the parent node
    childNode.remove();
    return childObject;
}
 
Example 5
Source File: SimpleRange.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static void deleteBefore(final DomNode node, int offset) {
    if (isOffsetChars(node)) {
        String text = getText(node);
        if (offset > -1 && offset < text.length()) {
            text = text.substring(offset);
        }
        else {
            text = "";
        }
        setText(node, text);
    }
    else {
        final DomNodeList<DomNode> children = node.getChildNodes();
        for (int i = 0; i < offset && i < children.getLength(); i++) {
            final DomNode child = children.get(i);
            child.remove();
            i--;
            offset--;
        }
    }
}
 
Example 6
Source File: SimpleRange.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static void deleteAfter(final DomNode node, final int offset) {
    if (isOffsetChars(node)) {
        String text = getText(node);
        if (offset > -1 && offset < text.length()) {
            text = text.substring(0, offset);
            setText(node, text);
        }
    }
    else {
        final DomNodeList<DomNode> children = node.getChildNodes();
        for (int i = offset; i < children.getLength(); i++) {
            final DomNode child = children.get(i);
            child.remove();
            i--;
        }
    }
}
 
Example 7
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified child node from the list of children and returns it.
 * @param childNode the child node to be removed from the list of children of this node
 * @return the removed child node
 */
@JsxFunction
public Object removeChild(final Object childNode) {
    Object removedChild = null;

    if (childNode instanceof XMLDOMNode) {
        // Get XML node for the DOM node passed in
        final DomNode childDomNode = ((XMLDOMNode) childNode).getDomNodeOrDie();

        // Remove the child from the parent node
        childDomNode.remove();
        removedChild = childNode;
    }
    return removedChild;
}
 
Example 8
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified child node from the list of children and returns it.
 * @param childNode the child node to be removed from the list of children of this node
 * @return the removed child node
 */
@JsxFunction
public Object removeChild(final Object childNode) {
    Object removedChild = null;

    if (childNode instanceof XMLDOMNode) {
        // Get XML node for the DOM node passed in
        final DomNode childDomNode = ((XMLDOMNode) childNode).getDomNodeOrDie();

        // Remove the child from the parent node
        childDomNode.remove();
        removedChild = childNode;
    }
    return removedChild;
}
 
Example 9
Source File: Element.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces this element (including all child elements) with the supplied value.
 * @param value the new value for replacing this element
 */
@JsxSetter({CHROME, FF, FF68, FF60})
public void setOuterHTML(final Object value) {
    final DomNode domNode = getDomNodeOrDie();
    final DomNode parent = domNode.getParentNode();
    if (null == parent) {
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_REMOVES_CHILDREN_FOR_DETACHED)) {
            domNode.removeAllChildren();
        }
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
            throw Context.reportRuntimeError("outerHTML is readonly for detached nodes");
        }
        return;
    }

    if (value == null && !getBrowserVersion().hasFeature(JS_OUTER_HTML_NULL_AS_STRING)) {
        domNode.remove();
        return;
    }
    final String valueStr = Context.toString(value);
    if (valueStr.isEmpty()) {
        domNode.remove();
        return;
    }

    final DomNode nextSibling = domNode.getNextSibling();
    domNode.remove();

    final DomNode target;
    final boolean append;
    if (nextSibling != null) {
        target = nextSibling;
        append = false;
    }
    else {
        target = parent;
        append = true;
    }

    final DomNode proxyDomNode = new ProxyDomNode(target.getPage(), target, append);
    parseHtmlSnippet(proxyDomNode, valueStr);
}
 
Example 10
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces this element (including all child elements) with the supplied value.
 * @param value the new value for replacing this element
 */
@JsxSetter({CHROME, FF})
public void setOuterHTML(final Object value) {
    final DomNode domNode = getDomNodeOrDie();
    final DomNode parent = domNode.getParentNode();
    if (null == parent) {
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_REMOVES_CHILDREN_FOR_DETACHED)) {
            domNode.removeAllChildren();
        }
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
            throw Context.reportRuntimeError("outerHTML is readonly for detached nodes");
        }
        return;
    }

    if (value == null && !getBrowserVersion().hasFeature(JS_OUTER_HTML_NULL_AS_STRING)) {
        domNode.remove();
        return;
    }
    final String valueStr = Context.toString(value);
    if (valueStr.isEmpty()) {
        domNode.remove();
        return;
    }

    final DomNode nextSibling = domNode.getNextSibling();
    domNode.remove();

    final DomNode target;
    final boolean append;
    if (nextSibling != null) {
        target = nextSibling;
        append = false;
    }
    else {
        target = parent;
        append = true;
    }

    final DomNode proxyDomNode = new ProxyDomNode(target.getPage(), target, append);
    parseHtmlSnippet(proxyDomNode, valueStr);
}