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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getNextSibling() . 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
/**
 * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
 * just after this ChildNode.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 */
protected static void after(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final DomNode thisDomNode = ((Node) thisObj).getDomNodeOrDie();
    final DomNode parentNode = thisDomNode.getParentNode();
    final DomNode nextSibling = thisDomNode.getNextSibling();
    for (Object arg : args) {
        final Node node = toNodeOrTextNode((Node) thisObj, arg);
        final DomNode newNode = node.getDomNodeOrDie();
        if (nextSibling != null) {
            nextSibling.insertBefore(newNode);
        }
        else {
            parentNode.appendChild(newNode);
        }
    }
}
 
Example 2
Source File: Node.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces this ChildNode in the children list of its parent with a set of Node or DOMString objects.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 */
protected static void replaceWith(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final DomNode thisDomNode = ((Node) thisObj).getDomNodeOrDie();
    final DomNode parentNode = thisDomNode.getParentNode();
    final DomNode nextSibling = thisDomNode.getNextSibling();
    boolean isFirst = true;
    for (Object arg : args) {
        final DomNode newNode = toNodeOrTextNode((Node) thisObj, arg).getDomNodeOrDie();
        if (isFirst) {
            isFirst = false;
            thisDomNode.replace(newNode);
        }
        else {
            if (nextSibling != null) {
                nextSibling.insertBefore(newNode);
            }
            else {
                parentNode.appendChild(newNode);
            }
        }
    }
}
 
Example 3
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts a set of Node or DOMString objects in the children list of this ChildNode's parent,
 * just after this ChildNode.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 */
protected static void after(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final DomNode thisDomNode = ((Node) thisObj).getDomNodeOrDie();
    final DomNode parentNode = thisDomNode.getParentNode();
    final DomNode nextSibling = thisDomNode.getNextSibling();
    for (Object arg : args) {
        final Node node = toNodeOrTextNode((Node) thisObj, arg);
        final DomNode newNode = node.getDomNodeOrDie();
        if (nextSibling != null) {
            nextSibling.insertBefore(newNode);
        }
        else {
            parentNode.appendChild(newNode);
        }
    }
}
 
Example 4
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces this ChildNode in the children list of its parent with a set of Node or DOMString objects.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 */
protected static void replaceWith(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final DomNode thisDomNode = ((Node) thisObj).getDomNodeOrDie();
    final DomNode parentNode = thisDomNode.getParentNode();
    final DomNode nextSibling = thisDomNode.getNextSibling();
    boolean isFirst = true;
    for (Object arg : args) {
        final DomNode newNode = toNodeOrTextNode((Node) thisObj, arg).getDomNodeOrDie();
        if (isFirst) {
            isFirst = false;
            thisDomNode.replace(newNode);
        }
        else {
            if (nextSibling != null) {
                nextSibling.insertBefore(newNode);
            }
            else {
                parentNode.appendChild(newNode);
            }
        }
    }
}
 
Example 5
Source File: ComputedCSSStyleDeclaration.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private int getTopForAbsolutePositionWithInheritance() {
    final String t = getTopWithInheritance();

    if (!AUTO.equals(t)) {
        // No need to calculate displacement caused by sibling nodes.
        return pixelValue(t);
    }

    final String b = getBottomWithInheritance();
    if (!AUTO.equals(b)) {
        // Estimate the vertical displacement caused by *all* siblings.
        // This is very rough, and doesn't even take position or display types into account.
        // It also doesn't take into account the fact that the parent's height may be hardcoded in CSS.
        int top = 0;
        DomNode child = getElement().getDomNodeOrDie().getParentNode().getFirstChild();
        while (child != null) {
            if (child instanceof HtmlElement && child.mayBeDisplayed()) {
                top += 20;
            }
            child = child.getNextSibling();
        }
        top -= pixelValue(b);
        return top;
    }

    return 0;
}
 
Example 6
Source File: SgmlPage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the document element.
 * @return the document element
 */
@Override
public DomElement getDocumentElement() {
    DomNode childNode = getFirstChild();
    while (childNode != null && !(childNode instanceof DomElement)) {
        childNode = childNode.getNextSibling();
    }
    return (DomElement) childNode;
}
 
