def getAdjacentTextElement(current, topMostNode, forward=None, traversingUp=False): if forward is None: forward = topMostNode topMostNode = None res = None #print "getAdjacentTextElement", current, topMostNode, forward, traversingUp # If traversingUp, then the children have already been processed if not traversingUp: if DOM.getChildCount(current) > 0: if forward: node = DOM.getFirstChild(current) else: node = DOM.getLastChild(current) if DOM.getNodeType(node) == DOM.TEXT_NODE: res = node else: # Depth first traversal, the recursive call deals with # siblings res = getAdjacentTextElement(node, topMostNode, forward, False) if res is None: if forward: node = current.nextSibling else: node = current.previousSibling # Traverse siblings if node is not None: if DOM.getNodeType(node) == DOM.TEXT_NODE: res = node else: #print node, DOM.getNodeType(node), node.innerHTML # 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 is None) and (not DOM.compare(current, topMostNode)): node = current.parentNode # Stop at document (technically could stop at "html" tag) if (node is not None) and \ (DOM.getNodeType(node) != DOM.DOCUMENT_NODE): res = getAdjacentTextElement(node, topMostNode, forward, True) return res
def findTextPoint(node, offset): """ If the found range is not on a text node, this finds the cooresponding text node to where the selection is. If it is on a text node, just directly creates the endpoint from it. @param node node returned as an endpoint of a range @param offset offset returned to the endpoint of a range @return A range end point with a proper (or None) text node """ if DOM.getNodeType(node) == DOM.TEXT_NODE: res = RangeEndPoint(node, offset) else: # search backwards unless this is after the last node dirn = offset >= DOM.getChildCount(node) child = (DOM.getChildCount(node) == 0) and node or DOM.getChild(node, dirn and (offset - 1) or offset) # Get the previous/next text node text = RangeUtil.getAdjacentTextElement(child, dirn) if text is None: # If we didn't find a text node in the preferred direction, # try the other direction dirn = not dirn text = RangeUtil.getAdjacentTextElement(child, dirn) res = RangeEndPoint(text, dirn) return res
def findTextPoint(node, offset): """ If the found range is not on a text node, this finds the cooresponding text node to where the selection is. If it is on a text node, just directly creates the endpoint from it. @param node node returned as an endpoint of a range @param offset offset returned to the endpoint of a range @return A range end point with a proper (or None) text node """ if DOM.getNodeType(node) == DOM.TEXT_NODE: res = RangeEndPoint(node, offset) else: # search backwards unless this is after the last node dirn = offset >= DOM.getChildCount(node) child = (DOM.getChildCount(node) == 0) and node or \ DOM.getChild(node, dirn and (offset - 1) or offset) # Get the previous/next text node text = RangeUtil.getAdjacentTextElement(child, dirn) if text is None: # If we didn't find a text node in the preferred direction, # try the other direction dirn = not dirn text = RangeUtil.getAdjacentTextElement(child, dirn) res = RangeEndPoint(text, dirn) return res
def findTextPoint(node, offset): if DOM.getNodeType(node) == DOM.TEXT_NODE: res = RangeEndPoint(node, offset) else: # search backwards unless this is after the last node dirn = offset >= DOM.getChildCount(node) child = (DOM.getChildCount(node) == 0) and node or DOM.getChild(node, dirn and (offset - 1) or offset) # Get the previous/next text node text = RangeUtil.getAdjacentTextElement(child, dirn) if text is None: # If we didn't find a text node in the preferred direction, # try the other direction dirn = not dirn text = RangeUtil.getAdjacentTextElement(child, dirn) res = RangeEndPoint(text, dirn) return res
def findTextPoint(node, offset): if DOM.getNodeType(node) == DOM.TEXT_NODE: res = RangeEndPoint(node, offset) else: # search backwards unless this is after the last node dirn = offset >= DOM.getChildCount(node) child = (DOM.getChildCount(node) == 0) and node or \ DOM.getChild(node, dirn and (offset - 1) or offset) # Get the previous/next text node text = RangeUtil.getAdjacentTextElement(child, dirn) if text is None: # If we didn't find a text node in the preferred direction, # try the other direction dirn = not dirn text = RangeUtil.getAdjacentTextElement(child, dirn) res = RangeEndPoint(text, dirn) return res
def isTextNode(node): if node is None: return False return DOM.getNodeType(node) == DOM.TEXT_NODE