def setRange(self, arg1, arg2=None): """ Sets the range to encompass the given element. May not work around non-text containing elements. @param element Element to surround by this range @return whether a range can be placed around this element. Set the range to be between the two given points. Both points must be within the same document, and end must come after start. @param startPoint Start point to set the range to @param endPoint End point to set the range to """ if arg2 is None: firstText = RangeUtil.getAdjacentTextElement( arg1, arg1, True, False) lastText = RangeUtil.getAdjacentTextElement( arg1, arg1, False, False) if (firstText is None) or (lastText is None): return False startPoint = RangeEndPoint(firstText, 0) endPoint = RangeEndPoint(lastText, lastText.length) else: startPoint = arg1 endPoint = arg2 assert (startPoint.getNode().ownerDocument == endPoint.getNode().ownerDocument) self._setRange(startPoint, endPoint) self.m_range = None
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 setRange(self, arg1, arg2=None): """ Sets the range to encompass the given element. May not work around non-text containing elements. @param element Element to surround by this range @return whether a range can be placed around this element. Set the range to be between the two given points. Both points must be within the same document, and end must come after start. @param startPoint Start point to set the range to @param endPoint End point to set the range to """ if arg2 is None: firstText = RangeUtil.getAdjacentTextElement(arg1, arg1, True, False) lastText = RangeUtil.getAdjacentTextElement(arg1, arg1, False, False) if (firstText is None) or (lastText is None): return False startPoint = RangeEndPoint(firstText, 0) endPoint = RangeEndPoint(lastText, lastText.length) else: startPoint = arg1 endPoint = arg2 assert startPoint.getNode().ownerDocument == endPoint.getNode().ownerDocument self._setRange(startPoint, endPoint) self.m_range = None
def getSelectedTextElements(self, startNode=None, endNode=None): """ Returns all text nodes between (and including) two arbitrary text nodes. Caller must ensure startNode comes before endNode. @param startNode start node to traverse @param endNode end node to finish traversal @return A list of all text nodes between these two text nodes """ if startNode is None and endNode is None: startNode = self.m_startPoint.getTextNode() endNode = self.m_endPoint.getTextNode() res = [] #print "getSelectedTextElements", startNode, endNode current = startNode while (current is not None) and (not DOM.compare(current, endNode)): res.append(current) current = RangeUtil.getAdjacentTextElement(current, None, True, False) if current is None: # With the old way this could have been backwards, but should not # happen now, so this is an error res = None else: res.append(current) return res
def setElement(self, element, start=None): if start is None: self.m_node = element else: text = RangeUtil.getAdjacentTextElement(element, element, start, False) self.setTextNode(text, start)
def getSelectedTextElements(self, startNode=None, endNode=None): """ Returns all text nodes between (and including) two arbitrary text nodes. Caller must ensure startNode comes before endNode. @param startNode start node to traverse @param endNode end node to finish traversal @return A list of all text nodes between these two text nodes """ if startNode is None and endNode is None: startNode = self.m_startPoint.getTextNode() endNode = self.m_endPoint.getTextNode() res = [] # print "getSelectedTextElements", startNode, endNode current = startNode while (current is not None) and (not DOM.compare(current, endNode)): res.append(current) current = RangeUtil.getAdjacentTextElement(current, None, True, False) if current is None: # With the old way this could have been backwards, but should not # happen now, so this is an error res = None else: res.append(current) 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 setRange(self, arg1, arg2=None): if arg2 is None: firstText = RangeUtil.getAdjacentTextElement(arg1, arg1, True, False) lastText = RangeUtil.getAdjacentTextElement(arg1, arg1, False, False) if (firstText is None) or (lastText is None): return False startPoint = RangeEndPoint(firstText, 0) endPoint = RangeEndPoint(lastText, lastText.length) else: startPoint = arg1 endPoint = arg2 assert startPoint.getNode().ownerDocument == endPoint.getNode().ownerDocument self._setRange(startPoint, endPoint) self.m_range = None
def minimizeBoundaryTextNodes(self, asStart): text = self.getTextNode() if (text is not None) and (self.m_offset == (asStart and text.getLength() or 0)): nxt = RangeUtil.getAdjacentTextElement(text, asStart) if nxt is not None: self.setTextNode(nxt) if asStart: self.m_offset = 0 else: self.m_offset = nxt.getLength()
def setRange(self, arg1, arg2=None): if arg2 is None: firstText = RangeUtil.getAdjacentTextElement(arg1, arg1, True, False) lastText = RangeUtil.getAdjacentTextElement(arg1, arg1, False, False) if (firstText is None) or (lastText is None): return False startPoint = RangeEndPoint(firstText, 0) endPoint = RangeEndPoint(lastText, lastText.length) else: startPoint = arg1 endPoint = arg2 assert (startPoint.getNode().ownerDocument == endPoint.getNode().ownerDocument) self._setRange(startPoint, endPoint) self.m_range = None
def setElement(self, element, start=None): """ Set the range end point at the start or end of an element. The actual selection will occur at the first/last text node within this element. @param element element to set this end point in @param start whether to make the end point at the start or the end """ if start is None: self.m_node = element else: text = RangeUtil.getAdjacentTextElement(element, element, start, False) self.setTextNode(text, start)
def minimizeBoundaryTextNodes(self, asStart): """ If the offset occurs at the beginning/end of the text node, potentially move to the end/beginning of the next/previous text node, to remove text nodes where 0 characters are actually used. If asStart is True then move a cursor at the end of a text node to the beginning of the next vice versa for False. @param asStart Whether to do this as a start or end range point """ text = self.getTextNode() if (text is not None) and (self.m_offset == (asStart and text.getLength() or 0)): nxt = RangeUtil.getAdjacentTextElement(text, asStart) if nxt is not None: self.setTextNode(nxt) if asStart: self.m_offset = 0 else: self.m_offset = nxt.getLength()
def getSelectedTextElements(self, startNode=None, endNode=None): if startNode is None and endNode is None: startNode = self.m_startPoint.getTextNode() endNode = self.m_endPoint.getTextNode() res = [] #print "getSelectedTextElements", startNode, endNode current = startNode while (current is not None) and (not DOM.compare(current, endNode)): res.append(current) current = RangeUtil.getAdjacentTextElement(current, None, True, False) if current is None: # With the old way this could have been backwards, but should not # happen now, so this is an error res = None else: res.append(current) return res
def move(self, forward, topMostNode, limit, type, count): """ TODO IMPLEMENTED ONLY FOR CHARACTER Move the end point forwards or backwards by one unit of type, such as by a word. @param forward True if moving forward @param topMostNode top node to not move past, or None @param limit an endpoint not to move past, or None @param type what unit to move by, ie MOVE_CHARACTER or MOVE_WORD @param count how many of these to move by @return how far this actually moved """ res = 0 limitText = limit and limit.getTextNode() curr = getTextNode() if curr is not None: last = curr offset = getOffset() if type == MOVE_CHARACTER: while curr is not None: last = curr len = forward and (curr.getLength() - offset) or offset if curr == limitText: # If there is a limiting endpoint, may not be able to # go as far len = forward and (limit.getOffset() - offset) or \ (offset - limit.getOffset()) if (len + res) < count: res += len if curr == limitText: break else: # Finis len = count - res offset = forward and (offset + len) or (offset - len) res = count break while True: # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, forward, False) if (curr is None) or (curr.getLength() != 0): break if curr is not None: if forward: offset = 0 else: offset = curr.getLength() # case MOVE_WORDSTART: # case MOVE_WORDEND: # if c_wsRexp is None: # setWhitespaceRexp(DEFAULT_WS_REXP) # # # while curr is not None: # # do { # # Next node, skipping any 0-length texts # curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, # forward, False) # while ((curr is not None) and (curr.getLength() == 0)) # # if curr is not None: # offset = forward ? 0 : curr.getLength() # # # break else: assert(False) self.setTextNode(last) self.setOffset(offset) return res
def move(self, forward, topMostNode, limit, type, count): res = 0 limitText = limit and limit.getTextNode() curr = getTextNode() if curr is not None: last = curr offset = getOffset() if type == MOVE_CHARACTER: while curr is not None: last = curr len = forward and (curr.getLength() - offset) or offset if curr == limitText: # If there is a limiting endpoint, may not be able to # go as far len = forward and (limit.getOffset() - offset) or \ (offset - limit.getOffset()) if (len + res) < count: res += len if curr == limitText: break else: # Finis len = count - res offset = forward and (offset + len) or (offset - len) res = count break while True: # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement( curr, topMostNode, forward, False) if (curr is None) or (curr.getLength() != 0): break if curr is not None: if forward: offset = 0 else: offset = curr.getLength() """ case MOVE_WORDSTART: case MOVE_WORDEND: if c_wsRexp is None: setWhitespaceRexp(DEFAULT_WS_REXP) while curr is not None: do { # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, forward, False) while ((curr is not None) and (curr.getLength() == 0)) if curr is not None: offset = forward ? 0 : curr.getLength() break """ else: assert (False) self.setTextNode(last) self.setOffset(offset) return res
def move(self, forward, topMostNode, limit, type, count): """ TODO IMPLEMENTED ONLY FOR CHARACTER Move the end point forwards or backwards by one unit of type, such as by a word. @param forward True if moving forward @param topMostNode top node to not move past, or None @param limit an endpoint not to move past, or None @param type what unit to move by, ie MOVE_CHARACTER or MOVE_WORD @param count how many of these to move by @return how far this actually moved """ res = 0 limitText = limit and limit.getTextNode() curr = getTextNode() if curr is not None: last = curr offset = getOffset() if type == MOVE_CHARACTER: while curr is not None: last = curr len = forward and (curr.getLength() - offset) or offset if curr == limitText: # If there is a limiting endpoint, may not be able to # go as far len = forward and (limit.getOffset() - offset) or \ (offset - limit.getOffset()) if (len + res) < count: res += len if curr == limitText: break else: # Finis len = count - res offset = forward and (offset + len) or (offset - len) res = count break while True: # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement( curr, topMostNode, forward, False) if (curr is None) or (curr.getLength() != 0): break if curr is not None: if forward: offset = 0 else: offset = curr.getLength() # case MOVE_WORDSTART: # case MOVE_WORDEND: # if c_wsRexp is None: # setWhitespaceRexp(DEFAULT_WS_REXP) # # # while curr is not None: # # do { # # Next node, skipping any 0-length texts # curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, # forward, False) # while ((curr is not None) and (curr.getLength() == 0)) # # if curr is not None: # offset = forward ? 0 : curr.getLength() # # # break else: assert (False) self.setTextNode(last) self.setOffset(offset) return res
def move(self, forward, topMostNode, limit, type, count): res = 0 limitText = limit and limit.getTextNode() curr = getTextNode() if curr is not None: last = curr offset = getOffset() if type == MOVE_CHARACTER: while curr is not None: last = curr len = forward and (curr.getLength() - offset) or offset if curr == limitText: # If there is a limiting endpoint, may not be able to # go as far len = forward and (limit.getOffset() - offset) or \ (offset - limit.getOffset()) if (len + res) < count: res += len if curr == limitText: break else: # Finis len = count - res offset = forward and (offset + len) or (offset - len) res = count break while True: # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, forward, False) if (curr is None) or (curr.getLength() != 0): break if curr is not None: if forward: offset = 0 else: offset = curr.getLength() """ case MOVE_WORDSTART: case MOVE_WORDEND: if c_wsRexp is None: setWhitespaceRexp(DEFAULT_WS_REXP) while curr is not None: do { # Next node, skipping any 0-length texts curr = RangeUtil.getAdjacentTextElement(curr, topMostNode, forward, False) while ((curr is not None) and (curr.getLength() == 0)) if curr is not None: offset = forward ? 0 : curr.getLength() break """ else: assert(False) self.setTextNode(last) self.setOffset(offset) return res