Exception thrown in KHTMLPart::selection()
Leo Savernik
l.savernik at aon.at
Sun Jan 2 17:34:06 GMT 2005
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <https://mail.kde.org/mailman/private/kfm-devel/attachments/20050102/9cf98009/attachment.sig>
More information about the kfm-devel
mailing list