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

Xiaomei Ji xji at chromium.org
Wed Jun 2 22:22:51 PDT 2010

Hi Dan,

Thanks for your quick feedback.
Please see my comments inline.

On Wed, Jun 2, 2010 at 9:30 AM, Dan Bernstein <mitz at apple.com> wrote:

> 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…

The node is just for constructing the position.

> 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?
>    1. If the offset is not at the edge of the text box, then just map it
>    to that box’s text node
>    2. 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?)
>    3. 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?

Looks like it works. I tried pure LTR text, RTL text, and mixed text in RTL
div, LTR div, div with span having the same direction, and div with span
having different direction.
Except one case it does not work correctly (neither arrow key works
<div contenteditable id="multiple_space_wrong_result">abc ששש def <span
dir=rtl>שנב  opq סטז</span>  uvw ששש xyz</div>
Note: there are 2 spaces between "שנב" and "opq" in <span> node.

The position information of the span node are:
(node, 12)ז(node, 11)ט(node, 10)ס(node, 9) (?)o(node, 6)p(node, 7)q(node, 8)
(node, 3)ב(node, 2)נ(node, 1)ש(node, 4)
Note: I am not able to place caret before 'o'.

When offset is at the left edge of text box " opq ", the chosen box will be
text box " opq " since it has the same directionality as that of its
enclosing box. The computed position will be (node, 4) since 4 is the min
caret offset of text box " opq ".
It is wrong because (node, 4) is at the right edge of text box "שנב".

When there is no extra space between  "שנב" and "opq" in <span> node, the
computation works correctly.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20100602/d4b3107f/attachment.html>

More information about the webkit-dev mailing list