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

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getParentNode() . 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-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 2
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 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
/**
 * 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: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void nodeChanged(final DomNode changed, final String attribName) {
    // If a stylesheet was changed, all of our calculations could be off; clear the cache.
    if (changed instanceof HtmlStyle) {
        clearComputedStyles();
        return;
    }
    if (changed instanceof HtmlLink) {
        final String rel = ((HtmlLink) changed).getRelAttribute().toLowerCase(Locale.ROOT);
        if ("stylesheet".equals(rel)) {
            clearComputedStyles();
            return;
        }
    }
    // Apparently it wasn't a stylesheet that changed; be semi-smart about what we evict and when.
    synchronized (computedStyles_) {
        final boolean clearParents = ATTRIBUTES_AFFECTING_PARENT.contains(attribName);
        for (final Iterator<Map.Entry<Element, Map<String, CSS2Properties>>> i
                = computedStyles_.entrySet().iterator(); i.hasNext();) {
            final Map.Entry<Element, Map<String, CSS2Properties>> entry = i.next();
            final DomNode node = entry.getKey().getDomNodeOrDie();
            if (changed == node
                || changed.getParentNode() == node.getParentNode()
                || changed.isAncestorOf(node)
                || clearParents && node.isAncestorOf(changed)) {
                i.remove();
            }
        }
    }
}
 
Example 6
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 7
Source File: XMLDOMNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively checks whether "xml:space" attribute is set to "default".
 * @param node node to start checking from
 * @return {@link Boolean#TRUE} if "default" is set, {@link Boolean#FALSE} for other value,
 *         or null if nothing is set.
 */
private static Boolean isXMLSpaceDefault(DomNode node) {
    for ( ; node instanceof DomElement; node = node.getParentNode()) {
        final String value = ((DomElement) node).getAttribute("xml:space");
        if (!value.isEmpty()) {
            return "default".equals(value);
        }
    }
    return null;
}
 
