Example usage for com.google.gwt.dom.client Node getPreviousSibling

List of usage examples for com.google.gwt.dom.client Node getPreviousSibling

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Node getPreviousSibling.

Prototype

@Override
    public Node getPreviousSibling() 

Source Link

Usage

From source file:cc.alcina.framework.gwt.client.util.WidgetUtils.java

License:Apache License

public static Element getElementForPositioning0(Element from) {
    assert tempPositioningText == null;
    if (!isVisibleAncestorChain(from)) {
        return null;
    }//from   w w  w  .j  a  v a  2s  .  co  m
    boolean hidden = isZeroOffsetDims(from);
    int kidCount = from.getChildCount();
    if (kidCount != 0 && !hidden) {
        return from;
    }
    Node parent = from.getParentNode();
    if (parent != null && parent.getFirstChild() == from && parent.getNodeType() == Node.ELEMENT_NODE
            && !isZeroOffsetDims((Element) parent)) {
        return (Element) parent;
    }
    ClientNodeIterator itr = new ClientNodeIterator(from, ClientNodeIterator.SHOW_ALL);
    Element fromContainingBlock = DomUtils.getContainingBlock(from);
    Node node = from;
    int insertTextIfOffsetMoreThanXChars = 100;
    while ((node = node.getPreviousSibling()) != null) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            insertTextIfOffsetMoreThanXChars -= TextUtils.normalizeWhitespaceAndTrim(node.getNodeValue())
                    .length();
            if (insertTextIfOffsetMoreThanXChars < 0) {
                // this causes a relayout - so we try and avoid. most of the
                // time, positioning elements will contain text (or be from
                // a friendly browser), or be at the start of a block elt)
                tempPositioningText = Document.get().createTextNode("---");
                from.appendChild(tempPositioningText);
                return from;
            }
        }
    }
    // give up after 50 node iterations (big tables maybe)
    int max = 50;
    while ((node = itr.nextNode()) != null && max-- > 0) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (!isZeroOffsetDims(node.getParentElement()) && node.getNodeName().equalsIgnoreCase("img")) {
                return (Element) node;
            }
            if (!UIObject.isVisible((Element) node)) {
                itr.skipChildren();
            }
        } else {
            // text
            if (!isZeroOffsetDims(node.getParentElement())
                    // we don't want the combined ancestor of everyone...
                    && (!node.getParentElement().isOrHasChild(from) ||
                    // but we do want <p><a><b>*some-text*</b></p>
                            DomUtils.getContainingBlock(node) == fromContainingBlock)) {
                return node.getParentElement();
            }
        }
    }
    return from.getParentElement();
}

From source file:client.net.sf.saxon.ce.dom.HTMLNodeWrapper.java

License:Mozilla Public License

/**
 * Get the index position of this node among its siblings (starting from 0).
 * In the case of a text node that maps to several adjacent siblings in the DOM,
 * the numbering actually refers to the position of the underlying DOM nodes;
 * thus the sibling position for the text node is that of the first DOM node
 * to which it relates, and the numbering of subsequent XPath nodes is not necessarily
 * consecutive.//  w w  w  .  j a  v  a2  s  . c o m
 */

public int getSiblingPosition() {
    if (index == -1) {
        switch (nodeKind) {
        case Type.ELEMENT:
        case Type.TEXT:
        case Type.COMMENT:
        case Type.PROCESSING_INSTRUCTION:
            int ix = 0;
            Node start = node;
            while (true) {
                start = start.getPreviousSibling();
                if (start == null) {
                    index = ix;
                    return ix;
                }
                ix++;
            }
        case Type.ATTRIBUTE:
            ix = 0;
            int fp = getFingerprint();
            AxisIterator iter = parent.iterateAxis(Axis.ATTRIBUTE);
            while (true) {
                NodeInfo n = (NodeInfo) iter.next();
                if (n == null || n.getFingerprint() == fp) {
                    index = ix;
                    return ix;
                }
                ix++;
            }

        case Type.NAMESPACE:
            ix = 0;
            fp = getFingerprint();
            iter = parent.iterateAxis(Axis.NAMESPACE);
            while (true) {
                NodeInfo n = (NodeInfo) iter.next();
                if (n == null || n.getFingerprint() == fp) {
                    index = ix;
                    return ix;
                }
                ix++;
            }
        default:
            index = 0;
            return index;
        }
    }
    return index;
}

