Exception thrown in KHTMLPart::selection()

John Tapsell john at geola.co.uk
Thu Jan 13 17:45:50 GMT 2005


I can't find the email where we were discussing this.. very wierd.

You sent me the below quoted email.  I implemented this, but it doesn't work 
quite right.
The problem is if you have something like:

<img..>text1<img..>text2

and you select "text1<img..>"  it counts that as selecting 
"text1<img..>text2".

I can't understand why.  You can test with the cvs head khtml, pasting into 
openoffice.   Do you know why this algorithm is doing this?



On Sunday 02 January 2005 17:34, Leo Savernik wrote:
> Am Freitag, 31. Dezember 2004 16:48 schrieb John Tapsell:
> > Hi all,
> >   There's a function selection()  which returns a DOMRange:
> >
> > DOM::Range KHTMLPart::selection() const
> >
> > (kdelibs/khtml/khtml_part.cpp)
> >
> >   I use it to get a domrange for the text the user selects, then I turn
> > this into html.  This works fine, but if the end of your selection is an
> > image (and probably any non-text element), it throws an exception.
>
> I guess you simply pass the starting (node, offset) pair and the ending
> (node, offset) pair of the selection into the DOM::Range constructor.
>
> The problem with this approach is that khtml's selection algorithm produces
> no valid DOM ranges. Therefore, it bails out.
>
> Hence, before passing a selection position to a dom range constructor, it
> must be converted to a DOM range compliant position first.
>
> In the designmode_branch, there's already code to do this. You may lift it
> and use it for your own purposes. It's this piece of code in
> xml/dom_position.cpp:
>
> Position Position::equivalentRangeCompliantPosition() const
> {
>     if (isEmpty())
>         return *this;
>
>     if (!node()->parentNode())
>         return *this;
>
>     RenderObject *renderer = node()->renderer();
>     if (!renderer)
>         return *this;
>
>     if (!renderer->isReplaced() && !renderer->isBR())
>         return *this;
>
>     int o = 0;
>     const NodeImpl *n = node();
>     while ((n = n->previousSibling()))
>         o++;
>
>     return Position(node()->parentNode(), o + offset());
> }
>
>
> It may not perfectly fit, so you have to experiment a bit.
>
> hth
>
>
> [...]
>
> mfg
>  Leo




More information about the kfm-devel mailing list