Example 8
Source File: XMLDOMNode.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively checks whether "xml:space" attribute is set to "default".
 * @param node node to start checking from
 * @return {@link Boolean#TRUE} if "default" is set, {@link Boolean#FALSE} for other value,
 *         or null if nothing is set.
 */
private static Boolean isXMLSpaceDefault(DomNode node) {
    for ( ; node instanceof DomElement; node = node.getParentNode()) {
        final String value = ((DomElement) node).getAttribute("xml:space");
        if (!value.isEmpty()) {
            return "default".equals(value);
        }
    }
    return null;
}
 
Example 9
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private Object getOffsetParentInternal(final boolean returnNullIfFixed) {
    DomNode currentElement = getDomNodeOrDie();

    if (currentElement.getParentNode() == null) {
        return null;
    }

    final HTMLElement htmlElement = currentElement.getScriptableObject();
    if (returnNullIfFixed && "fixed".equals(htmlElement.getStyle().getStyleAttribute(
            StyleAttributes.Definition.POSITION, true))) {
        return null;
    }

    final ComputedCSSStyleDeclaration style = htmlElement.getWindow().getComputedStyle(htmlElement, null);
    final String position = style.getPositionWithInheritance();
    final boolean staticPos = "static".equals(position);

    while (currentElement != null) {

        final DomNode parentNode = currentElement.getParentNode();
        if (parentNode instanceof HtmlBody
            || (staticPos && parentNode instanceof HtmlTableDataCell)
            || (staticPos && parentNode instanceof HtmlTable)) {
            return parentNode.getScriptableObject();
        }

        if (parentNode != null && parentNode.getScriptableObject() instanceof HTMLElement) {
            final HTMLElement parentElement = parentNode.getScriptableObject();
            final ComputedCSSStyleDeclaration parentStyle =
                        parentElement.getWindow().getComputedStyle(parentElement, null);
            final String parentPosition = parentStyle.getPositionWithInheritance();
            if (!"static".equals(parentPosition)) {
                return parentNode.getScriptableObject();
            }
        }

        currentElement = currentElement.getParentNode();
    }

    return null;
}
 
Example 10
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns this element's {@code offsetTop}, which is the calculated top position of this
 * element relative to the {@code offsetParent}.
 *
 * @return this element's {@code offsetTop}
 * @see <a href="http://msdn2.microsoft.com/en-us/library/ms534303.aspx">MSDN Documentation</a>
 * @see <a href="http://www.quirksmode.org/js/elementdimensions.html">Element Dimensions</a>
 * @see <a href="http://dump.testsuite.org/2006/dom/style/offset/spec">Reverse Engineering by Anne van Kesteren</a>
 */
@JsxGetter
public int getOffsetTop() {
    if (this instanceof HTMLBodyElement) {
        return 0;
    }

    int top = 0;
    final HTMLElement offsetParent = getOffsetParent();

    // Add the offset for this node.
    DomNode node = getDomNodeOrDie();
    HTMLElement element = (HTMLElement) node.getScriptableObject();
    ComputedCSSStyleDeclaration style = element.getWindow().getComputedStyle(element, null);
    top += style.getTop(true, false, false);

    // If this node is absolutely positioned, we're done.
    final String position = style.getPositionWithInheritance();
    if ("absolute".equals(position)) {
        return top;
    }

    // Add the offset for the ancestor nodes.
    node = node.getParentNode();
    while (node != null && node.getScriptableObject() != offsetParent) {
        if (node.getScriptableObject() instanceof HTMLElement) {
            element = (HTMLElement) node.getScriptableObject();
            style = element.getWindow().getComputedStyle(element, null);
            top += style.getTop(false, true, true);
        }
        node = node.getParentNode();
    }

    if (offsetParent != null) {
        final HTMLElement thiz = (HTMLElement) getDomNodeOrDie().getScriptableObject();
        style = thiz.getWindow().getComputedStyle(thiz, null);
        final boolean thisElementHasTopMargin = style.getMarginTopValue() != 0;

        style = offsetParent.getWindow().getComputedStyle(offsetParent, null);
        if (!thisElementHasTopMargin) {
            top += style.getMarginTopValue();
        }
        top += style.getPaddingTopValue();
    }

    return top;
}
 
Example 11
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 12
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns this element's <tt>offsetLeft</tt>, which is the calculated left position of this
 * element relative to the <tt>offsetParent</tt>.
 *
 * @return this element's <tt>offsetLeft</tt>
 * @see <a href="http://msdn2.microsoft.com/en-us/library/ms534200.aspx">MSDN Documentation</a>
 * @see <a href="http://www.quirksmode.org/js/elementdimensions.html">Element Dimensions</a>
 * @see <a href="http://dump.testsuite.org/2006/dom/style/offset/spec">Reverse Engineering by Anne van Kesteren</a>
 */
@JsxGetter
public int getOffsetLeft() {
    if (this instanceof HTMLBodyElement) {
        return 0;
    }

    int left = 0;
    final HTMLElement offsetParent = getOffsetParent();

    // Add the offset for this node.
    DomNode node = getDomNodeOrDie();
    HTMLElement element = (HTMLElement) node.getScriptableObject();
    ComputedCSSStyleDeclaration style = element.getWindow().getComputedStyle(element, null);
    left += style.getLeft(true, false, false);

    // If this node is absolutely positioned, we're done.
    final String position = style.getPositionWithInheritance();
    if ("absolute".equals(position)) {
        return left;
    }

    // Add the offset for the ancestor nodes.
    node = node.getParentNode();
    while (node != null && node.getScriptableObject() != offsetParent) {
        if (node.getScriptableObject() instanceof HTMLElement) {
            element = (HTMLElement) node.getScriptableObject();
            style = element.getWindow().getComputedStyle(element, null);
            left += style.getLeft(true, true, true);
        }
        node = node.getParentNode();
    }

    if (offsetParent != null) {
        style = offsetParent.getWindow().getComputedStyle(offsetParent, null);
        left += style.getMarginLeftValue();
        left += style.getPaddingLeftValue();
    }

    return left;
}
 
Example 13
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 14
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 15
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns this element's {@code offsetTop}, which is the calculated top position of this
 * element relative to the {@code offsetParent}.
 *
 * @return this element's {@code offsetTop}
 * @see <a href="http://msdn2.microsoft.com/en-us/library/ms534303.aspx">MSDN Documentation</a>
 * @see <a href="http://www.quirksmode.org/js/elementdimensions.html">Element Dimensions</a>
 * @see <a href="http://dump.testsuite.org/2006/dom/style/offset/spec">Reverse Engineering by Anne van Kesteren</a>
 */
@JsxGetter
public int getOffsetTop() {
    if (this instanceof HTMLBodyElement) {
        return 0;
    }

    int top = 0;
    final HTMLElement offsetParent = getOffsetParent();

    // Add the offset for this node.
    DomNode node = getDomNodeOrDie();
    HTMLElement element = node.getScriptableObject();
    ComputedCSSStyleDeclaration style = element.getWindow().getComputedStyle(element, null);
    top += style.getTop(true, false, false);

    // If this node is absolutely positioned, we're done.
    final String position = style.getPositionWithInheritance();
    if ("absolute".equals(position)) {
        return top;
    }

    // Add the offset for the ancestor nodes.
    node = node.getParentNode();
    while (node != null && node.getScriptableObject() != offsetParent) {
        if (node.getScriptableObject() instanceof HTMLElement) {
            element = node.getScriptableObject();
            style = element.getWindow().getComputedStyle(element, null);
            top += style.getTop(false, true, true);
        }
        node = node.getParentNode();
    }

    if (offsetParent != null) {
        final HTMLElement thiz = getDomNodeOrDie().getScriptableObject();
        style = thiz.getWindow().getComputedStyle(thiz, null);
        final boolean thisElementHasTopMargin = style.getMarginTopValue() != 0;

        style = offsetParent.getWindow().getComputedStyle(offsetParent, null);
        if (!thisElementHasTopMargin) {
            top += style.getMarginTopValue();
        }
        top += style.getPaddingTopValue();
    }

    return top;
}
 
Example 16
Source File: HTMLTableElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Indicates if the row belongs to this container.
 * @param row the row to test
 * @return {@code true} if it belongs to this container
 */
@Override
protected boolean isContainedRow(final HtmlTableRow row) {
    final DomNode parent = row.getParentNode(); // the tbody, thead or tfoo
    return (parent != null) && parent.getParentNode() == getDomNodeOrDie();
}
 
Example 17
Source File: HTMLTableElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Indicates if the row belongs to this container.
 * @param row the row to test
 * @return {@code true} if it belongs to this container
 */
@Override
protected boolean isContainedRow(final HtmlTableRow row) {
    final DomNode parent = row.getParentNode(); // the tbody, thead or tfoo
    return (parent != null) && parent.getParentNode() == getDomNodeOrDie();
}
 
Example 18
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
public synchronized void nodeChanged(final DomNode changed, final boolean clearParents) {
    final Iterator<Map.Entry<Element, Map<String, CSS2Properties>>> i = computedStyles_.entrySet().iterator();
    while (i.hasNext()) {
        final Map.Entry<Element, Map<String, CSS2Properties>> entry = i.next();
        final DomNode node = entry.getKey().getDomNodeOrDie();
        if (changed == node
            || changed.getParentNode() == node.getParentNode()
            || changed.isAncestorOf(node)
            || clearParents && node.isAncestorOf(changed)) {
            i.remove();
        }
    }

    // maybe this is a better solution but i have to think a bit more about this
    //
    //            if (computedStyles_.isEmpty()) {
    //                return;
    //            }
    //
    //            // remove all siblings
    //            DomNode parent = changed.getParentNode();
    //            if (parent != null) {
    //                for (DomNode sibling : parent.getChildNodes()) {
    //                    computedStyles_.remove(sibling.getScriptableObject());
    //                }
    //
    //                if (clearParents) {
    //                    // remove all parents
    //                    while (parent != null) {
    //                        computedStyles_.remove(parent.getScriptableObject());
    //                        parent = parent.getParentNode();
    //                    }
    //                }
    //            }
    //
    //            // remove changed itself and all descendants
    //            computedStyles_.remove(changed.getScriptableObject());
    //            for (DomNode descendant : changed.getDescendants()) {
    //                computedStyles_.remove(descendant.getScriptableObject());
    //            }
}
 
Example 19
Source File: HTMLElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private Object getOffsetParentInternal(final boolean returnNullIfFixed) {
    DomNode currentElement = getDomNodeOrDie();

    if (currentElement.getParentNode() == null) {
        return null;
    }

    Object offsetParent = null;
    final HTMLElement htmlElement = (HTMLElement) currentElement.getScriptableObject();
    if (returnNullIfFixed && "fixed".equals(htmlElement.getStyle().getStyleAttribute(
            StyleAttributes.Definition.POSITION, true))) {
        return null;
    }

    final ComputedCSSStyleDeclaration style = htmlElement.getWindow().getComputedStyle(htmlElement, null);
    final String position = style.getPositionWithInheritance();
    final boolean staticPos = "static".equals(position);

    final boolean useTables = staticPos;

    while (currentElement != null) {

        final DomNode parentNode = currentElement.getParentNode();
        if (parentNode instanceof HtmlBody
            || (useTables && parentNode instanceof HtmlTableDataCell)
            || (useTables && parentNode instanceof HtmlTable)) {
            offsetParent = parentNode.getScriptableObject();
            break;
        }

        if (parentNode != null && parentNode.getScriptableObject() instanceof HTMLElement) {
            final HTMLElement parentElement = (HTMLElement) parentNode.getScriptableObject();
            final ComputedCSSStyleDeclaration parentStyle =
                        parentElement.getWindow().getComputedStyle(parentElement, null);
            final String parentPosition = parentStyle.getPositionWithInheritance();
            final boolean parentIsStatic = "static".equals(parentPosition);
            if (!parentIsStatic) {
                offsetParent = parentNode.getScriptableObject();
                break;
            }
        }

        currentElement = currentElement.getParentNode();
    }

    return offsetParent;
}
 
Example 20
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);
}