List of usage examples for com.google.gwt.dom.client Node getParentNode
@Override
public Node getParentNode()
From source file:com.bfr.client.selection.HtmlBBox.java
License:Apache License
/** * Determine the index of a node within its parent * * @param child A node to determine the index of * @return index of the node, or -1 on failure */// w ww . j a v a 2 s. c om public static int getChildIndex(Node child) { int res = -1; Node parent = child.getParentNode(); if (parent != null) { for (int i = 0; i < parent.getChildCount(); i++) { if (child == parent.getChild(i)) { res = i; break; } } } return res; }
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/>// w w w . java2 s . c o 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.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private static void getDescendantNodes(JsNodeArray matchingElms, String nextTagStr, Node prevRef) { NodeList<Element> children = getElementsByTagName(nextTagStr, prevRef); for (int k = 0, klen = children.getLength(); k < klen; k++) { Node child = children.getItem(k); if (child.getParentNode() == prevRef) { matchingElms.addNode(child); }/* ww w . j a v a2 s. c o m*/ } }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private JsNodeArray getNthChildPseudo(JsNodeArray previousMatch, String pseudoValue, JsNodeArray prevParents, JsNodeArray matchingElms) {/* w w w . ja v a 2s . com*/ Node previous; if (JsUtils.eq(pseudoValue, "n")) { matchingElms = previousMatch; } else { Sequence sequence = getSequence(pseudoValue); if (sequence != null) { for (int l = 0, llen = previousMatch.size(); l < llen; l++) { previous = previousMatch.getNode(l); Node prevParent = previous.getParentNode(); if (!hasChildElms(prevParent)) { int iteratorNext = sequence.start; int childCount = 0; Node childElm = prevParent.getFirstChild(); while (childElm != null && (sequence.max < 0 || iteratorNext <= sequence.max)) { if (childElm.getNodeType() == Node.ELEMENT_NODE) { if (++childCount == iteratorNext) { if (JsUtils.eq(childElm.getNodeName(), previous.getNodeName())) { matchingElms.addNode(childElm); } iteratorNext += sequence.add; } } childElm = SelectorEngine.getNextSibling(childElm); } setHasChildElms(prevParent, true); prevParents.addNode(prevParent); } } clearChildElms(prevParents); } } return matchingElms; }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private JsNodeArray getNthOfTypePseudo(JsNodeArray previousMatch, String pseudoValue, JsNodeArray prevParents, JsNodeArray matchingElms) {/*from www . j ava2 s .c o m*/ Node previous; if (pseudoValue.startsWith("n")) { matchingElms = previousMatch; } else { Sequence sequence = getSequence(pseudoValue); if (sequence != null) { for (int p = 0, plen = previousMatch.size(); p < plen; p++) { previous = previousMatch.getNode(p); Node prevParent = previous.getParentNode(); if (!hasChildElms(prevParent)) { int iteratorNext = sequence.start; int childCount = 0; Node childElm = prevParent.getFirstChild(); while (JsUtils.truth(childElm) && (sequence.max < 0 || iteratorNext <= sequence.max)) { if (JsUtils.eq(childElm.getNodeName(), previous.getNodeName())) { if (++childCount == iteratorNext) { matchingElms.addNode(childElm); iteratorNext += sequence.add; } } childElm = SelectorEngine.getNextSibling(childElm); } setHasChildElms(prevParent, true); prevParents.addNode(prevParent); } } clearChildElms(prevParents); } } return matchingElms; }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private void getOnlyChildPseudo(JsNodeArray previousMatch, JsNodeArray matchingElms) { Node previous; Node next;//from w ww . j a v a2 s .c o m Node prev; Node kParent = null; for (int k = 0, klen = previousMatch.size(); k < klen; k++) { prev = next = previous = previousMatch.getNode(k); Node prevParent = previous.getParentNode(); if (prevParent != kParent) { while (JsUtils.truth(prev = SelectorEngine.getPreviousSibling(prev)) && prev.getNodeType() != Node.ELEMENT_NODE) { } while (JsUtils.truth(next = SelectorEngine.getNextSibling(next)) && next.getNodeType() != Node.ELEMENT_NODE) { } if (!JsUtils.truth(prev) && !JsUtils.truth(next)) { matchingElms.addNode(previous); } kParent = prevParent; } } }
From source file:com.cgxlib.xq.client.impl.research.SelectorEngineJS.java
License:Apache License
private void getOnlyOfTypePseudo(JsNodeArray previousMatch, JsNodeArray matchingElms) { Node previous; Node next;/*from w w w . jav a 2 s .com*/ Node prev; Node oParent = null; for (int o = 0, olen = previousMatch.size(); o < olen; o++) { prev = next = previous = previousMatch.getNode(o); Node prevParent = previous.getParentNode(); if (prevParent != oParent) { while (JsUtils.truth(prev = SelectorEngine.getPreviousSibling(prev)) && !JsUtils.eq(prev.getNodeName(), previous.getNodeName())) { } while (JsUtils.truth(next = SelectorEngine.getNextSibling(next)) && !JsUtils.eq(next.getNodeName(), previous.getNodeName())) { } if (!JsUtils.truth(prev) && !JsUtils.truth(next)) { matchingElms.addNode(previous); } oParent = prevParent; } } }
From source file:com.dom_distiller.client.DomUtil.java
License:Open Source License
/** * Get a list of all the parents of this node starting with the node itself. * @param n The node to get the parents of. * @return A list of the provided node's partnts. *//* w w w . j av a 2s . c o m*/ public static List<Node> getParentNodes(Node n) { ArrayList<Node> result = new ArrayList<Node>(); Node curr = n; while (curr != null) { result.add(curr); curr = curr.getParentNode(); } return result; }
From source file:com.dom_distiller.client.DomWalker.java
License:Open Source License
/** * Walk the subtree rooted at n.// w w w. ja v a 2 s. com */ public void walk(Node top) { // Conceptually, this maintains a pointer to the currently "walked" node. When first seeing // the node, it calls visit() on it. The next node to visit is then (1) the first child, (2) // the next sibling, or (3) the next sibling of the first ancestor w/ a next sibling. // // Every time the walk "crosses" the "exit" of a node (i.e. when the pointer goes from // somewhere in the node's subtree to somewhere outside of that subtree), exit() is called // for that node (unless visit() for that node returned false). if (!visitor.visit(top)) return; Node n = top.getFirstChild(); if (n != null) { while (n != top) { // shouldExit is used to suppress the exit call for the current node when visit() // returns false. boolean shouldExit = false; if (visitor.visit(n)) { Node c = n.getFirstChild(); if (c != null) { n = c; continue; } shouldExit = true; } while (n != top) { if (shouldExit) visitor.exit(n); Node s = n.getNextSibling(); if (s != null) { n = s; break; } n = n.getParentNode(); shouldExit = true; } } } visitor.exit(top); }
From source file:com.dom_distiller.client.NodeListExpander.java
License:Open Source License
private static Node getTopNode(Node n) { for (Node next = n.getParentNode(); next != null && next.getNodeType() != Node.DOCUMENT_NODE; n = next, next = n.getParentNode()) { }// w w w .j a v a2 s . c o m return n; }