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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#appendChild() . 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: Element.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the given text into the element at the specified location.
 * @param where specifies where to insert the text, using one of the following values (case-insensitive):
 *      beforebegin, afterbegin, beforeend, afterend
 * @param text the text to insert
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536453.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF, FF68, FF60})
public void insertAdjacentText(final String where, final String text) {
    final Object[] values = getInsertAdjacentLocation(where);
    final DomNode node = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    final DomText domText = new DomText(node.getPage(), text);
    // add the new nodes
    if (append) {
        node.appendChild(domText);
    }
    else {
        node.insertBefore(domText);
    }
}
 
Example 2
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 3
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 4
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 5
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the given text into the element at the specified location.
 * @param where specifies where to insert the text, using one of the following values (case-insensitive):
 *      beforebegin, afterbegin, beforeend, afterend
 * @param text the text to insert
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536453.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF52})
public void insertAdjacentText(final String where, final String text) {
    final Object[] values = getInsertAdjacentLocation(where);
    final DomNode node = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    final DomText domText = new DomText(node.getPage(), text);
    // add the new nodes
    if (append) {
        node.appendChild(domText);
    }
    else {
        node.insertBefore(domText);
    }
}
 
Example 6
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the given element into the element at the location.
 * @param where specifies where to insert the element, using one of the following values (case-insensitive):
 *        beforebegin, afterbegin, beforeend, afterend
 * @param insertedElement the element to be inserted
 * @return an element object
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536451.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF52})
public Object insertAdjacentElement(final String where, final Object insertedElement) {
    if (insertedElement instanceof Node) {
        final DomNode childNode = ((Node) insertedElement).getDomNodeOrDie();
        final Object[] values = getInsertAdjacentLocation(where);
        final DomNode node = (DomNode) values[0];
        final boolean append = ((Boolean) values[1]).booleanValue();

        if (append) {
            node.appendChild(childNode);
        }
        else {
            node.insertBefore(childNode);
        }
        return insertedElement;
    }
    throw Context.reportRuntimeError("Passed object is not an element: " + insertedElement);
}
 
Example 7
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * The worker for setInnerText.
 * @param value the new value for the contents of this node
 */
protected void setInnerTextImpl(final String value) {
    final DomNode domNode = getDomNodeOrDie();

    domNode.removeAllChildren();

    if (value != null && !value.isEmpty()) {
        domNode.appendChild(new DomText(domNode.getPage(), value));
    }
}
 
Example 8
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 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;

        // if the parentNode has null parentNode in IE,
        // 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;
}
 
Example 9
Source File: HTMLTitleElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} attribute.
 * @param text the {@code text} attribute
 */
@JsxSetter
public void setText(final String text) {
    final DomNode htmlElement = getDomNodeOrDie();
    DomNode firstChild = htmlElement.getFirstChild();
    if (firstChild == null) {
        firstChild = new DomText(htmlElement.getPage(), text);
        htmlElement.appendChild(firstChild);
    }
    else {
        firstChild.setNodeValue(text);
    }
}
 
Example 10
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a DOM node to the node.
 * @param childObject the node to add to this node
 * @return the newly added child node
 */
@JsxFunction
public Object appendChild(final Object childObject) {
    Object appendedChild = null;
    if (childObject instanceof Node) {
        final Node childNode = (Node) childObject;

        // is the node allowed here?
        if (!isNodeInsertable(childNode)) {
            throw asJavaScriptException(
                new DOMException("Node cannot be inserted at the specified point in the hierarchy",
                    DOMException.HIERARCHY_REQUEST_ERR));
        }

        // 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 = childObject;

        initInlineFrameIfNeeded(childDomNode);
        for (final DomNode domNode : childDomNode.getDescendants()) {
            initInlineFrameIfNeeded(domNode);
        }
    }
    return appendedChild;
}
 
Example 11
Source File: XSLTProcessor.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void transform(final Node source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtil.appendChild(parentPage, parent, children.item(i), true);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 12
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * 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;

        // if the parentNode has null parentNode in IE,
        // 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;
}
 
