[webkit-dev] How to convert character offset into VisiblePosition?

Dan Bernstein mitz at apple.com
Wed Jun 2 09:30:35 PDT 2010

On Jun 1, 2010, at 10:48 PM, Xiaomei Ji wrote:

> Given a DOM Node and an InlineBox, is there a way to convert the character offset within this InlineBox to a VisiblePosition? considering bidi/RTL.

Not sure how being given a DOM node helps here…

> Using <span> node in the following example as an example:
> <div contenteditable dir=rtl>abc ששש def <span dir=ltr>שנב abc סטז</span> uvw זזז xyz</div> 
> Assume the <span> node is node A, using "(node, offset)a" to represent the node and offset information of Position when caret is before character "a", the position information are:
> ...... (A, 4)a(A, 5)b(A, 6)c(A, 7) (A, 0xb)ז(A, 0xa)ט(A, 9)ס(A, 8).
> Given the <span> node, the InlineBox representing " abc "(with surrounding spaces), and character offset 5 within the InlineBox, is there a way to convert offset 5 into VisiblePosition (A, 0xb) (not (A, 8))? which means when caret is placed after the 2nd space in " abc ", the visible position is (A, 0xb).

I don’t think there’s code to do this—hit testing code will just choose the DOM offset that corresponds to the line box you hit—but how about something like this?

If the offset is not at the edge of the text box, then just map it to that box’s text node
Otherwise, if there is an adjacent text box on that edge, and it has the same direction as the first text box, just map to the first text box’s text node (or, perhaps you should look at the bidi levels of both boxes and choose the one with the higher level?)
Otherwise, of the two adjacent text boxes, choose the one whose direction is equal to the block direction, and return an offset into the chosen box’s text node, corresponding to the edge

Does this cover all cases?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20100602/519f22e6/attachment.html>

More information about the webkit-dev mailing list