Example 7
Source File: ComputedCSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private int getTopForAbsolutePositionWithInheritance() {
    int top = 0;
    final String t = getTopWithInheritance();

    if (!"auto".equals(t)) {
        // No need to calculate displacement caused by sibling nodes.
        top = pixelValue(t);
    }
    else {
        final String b = getBottomWithInheritance();

        if (!"auto".equals(b)) {
            // Estimate the vertical displacement caused by *all* siblings.
            // This is very rough, and doesn't even take position or display types into account.
            // It also doesn't take into account the fact that the parent's height may be hardcoded in CSS.
            top = 0;
            DomNode child = getElement().getDomNodeOrDie().getParentNode().getFirstChild();
            while (child != null) {
                if (child instanceof HtmlElement && child.mayBeDisplayed()) {
                    top += 20;
                }
                child = child.getNextSibling();
            }
            top -= pixelValue(b);
        }
    }
    return top;
}
 
Example 8
Source File: SgmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the document element.
 * @return the document element
 */
@Override
public DomElement getDocumentElement() {
    DomNode childNode = getFirstChild();
    while (childNode != null && !(childNode instanceof DomElement)) {
        childNode = childNode.getNextSibling();
    }
    return (DomElement) childNode;
}
 
Example 9
Source File: Element.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns where and how to add the new node.
 * Used by {@link #insertAdjacentHTML(String, String)},
 * {@link #insertAdjacentElement(String, Object)} and
 * {@link #insertAdjacentText(String, String)}.
 * @param where specifies where to insert the element, using one of the following values (case-insensitive):
 *        beforebegin, afterbegin, beforeend, afterend
 * @return an array of 1-DomNode:parentNode and 2-Boolean:append
 */
private Object[] getInsertAdjacentLocation(final String where) {
    final DomNode currentNode = getDomNodeOrDie();
    final DomNode node;
    final boolean append;

    // compute the where and how the new nodes should be added
    if (POSITION_AFTER_BEGIN.equalsIgnoreCase(where)) {
        if (currentNode.getFirstChild() == null) {
            // new nodes should appended to the children of current node
            node = currentNode;
            append = true;
        }
        else {
            // new nodes should be inserted before first child
            node = currentNode.getFirstChild();
            append = false;
        }
    }
    else if (POSITION_BEFORE_BEGIN.equalsIgnoreCase(where)) {
        // new nodes should be inserted before current node
        node = currentNode;
        append = false;
    }
    else if (POSITION_BEFORE_END.equalsIgnoreCase(where)) {
        // new nodes should appended to the children of current node
        node = currentNode;
        append = true;
    }
    else if (POSITION_AFTER_END.equalsIgnoreCase(where)) {
        if (currentNode.getNextSibling() == null) {
            // new nodes should appended to the children of parent node
            node = currentNode.getParentNode();
            append = true;
        }
        else {
            // new nodes should be inserted before current node's next sibling
            node = currentNode.getNextSibling();
            append = false;
        }
    }
    else {
        throw Context.reportRuntimeError("Illegal position value: \"" + where + "\"");
    }

    if (append) {
        return new Object[] {node, Boolean.TRUE};
    }
    return new Object[] {node, Boolean.FALSE};
}
 
Example 10
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 11
Source File: XMLSerializer.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void toXml(final int indent,
        final DomNode node, final StringBuilder builder) {
    final String nodeName = node.getNodeName();
    builder.append('<').append(nodeName);

    final String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement;
                parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    }

    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        builder.append(' ').append(attrib.getQualifiedName()).append('=')
            .append('"').append(attrib.getValue()).append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            builder.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                toXml(indent + 1, child, builder);
                break;

            case Node.TEXT_NODE:
                String value = child.getNodeValue();
                value = StringUtils.escapeXmlChars(value);
                if (preserveWhiteSpace_) {
                    builder.append(value.replace("\n", "\r\n"));
                }
                else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
                    builder.append("\r\n");
                    final DomNode sibling = child.getNextSibling();
                    if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
                        for (int i = 0; i < indent; i++) {
                            builder.append('\t');
                        }
                    }
                }
                else {
                    builder.append(value.replace("\n", "\r\n"));
                }
                break;

            case Node.CDATA_SECTION_NODE:
            case Node.COMMENT_NODE:
                if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
                    for (int i = 0; i < indent; i++) {
                        builder.append('\t');
                    }
                }
                builder.append(child.asXml());
                break;

            default:

        }
    }
    if (!startTagClosed) {
        builder.append(optionalPrefix).append("/>");
    }
    else {
        if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
            for (int i = 0; i < indent - 1; i++) {
                builder.append('\t');
            }
        }
        builder.append('<').append('/').append(nodeName).append('>');
    }
}
 