Example 13
Source File: XSLProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void transform(final XMLDOMNode source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtils.appendChild(parentPage, parent, children.item(i), false);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 14
Source File: XmlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Copy all children from 'source' to 'dest', within the context of the specified page.
 * @param page the page which the nodes belong to
 * @param source the node to copy from
 * @param dest the node to copy to
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
private static void copy(final SgmlPage page, final Node source, final DomNode dest,
    final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final NodeList nodeChildren = source.getChildNodes();
    for (int i = 0; i < nodeChildren.getLength(); i++) {
        final Node child = nodeChildren.item(i);
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
                dest.appendChild(childXml);
                copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
                break;

            case Node.TEXT_NODE:
                dest.appendChild(new DomText(page, child.getNodeValue()));
                break;

            case Node.CDATA_SECTION_NODE:
                dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
                break;

            case Node.COMMENT_NODE:
                dest.appendChild(new DomComment(page, child.getNodeValue()));
                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
                break;

            default:
                if (LOG.isWarnEnabled()) {
                    LOG.warn("NodeType " + child.getNodeType()
                        + " (" + child.getNodeName() + ") is not yet supported.");
                }
        }
    }
}
 
Example 15
Source File: XmlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively appends a {@link Node} child to {@link DomNode} parent.
 *
 * @param page the owner page of {@link DomElement}s to be created
 * @param parent the parent DomNode
 * @param child the child Node
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 * @param attributesOrderMap (optional) the one returned by {@link #getAttributesOrderMap(Document)}
 */
public static void appendChild(final SgmlPage page, final DomNode parent, final Node child,
    final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final DocumentType documentType = child.getOwnerDocument().getDoctype();
    if (documentType != null && page instanceof XmlPage) {
        final DomDocumentType domDoctype = new DomDocumentType(
                page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
        ((XmlPage) page).setDocumentType(domDoctype);
    }
    final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
    parent.appendChild(childXml);
    copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
}
 
Example 16
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * The worker for setInnerText.
 * @param value the new value for the contents of this node
 */
protected void setInnerTextImpl(final String value) {
    final DomNode domNode = getDomNodeOrDie();

    domNode.removeAllChildren();

    if (value != null && !value.isEmpty()) {
        domNode.appendChild(new DomText(domNode.getPage(), value));
    }
}
 
Example 17
Source File: HTMLTitleElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} attribute.
 * @param text the {@code text} attribute
 */
@JsxSetter
public void setText(final String text) {
    final DomNode htmlElement = getDomNodeOrDie();
    DomNode firstChild = htmlElement.getFirstChild();
    if (firstChild == null) {
        firstChild = new DomText(htmlElement.getPage(), text);
        htmlElement.appendChild(firstChild);
    }
    else {
        firstChild.setNodeValue(text);
    }
}
 
Example 18
Source File: XmlUtil.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively appends a {@link Node} child to {@link DomNode} parent.
 *
 * @param page the owner page of {@link DomElement}s to be created
 * @param parent the parent DomNode
 * @param child the child Node
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 * @param attributesOrderMap (optional) the one returned by {@link #getAttributesOrderMap(Document)}
 */
public static void appendChild(final SgmlPage page, final DomNode parent, final Node child,
    final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final DocumentType documentType = child.getOwnerDocument().getDoctype();
    if (documentType != null && page instanceof XmlPage) {
        final DomDocumentType domDoctype = new DomDocumentType(
                page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
        ((XmlPage) page).setDocumentType(domDoctype);
    }
    final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
    parent.appendChild(childXml);
    copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
}
 
Example 19
Source File: XmlUtil.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Copy all children from 'source' to 'dest', within the context of the specified page.
 * @param page the page which the nodes belong to
 * @param source the node to copy from
 * @param dest the node to copy to
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
private static void copy(final SgmlPage page, final Node source, final DomNode dest,
    final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final NodeList nodeChildren = source.getChildNodes();
    for (int i = 0; i < nodeChildren.getLength(); i++) {
        final Node child = nodeChildren.item(i);
        switch (child.getNodeType()) {
            case Node.ELEMENT_NODE:
                final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
                dest.appendChild(childXml);
                copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
                break;

            case Node.TEXT_NODE:
                dest.appendChild(new DomText(page, child.getNodeValue()));
                break;

            case Node.CDATA_SECTION_NODE:
                dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
                break;

            case Node.COMMENT_NODE:
                dest.appendChild(new DomComment(page, child.getNodeValue()));
                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
                break;

            default:
                LOG.warn("NodeType " + child.getNodeType()
                    + " (" + child.getNodeName() + ") is not yet supported.");
        }
    }
}
 
Example 20
Source File: XSLTProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void transform(final Node source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtils.appendChild(parentPage, parent, children.item(i), true);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}