From source file:com.alkacon.acacia.client.ui.AttributeValueView.java

License:Open Source License

/**
 * Returns the attribute value index.<p>
 * //from   w ww  . j a v  a2  s . co m
 * @return the attribute value index
 */
public int getValueIndex() {

    int result = 0;
    Node previousSibling = getElement().getPreviousSibling();
    while (previousSibling != null) {
        result++;
        previousSibling = previousSibling.getPreviousSibling();
    }
    return result;
}

From source file:com.bfr.client.selection.Range.java

License:Apache License

/**
 * Returns the next adjacent text node in the given direction.  Will move
 * down the hierarchy (if traversingUp is not set), then through siblings,
 * then up (but not past topMostNode), looking for the first node
 * <p/>//from w  ww  . j a  va2  s  .  co m
 * This could be non-statically included in the Node class
 *
 * @param current      An element to start the search from, can be any type
 *                     of node.
 * @param topMostNode  A node that this will traverse no higher than
 * @param forward      whether to search forward or backward
 * @param traversingUp if true, will not look at the children of this element
 * @return the next (previous) text node, or null if no more
 */
public static Text getAdjacentTextElement(Node current, Node topMostNode, boolean forward,
        boolean traversingUp) {
    Text res = null;
    Node node;

    // If traversingUp, then the children have already been processed
    if (!traversingUp) {
        if (current.getChildCount() > 0) {
            node = forward ? current.getFirstChild() : current.getLastChild();

            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    if (res == null) {
        node = forward ? current.getNextSibling() : current.getPreviousSibling();
        // Traverse siblings
        if (node != null) {
            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    // Go up and over if still not found
    if ((res == null) && (current != topMostNode)) {
        node = current.getParentNode();
        // Stop at document (technically could stop at "html" tag)
        if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE)) {
            res = getAdjacentTextElement(node, topMostNode, forward, true);
        }
    }

    return res;
}

From source file:com.vaadin.client.WidgetUtil.java

License:Apache License

/**
 * Returns the index of the childElement within its parent.
 * //from  w w w  .  j a v  a 2s . co m
 * @param subElement
 * @return
 */
public static int getChildElementIndex(Element childElement) {
    int idx = 0;
    Node n = childElement;
    while ((n = n.getPreviousSibling()) != null) {
        idx++;
    }

    return idx;
}

From source file:de.catma.ui.client.ui.tagger.editor.LeafFinder.java

License:Open Source License

private Node ascentLeft(Node node) {
    if ((node != null) && (!node.equals(upperBorderNode))) {
        Node sibling = node.getPreviousSibling();
        if (sibling == null) {
            return ascentLeft(node.getParentNode());
        } else {/*ww w . j a  va2 s  .co  m*/
            return sibling;
        }
    }
    return null;
}

From source file:org.chromium.distiller.PageParameterParser.java

License:Open Source License

/**
 * Finds and adds the leaf node(s) closest to the given start node.
 * This recurses and keeps finding and, if necessary, adding the numeric text of valid nodes,
 * collecting the PageParamInfo.PageInfo's for the current adjacency group.
 * For backward search, i.e. nodes before start node, search terminates (i.e. recursion stops)
 * once a text node or anchor is encountered.  If the text node contains numeric text, it's
 * added to the current adjacency group.  Otherwise, a new group is created to break the
 * adjacency.//from w ww  . j  a  v a2 s.  co m
 * For forward search, i.e. nodes after start node, search continues (i.e. recursion continues)
 * until a text node or anchor with non-numeric text is encountered.  In the process, text nodes
 * and anchors with numeric text are added to the current adjacency group.  When a non-numeric
 * text node or anchor is encountered, a new group is started to break the adjacency, and search
 * ends.
 *
 * @return true to continue search, false to stop.
 *
 * @param start node to work on.
 * @param checkStart true to check start node.  Otherwise, the previous or next sibling of the
 * start node is checked.
 * @param backward true to search backward (i.e. nodes before start node), false to search
 * forward (i.e. nodes after start node).
 * @param baseAnchor created for the current document, only needed for forward search.
 */
private boolean findAndAddClosestValidLeafNodes(Node start, boolean checkStart, boolean backward,
        AnchorElement baseAnchor) {
    Node node = checkStart ? start : (backward ? start.getPreviousSibling() : start.getNextSibling());
    if (node == null) { // No sibling, try parent.
        node = start.getParentNode();
        if (sInvalidParentWrapper == null) {
            sInvalidParentWrapper = RegExp.compile("(BODY)|(HTML)");
        }
        if (sInvalidParentWrapper.test(node.getNodeName()))
            return false;
        return findAndAddClosestValidLeafNodes(node, false, backward, baseAnchor);
    }

    checkStart = false;
    switch (node.getNodeType()) {
    case Node.TEXT_NODE:
        String text = node.getNodeValue();
        // Text must contain words.
        if (text.isEmpty() || StringUtil.countWords(text) == 0)
            break;
        boolean added = addNonLinkTextIfValid(node.getNodeValue());
        // For backward search, we're done regardless if text was added.
        // For forward search, we're done only if text was invalid, otherwise continue.
        if (backward || !added)
            return false;
        break;

    case Node.ELEMENT_NODE:
        Element e = Element.as(node);
        if (e.hasTagName("A")) {
            // For backward search, we're done because we've already processed the anchor.
            if (backward)
                return false;
            // For forward search, we're done only if link was invalid, otherwise continue.
            mNumForwardLinksProcessed++;
            if (!addLinkIfValid(AnchorElement.as(e), baseAnchor))
                return false;
            break;
        }
        // Intentionally fall through.

    default:
        // Check children nodes.
        if (!node.hasChildNodes())
            break;
        checkStart = true; // We want to check the child node.
        if (backward) {
            // Start the backward search with the rightmost child i.e. last and closest to
            // given node.
            node = node.getLastChild();
        } else {
            // Start the forward search with the leftmost child i.e. first and closest to
            // given node.
            node = node.getFirstChild();
        }
        break;
    }

    return findAndAddClosestValidLeafNodes(node, checkStart, backward, baseAnchor);
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.Utils.java

License:Apache License

public static String removeWhitespaces(String text, Node node, boolean forceIfOnlyWhitespaces) {
    if (text == null) {
        return "";
    }//from  w ww.  j  a v a  2  s.c o m
    if (!forceIfOnlyWhitespaces) {
        if (text.matches("^\\s+$")) {
            return text;
        }
    }
    // Window.alert("Before removeWS: " + text);
    Element prevSibling = (Element) node.getPreviousSibling();

    String processedText = text;
    if (prevSibling == null
            || !(new CSSClassManager(prevSibling).isClassPresent(AnnotationConstant.IGNORED_ELEMENT))) {
        processedText = processedText.replaceAll("^\\s+", "");
    }
    // Window.alert("in progress removeWS: " + processedText);
    processedText = processedText.replaceAll("\\s+", " ");
    // Window.alert("after removeWS: " + processedText);
    return processedText;
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.XPathUtil.java

License:Apache License

public String getXPath(Node node) {
    Log.debug("XPathUtil] node: " + node.getNodeName() + " parent node: " + node.getParentNode().getNodeName());
    Document document = node.getOwnerDocument();
    Node current = node;
    StringBuilder xpath = new StringBuilder();
    while (!current.equals(document)) {
        int counter = 1;
        String name = current.getNodeName();
        while (current.getPreviousSibling() != null) {
            if (current.getPreviousSibling().getNodeName().equalsIgnoreCase(name)
                    && !isIgnored(current.getPreviousSibling())) {
                counter++;/*from   w  w w.  j av a  2 s . c o  m*/
            }
            current = current.getPreviousSibling();
        }
        xpath.insert(0, "/" + name.toLowerCase() + "[" + counter + "]");
        current = current.getParentNode();
    }
    Log.debug("XPathUtil] xpath: " + xpath);
    return xpath.toString();
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.TextAnnotater.java

License:Apache License

private Container getCustomContainer(Node node, int currentOffset) {
    int offset = 0;
    Node n = node.getPreviousSibling();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text text = (Text) n;
            offset += text.getLength();/*from  w w  w . j  av  a 2  s . c  om*/
        } else if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element ele = (Element) n;
            offset += ele.getInnerText().length();
        }
        n = n.getPreviousSibling();
    }
    node = node.getParentNode();
    currentOffset += offset;
    return new Container(xpathUtil.getXPath(node), currentOffset);
}