Fix for setSelection( htmlDocument().createRange() )

David Faure faure at kde.org
Fri Nov 5 01:36:28 GMT 2004


As Marc Mutz discovered when trying to do this from kmail,
KHTMLPart::setSelection() doesn't have a way to clear the selection currently.

When called with an "empty range" (such as the one returned by createRange(),
where start and end are the Document, at index 0, as per the Range spec),
RenderCanvas::setSelection() is called, which ends up selecting the whole document,
probably due to
    while (e->lastChild())
        e = e->lastChild();

Maybe this should be fixed in RenderCanvas? The render tree scares me though
so I wrote a fix for KHTMLPart instead. With -b it looks like this:

@@ -3331,6 +3331,10 @@ void KHTMLPart::selection(DOM::Node &s,

 void KHTMLPart::setSelection( const DOM::Range &r )
 {
+    if ( r.startContainer() == document() && r.endContainer() == document() &&
+         r.startOffset() == 0 && r.endOffset() == 0 )
+        slotClearSelection();
+    else {
     d->m_selectionStart = r.startContainer();
     d->m_startOffset = r.startOffset();
     d->m_selectionEnd = r.endContainer();
@@ -3341,6 +3345,7 @@ void KHTMLPart::setSelection( const DOM:
     bool v = d->m_view->placeCaret();
     emitCaretPositionChanged(v ? d->caretNode() : 0, d->caretOffset());
 #endif
+    }
 }

Should I commit this, or does someone want to tackle RenderCanvas::setSelection()
without breaking it for the other cases? :)

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kfm-devel mailing list