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

[whatwg] Control over selection direction

From: Tim Down <timdown@gmail.com>
Date: Fri, 14 Jan 2011 11:09:59 +0000
Message-ID: <AANLkTi=g5OyjM-CXXk+Oas27OmRQXnaYNOuAAi7xP04+@mail.gmail.com>
On 14 January 2011 10:16, Marijn Haverbeke <marijnh at gmail.com> wrote:
> Another relevant precedent is window.getSelection().modify (Webkit and
> Gecko-2 specific), which uses the strings "forward" and "backward" to
> specify the direction in which to alter the selection. English is not
> my native language, and I'm not sure what the semantic difference
> between "forward" and "forwards" is, but I do expect people to
> misremember which one we end up using, and use the other one. Would it
> make sense to accept both the with-s and the without-s versions, or is
> that kind of do-what-mean stuff not HTML5-style?
>
> (This .modify method can, by the way, already be used, on the browsers
> that support it, to create reversed selections by setting a collapsed
> selection at the end of the desired selection, and then calling
> getSelection().modify("extend", "backward", "character") X times to
> adjust the start to the desired point. This is, unfortunately,
> horribly slow, and quite clunky.)

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);
}

Tim
Received on Friday, 14 January 2011 03:09:59 UTC

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