def toCursor(self, start): rng = self.getRange() if rng is None or rng.isCursor(): return rng.collapse(start) self.getSelection() Selection.setRange(rng) self.refresh()
def getSelection(self): res = None try: window = self.getWindow() Selection.getSelection(window) except: print "Error getting the selection" traceback.print_exc()
def selectNodes(self, startNode, startOffset, endNode=None, endOffset=None): startText = self.findNodeByNumber(startNode) if endNode is not None: endText = self.findNodeByNumber(endNode) else: endText = startText endOffset = startOffset rng = Range(RangeEndPoint(startText, startOffset), RangeEndPoint(endText, endOffset)) self.getSelection() Selection.setRange(rng) self.refresh()
def captureSelection(self): try: self.getSelection() self.m_lastRange = Selection.getRange() except: GWT.log("Error capturing selection for IE", ex)
def getRange(self): if self.m_lastRange is None: self.getSelection() return Selection.getRange() else: return self.m_lastRange
def run(self): try: self.getSelection() rng = Selection.getRange() if (self.timerRange is None) or (not self.timerRange.equals(rng)): self.onSelectionChange(rng) self.timerRange = rng except: GWT.log("Error in timer selection", ex)
def captureSelection(self): """ This captures the selection when the mouse leaves the RTE, because in IE the selection indicating the cursor position is lost once another widget gains focus. Could be implemented for IE only. """ try: self.getSelection() self.lastRange = Selection.getRange() except: GWT.log("Error capturing selection for IE", ex)
def _apply(self): formatter = self.m_editor.getFormatter() link = self.m_webPageText.getText().strip() if not link: return False print self.m_origAnchorStart, self.m_origAnchorEnd if (self.m_origAnchorStart is not None) and self.m_fillOutCB.getValue(): # Expand selection to these bounds self.m_range.setRange(self.m_origAnchorStart, self.m_origAnchorEnd) # Ensure the selection hasn't changed, or at least changes to the # expanded bounds we want Selection.setRange(self.m_range) targetText = self.m_targetText.getText() if self.m_range.isCursor(): # Insert into a single cursor location newEle = DOM.createAnchor() newEle.href = link newEle.innerText = targetText sp = self.m_range.getStartPoint() startNode = sp.getTextNode() offset = sp.getOffset() print "sp", sp, startNode, offset parentEle = startNode.parentElement text = startNode.data if offset == 0: parentEle.insertBefore(newEle, startNode) else: if offset < len(text): # Split this in two and insert the node between startNode.splitText(offset) DOM.insertAfter(startNode, newEle) Selection.setRange(Range(newEle)) elif targetText != self.m_origTargetText: # Replace whatever was selected with this text ele = self.m_range.surroundContents() newEle = DOM.createAnchor() newEle.href = link newEle.innerText = targetText ele.parentElement.replaceChild(newEle, ele) Selection.setRange(Range(newEle)) else: formatter.createLink(link) return True
def _apply(self): formatter = self.m_editor.getFormatter() link = self.m_webPageText.getText().strip() if not link: return False print self.m_origAnchorStart, self.m_origAnchorEnd if (self.m_origAnchorStart is not None) and self.m_fillOutCB.getValue(): # Expand selection to these bounds self.m_range.setRange(self.m_origAnchorStart, self.m_origAnchorEnd) # Ensure the selection hasn't changed, or at least changes to the # expanded bounds we want Selection.setRange(self.m_range) targetText = self.m_targetText.getText() if self.m_range.isCursor(): # Insert into a single cursor location newEle = DOM.createAnchor() newEle.setHref(link) newEle.setInnerText(targetText) sp = self.m_range.getStartPoint() startNode = sp.getTextNode() offset = sp.getOffset() print "sp", sp, startNode, offset parentEle = startNode.parentElement text = startNode.data if offset == 0: parentEle.insertBefore(newEle, startNode) else: if offset < text.length(): # Split this in two and insert the node between startNode.splitText(offset) parentEle.insertAfter(newEle, startNode) Selection.setRange(Range(newEle)) elif targetText != self.m_origTargetText: # Replace whatever was selected with this text ele = self.m_range.surroundContents() newEle = DOM.createAnchor() newEle.href = link newEle.innerText = targetText ele.parentElement.replaceChild(newEle, ele) Selection.setRange(Range(newEle)) else: formatter.createLink(link) return True
def getDocument(self): return Selection.getDocument(self.getWindow())
def _surround(self, kls, cls): """ this is possibly one of the most truly dreadful bits of code for manipulating DOM ever written. its purpose is to add only the editor class required, and no more. unfortunately, DOM gets chopped up by the range thing, and a bit more besides. so we have to: * extract the range contents * clean up removing any blank text nodes that got created above * slap a span round it * clean up removing any blank text nodes that got created above * remove any prior editor styles on the range contents * go hunting through the entire document for stacked editor styles this latter is funfunfun because only "spans with editor styles which themselves have no child elements but a single span with an editor style" must be removed. e.g. if an outer editor span has another editor span and also some text, the outer span must be left alone. """ rng = self.getRange() if (rng is None) or rng.isCursor(): return csm = kls(rng.m_document, cls) rng.ensureRange() dfrag = rng.m_range.extractContents() remove_editor_styles(rng.m_document, csm, dfrag) element = csm.create() DOM.appendChild(element, dfrag) rng.m_range.insertNode(element) it = DOM.IterWalkChildren(element, True) while True: try: node = it.next() except StopIteration: break if node.nodeType == 3 and unicode(node.data) == u'': DOM.removeChild(node.parentNode, node) rng.setRange(element) it = DOM.IterWalkChildren(rng.m_document, True) while True: try: node = it.next() except StopIteration: break if node.nodeType == 3 and unicode(node.data) == u'': DOM.removeChild(node.parentNode, node) # clears out all nodes with no children. it = DOM.IterWalkChildren(rng.m_document) while True: try: node = it.next() except StopIteration: break if node.firstChild or not csm.identify(node): continue DOM.removeChild(node.parentNode, node) it = DOM.IterWalkChildren(rng.m_document, True) while True: try: node = it.next() except StopIteration: break if not csm.identify(node): continue if node.firstChild is None: continue if not csm.identify(node.firstChild): continue if node.firstChild.nextSibling: continue # remove the *outer* one because the range was added to # the inner, and the inner one overrides anyway remove_node(rng.m_document, node) doc = self.getDocument() self.getSelection() Selection.setRange(rng) self.refresh()