/** * First try sibling, if it retruns null, try search neighbor. * * @param parent * @param childIndex * @param forward * @return the node */ Node getNeighbor(Node parent, int childIndex, boolean forward) { if (!EditValidateUtil.validNode(parent)) { return null; } NodeList nodeList = parent.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { if (nodeList.getLength() < childIndex) { return null; } Node childNode = null; if (!forward) { --childIndex; } childNode = nodeList.item(childIndex); if (childNode != null) { return childNode; } return getNeighbor(parent, forward); } if (parent.getNodeType() == Node.TEXT_NODE) { return getNeighbor(parent, forward); } return null; }
/** * Get node's sibling according to 'forward' direction * * @param node * @param forward * @return the node */ public Node getSibling(Node node, boolean forward) { EditValidateUtil.validNode(node); if (forward) { return node.getNextSibling(); } return node.getPreviousSibling(); }
/** * Get lowest ancestor of a 'node' which is block type. * * @param node * @return the node */ Node getBlockAncestor(Node node) { if (!EditValidateUtil.validNode(node)) { return null; } while (node != null && isChild(IHTMLConstants.TAG_BODY, node, true)) { if (isBlockNode(node)) { return node; } node = node.getParentNode(); } return null; }
/** * Get node index in its parent's children. * * @param node * @return the node index or -1 if not found */ static int getNodeIndex(Node node) { EditValidateUtil.validNode(node); Node parent = node.getParentNode(); int index = 0; for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child == node) { return index; } index++; } return -1; // error }
/** * Get previous sibling, or if sibling is null then get previous neighbor. * * @param node * @return the node */ public Node getPreviousNeighbor(Node node) { if (!EditValidateUtil.validNode(node)) { return null; } while (node != null && node.getNodeType() != Node.DOCUMENT_NODE && node.getPreviousSibling() == null) { node = node.getParentNode(); } return (node != null && node.getNodeType() != Node.DOCUMENT_NODE) ? node.getPreviousSibling() : null; }
/** * If parent has more than one children of which each node's localName is the same as of 'node', * return the index of 'node' in the same type children list. * * @param node * @return the index */ public int getSameTypeNodeIndex(Node node) { EditValidateUtil.validNode(node); int i = 0; while (node != null) { Node sibling = node.getPreviousSibling(); if (sibling != null && sibling.getLocalName() != null && sibling.getLocalName().equals(node.getLocalName())) { i++; } node = sibling; } return i; // error }
/** * Get neighbor which is descendent of root. * * @param node * @param root * @return the node */ Node getNextNeighbor(Node node, Node root) { if (!EditValidateUtil.validNode(node)) { return null; } while (node != null && node != root && node.getNodeType() != Node.DOCUMENT_NODE && node.getNextSibling() == null) { node = node.getParentNode(); } return (node != null && node != root && node.getNodeType() != Node.DOCUMENT_NODE) ? node.getNextSibling() : null; }
/** * Return 'node' indexed end name' start position Example: <a>aaa| </a>, the position is indicated * by '|' If node is <a /> style or there is no </a> to pair with <a>, the function return -1. * * @param node * @return the start index */ public static int getNodeEndNameStartIndex(Node node) { if (isText(node)) { return getNodeEndIndex(node); } if (EditValidateUtil.validNode(node) && node instanceof IDOMNode) { IStructuredDocumentRegion region = ((IDOMNode) node).getEndStructuredDocumentRegion(); if (region != null) { return region.getStartOffset(); } // else // { // if (node.hasChildNodes()) // { // return getNodeEndIndex(node); // } // } } return getNodeEndIndex(node); }
/** * Return 'node' indexed start name's end position Example: <a>|aaa </a>, the position is * indicated by '|' * * @param node * @return the index */ public static int getNodeStartNameEndIndex(Node node) { if (isText(node)) { return getNodeStartIndex(node); } if (EditValidateUtil.validNode(node) && node instanceof IDOMNode) { IStructuredDocumentRegion region = ((IDOMNode) node).getStartStructuredDocumentRegion(); if (region != null) { return region.getEndOffset(); } // else // { // // if (node.hasChildNodes()) // // { // // // Node should always have start name, so this part should // never reach, // // // the assert is for inner debug. // // Assert.isTrue(false); // // return getNodeStartIndex(node); // // } // } } // This should never happen. return getNodeStartIndex(node); }
/** * Return 'node' indexed end position Example: <a></a>|, the position is indicated by '|' * * @param node * @return the end index */ public static int getNodeEndIndex(Node node) { if (EditValidateUtil.validNode(node) && node instanceof IndexedRegion) { return ((IndexedRegion) node).getEndOffset(); } return -1; }
/** * Calculate node's Indexed length in model. * * @param node * @return the node length */ public static int getNodeLenth(Node node) { if (node != null && EditValidateUtil.validNode(node)) { return ((IndexedRegion) node).getEndOffset() - ((IndexedRegion) node).getStartOffset(); } return 0; }