def getTargetInChildren(element, x, y): """ x and y are absolute coordinates within the document. Return the last child of element that contains (x,y). Return None if not found. """ return_elt = None for elt in DOM.IterWalkChildren(element): hit = isIn(elt, x, y) if hit: return_elt = elt return return_elt
def findDraggable(element, x, y): """ Find the element within a widget's main element that is draggable and under the mouse cursor. We need this for event delegation. """ for elt in DOM.IterWalkChildren(element): try: draggable = elt.draggable except: draggable = False if draggable: if isIn(elt, x, y): return elt return None
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()