Example 12
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns where and how to add the new node.
 * Used by {@link #insertAdjacentHTML(String, String)},
 * {@link #insertAdjacentElement(String, Object)} and
 * {@link #insertAdjacentText(String, String)}.
 * @param where specifies where to insert the element, using one of the following values (case-insensitive):
 *        beforebegin, afterbegin, beforeend, afterend
 * @return an array of 1-DomNode:parentNode and 2-Boolean:append
 */
private Object[] getInsertAdjacentLocation(final String where) {
    final DomNode currentNode = getDomNodeOrDie();
    final DomNode node;
    final boolean append;

    // compute the where and how the new nodes should be added
    if (POSITION_AFTER_BEGIN.equalsIgnoreCase(where)) {
        if (currentNode.getFirstChild() == null) {
            // new nodes should appended to the children of current node
            node = currentNode;
            append = true;
        }
        else {
            // new nodes should be inserted before first child
            node = currentNode.getFirstChild();
            append = false;
        }
    }
    else if (POSITION_BEFORE_BEGIN.equalsIgnoreCase(where)) {
        // new nodes should be inserted before current node
        node = currentNode;
        append = false;
    }
    else if (POSITION_BEFORE_END.equalsIgnoreCase(where)) {
        // new nodes should appended to the children of current node
        node = currentNode;
        append = true;
    }
    else if (POSITION_AFTER_END.equalsIgnoreCase(where)) {
        if (currentNode.getNextSibling() == null) {
            // new nodes should appended to the children of parent node
            node = currentNode.getParentNode();
            append = true;
        }
        else {
            // new nodes should be inserted before current node's next sibling
            node = currentNode.getNextSibling();
            append = false;
        }
    }
    else {
        throw Context.reportRuntimeError("Illegal position value: \"" + where + "\"");
    }

    if (append) {
        return new Object[] {node, Boolean.TRUE};
    }
    return new Object[] {node, Boolean.FALSE};
}
 
Example 13
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);
}
 
Example 14
Source File: XMLSerializer.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private void toXml(final int indent,
        final DomNode node, final StringBuilder builder) {
    final String nodeName = node.getNodeName();
    builder.append('<').append(nodeName);

    final String optionalPrefix = "";
    final String namespaceURI = node.getNamespaceURI();
    final String prefix = node.getPrefix();
    if (namespaceURI != null && prefix != null) {
        boolean sameNamespace = false;
        for (DomNode parentNode = node.getParentNode(); parentNode instanceof DomElement;
                parentNode = parentNode.getParentNode()) {
            if (namespaceURI.equals(parentNode.getNamespaceURI())) {
                sameNamespace = true;
            }
        }
        if (node.getParentNode() == null || !sameNamespace) {
            ((DomElement) node).setAttribute("xmlns:" + prefix, namespaceURI);
        }
    }

    final NamedNodeMap attributesMap = node.getAttributes();
    for (int i = 0; i < attributesMap.getLength(); i++) {
        final DomAttr attrib = (DomAttr) attributesMap.item(i);
        builder.append(' ').append(attrib.getQualifiedName()).append('=')
            .append('"').append(attrib.getValue()).append('"');
    }
    boolean startTagClosed = false;
    for (final DomNode child : node.getChildren()) {
        if (!startTagClosed) {
            builder.append(optionalPrefix).append('>');
            startTagClosed = true;
        }
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                toXml(indent + 1, child, builder);
                break;

            case Node.TEXT_NODE:
                String value = child.getNodeValue();
                value = StringUtils.escapeXmlChars(value);
                if (preserveWhiteSpace_) {
                    builder.append(value.replace("\n", "\r\n"));
                }
                else if (org.apache.commons.lang3.StringUtils.isBlank(value)) {
                    builder.append("\r\n");
                    final DomNode sibling = child.getNextSibling();
                    if (sibling != null && sibling.getNodeType() == Node.ELEMENT_NODE) {
                        for (int i = 0; i < indent; i++) {
                            builder.append('\t');
                        }
                    }
                }
                else {
                    builder.append(value.replace("\n", "\r\n"));
                }
                break;

            case Node.CDATA_SECTION_NODE:
            case Node.COMMENT_NODE:
                if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
                    for (int i = 0; i < indent; i++) {
                        builder.append('\t');
                    }
                }
                builder.append(child.asXml());
                break;

            default:

        }
    }
    if (!startTagClosed) {
        builder.append(optionalPrefix).append("/>");
    }
    else {
        if (!preserveWhiteSpace_ && builder.charAt(builder.length() - 1) == '\n') {
            for (int i = 0; i < indent - 1; i++) {
                builder.append('\t');
            }
        }
        builder.append('<').append('/').append(nodeName).append('>');
    }
}