[whatwg] Control over selection direction

Ryosuke Niwa rniwa at webkit.org
Fri Jan 14 10:09:03 PST 2011


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



More information about the whatwg mailing list