W3C home > Mailing lists > Public > whatwg@whatwg.org > January 2011

[whatwg] Control over selection direction

From: Ryosuke Niwa <rniwa@webkit.org>
Date: Fri, 14 Jan 2011 10:09:03 -0800
Message-ID: <AANLkTi=azwG0vXyXK_V-WzvzmqSLrtsJq2jA4Pg-Zgtj@mail.gmail.com>
On Fri, Jan 14, 2011 at 3:09 AM, Tim Down <timdown at gmail.com> wrote:

> If you don't need the TextRange-like character-based modification, you
> can instead use the selection's extend() method (supported in Mozilla,
> WebKit and Opera for years) to create a backwards selection:
>
> function selectRangeBackwards(range) {
>    var sel = window.getSelection();
>    var endRange = range.cloneRange();
>    endRange.collapse(false);
>    sel.addRange(endRange);
>    sel.extend(range.startContainer, range.startOffset);
> }


WebKit-based browsers also support setBaseAndExtent, which takes
startContainer, startOffset, endContainer, endOffset.  IMHO, this function
is better than extend because it can set the whole selection at once.

On Fri, Jan 14, 2011 at 3:53 AM, Tim Down <timdown at gmail.com> wrote:

> I'm sorry, I thought you were referring to regular selections as
> opposed to inputs or textareas, since you mentioned
> window.getSelection(). It hadn't occurred to me to use modify() to try
> and affect the selection in a textarea.


Maybe we can spec this so that regular selection primitives work properly
for textarea/input.  e.g. you can expect that when
startContainer/endContainer is textarea/input, selection is set inside
textarea/input.

- Ryosuke
Received on Friday, 14 January 2011 10:09:03 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:29 UTC