Bug 25584 - No way to get the x/y location of a selection
Summary: No way to get the x/y location of a selection
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-05 23:05 PDT by Eric Seidel (no email)
Modified: 2010-01-28 00:07 PST (History)
7 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2009-05-05 23:05:04 PDT
IE provides an x,y, but FF and WK don't.

I don't have a concrete proposal yet for what API to add here.
Comment 1 Ojan Vafai 2009-05-06 00:06:19 PDT
IE has getClientRects, getBoundingClientRect and boundingTop/Bottom/Left/Right on TextRanges. See http://msdn.microsoft.com/en-us/library/ms535872.aspx

This mostly seems like the right API to me. I'd be fine with just adding getClientRects and getBoundingClientRect to Ranges. bounding* seem redundant with getBoundingClientRect.

There's no reason to restrict this to Selections though. This ought to be possible on any Range.
Comment 2 Eric Seidel (no email) 2009-05-06 20:35:55 PDT
Looks like this is already speced as part of CSSOM:
http://www.w3.org/TR/cssom-view/#the-rangeview-interface

Let's do it! :)
Comment 3 Eric Seidel (no email) 2009-05-06 20:39:29 PDT
Actually, it doesn't help us.

The getClientRects() method, when invoked, must return an empty (static) TextRectangleList object if the range is empty (if the boundary-points are identical) and otherwise a (static) TextRectangleList object based on the following conditions:

That makes it impossible to find out where a collapsed range is!  (i.e. the cursor).

That's broken for our purposes, I guess I'll talk to  Anne.
Comment 4 Eric Seidel (no email) 2009-05-06 21:14:51 PDT
One answer to this would be to convince Anne to change CSSOM to make collapsed ranges return a non-empty rect.

Another would be to add getClientRects and getBoundingClientRect to the Selection object.

One question with either of those is what is the width of a cursor rect? Or the height? (line height?)  And what about a split cursor on the mac?  And are there non-text cursors we need to care about?
Comment 5 Eric Seidel (no email) 2009-05-06 21:16:17 PDT
FYI the current workaround here @ Google is to insert a TextNode at the current caret and call getClientBoundingRect() on that inserted text node, and then remove it. :)
Comment 6 Simon Fraser (smfr) 2009-05-06 21:24:11 PDT
The other problem with getClientRects et al. is that they are not transform-capable.
Comment 7 Ojan Vafai 2009-05-06 21:34:15 PDT
To clarify, the workaround is to put in a span (can't get the position of a text node). This mostly works OK, but in addition to being a bit heavyweight, it has side-effects like changing wrapping behavior (putting a span in the middle of the word would make the word breakable at that point).
Comment 8 Ojan Vafai 2009-05-06 21:37:08 PDT
smfr can you think of an API we could expose that would be transform capable?
Comment 9 Anne van Kesteren 2009-05-06 23:46:01 PDT
We can change the specification probably to still return a rect. I talked with Dean about transform capable APIs a while back, but it's not really my area of expertise. He said he would look into it.
Comment 10 Robert O'Callahan 2009-05-07 19:14:27 PDT
(In reply to comment #4)
> One answer to this would be to convince Anne to change CSSOM to make collapsed
> ranges return a non-empty rect.

I think this is the right direction.

> One question with either of those is what is the width of a cursor rect? 

I'd return a rect of width 0. I think we want the invariant that for text on a single line, if you decompose a range R into a set of ranges (including empty ranges) that partition R, adding up their widths gives you the width of R.

> Or the height? (line height?)

The spec says for non-empty text selections, "the vertical dimension of each box is determined by the font ascent and descent"; maybe that's not precise enough, but the behaviour should be the same for empty and non-empty ranges.

> And what about a split cursor on the mac?  And are
> there non-text cursors we need to care about?

Determining the actual caret position and dimensions is a different problem. I'd actually prefer that we not expose that to Web apps unless you have a really good use-case for it --- I can't think of one myself, right now.

(In reply to comment #7)
> in addition to being a bit heavyweight,
> it has side-effects like changing wrapping behavior (putting a span in the
> middle of the word would make the word breakable at that point).

A side issue, but IMHO that would be a bug in Webkit. Linebreaking is underspecified, but IMHO empty spans should not affect linebreaking and in Gecko they don't by design.
Comment 11 Julie Parent 2010-01-28 00:07:39 PST
The spec was updated to work for collapsed ranges (and seems to be implemented in FF 3.7a).  I'm going to close this bug in favor of the specific one implementing getClientRect for collapsed ranges (https://bugs.webkit.org/show_bug.cgi?id=34239)