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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#insertBefore() . 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 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, FF, FF68, FF60})
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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
Source File: Node.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Add a DOM node as a child to this node before the referenced node.
 * If the referenced node is null, append to the end.
 * @param args the arguments
 * @return the newly added child node
 */
protected Object insertBeforeImpl(final Object[] args) {
    if (args.length < 1) {
        throw ScriptRuntime.typeError(
                "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 0 present.");
    }

    final Object newChildObject = args[0];
    final Object refChildObject;
    if (args.length > 1) {
        refChildObject = args[1];
    }
    else {
        refChildObject = Undefined.instance;
    }
    Object insertedChild = null;

    if (newChildObject instanceof Node) {
        final Node newChild = (Node) newChildObject;
        final DomNode newChildNode = newChild.getDomNodeOrDie();

        // is the node allowed here?
        if (!isNodeInsertable(newChild)) {
            throw ScriptRuntime.constructError("ReferenceError",
                    "Node cannot be inserted at the specified point in the hierarchy");
        }
        if (newChildNode instanceof DomDocumentFragment) {
            final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
            for (final DomNode child : fragment.getChildren()) {
                if (!isNodeInsertable((Node) child.getScriptableObject())) {
                    throw ScriptRuntime.constructError("ReferenceError",
                            "Node cannot be inserted at the specified point in the hierarchy");
                }
            }
        }

        // extract refChild
        final DomNode refChildNode;
        if (Undefined.isUndefined(refChildObject)) {
            if (args.length == 2 || getBrowserVersion().hasFeature(JS_NODE_INSERT_BEFORE_REF_OPTIONAL)) {
                refChildNode = null;
            }
            else {
                throw ScriptRuntime.typeError(
                        "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present.");
            }
        }
        else if (refChildObject != null) {
            refChildNode = ((Node) refChildObject).getDomNodeOrDie();
        }
        else {
            refChildNode = null;
        }

        final DomNode domNode = getDomNodeOrDie();

        try {
            domNode.insertBefore(newChildNode, refChildNode);
        }
        catch (final org.w3c.dom.DOMException e) {
            throw ScriptRuntime.constructError("ReferenceError", e.getMessage());
        }
        insertedChild = newChild;
    }
    return insertedChild;
}
 
Example 10
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Add a DOM node as a child to this node before the referenced node.
 * If the referenced node is null, append to the end.
 * @param args the arguments
 * @return the newly added child node
 */
protected Object insertBeforeImpl(final Object[] args) {
    if (args.length < 1) {
        throw ScriptRuntime.typeError(
                "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 0 present.");
    }

    final Object newChildObject = args[0];
    final Object refChildObject;
    if (args.length > 1) {
        refChildObject = args[1];
    }
    else {
        refChildObject = Undefined.instance;
    }
    Object insertedChild = null;

    if (newChildObject instanceof Node) {
        final Node newChild = (Node) newChildObject;
        final DomNode newChildNode = newChild.getDomNodeOrDie();

        // is the node allowed here?
        if (!isNodeInsertable(newChild)) {
            throw ScriptRuntime.constructError("ReferenceError",
                    "Node cannot be inserted at the specified point in the hierarchy");
        }
        if (newChildNode instanceof DomDocumentFragment) {
            final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
            for (final DomNode child : fragment.getChildren()) {
                if (!isNodeInsertable((Node) child.getScriptableObject())) {
                    throw ScriptRuntime.constructError("ReferenceError",
                            "Node cannot be inserted at the specified point in the hierarchy");
                }
            }
        }

        // extract refChild
        final DomNode refChildNode;
        if (refChildObject == Undefined.instance) {
            if (args.length == 2 || getBrowserVersion().hasFeature(JS_NODE_INSERT_BEFORE_REF_OPTIONAL)) {
                refChildNode = null;
            }
            else {
                throw ScriptRuntime.typeError(
                        "Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present.");
            }
        }
        else if (refChildObject != null) {
            refChildNode = ((Node) refChildObject).getDomNodeOrDie();
        }
        else {
            refChildNode = null;
        }

        final DomNode domNode = getDomNodeOrDie();

        try {
            domNode.insertBefore(newChildNode, refChildNode);
        }
        catch (final org.w3c.dom.DOMException e) {
            throw ScriptRuntime.constructError("ReferenceError", e.getMessage());
        }
        insertedChild = newChild;
    }
    return insertedChild;
}