Is there no way to create a reverse (i.e., right to left) selection from JavaScript?

I am trying to create a selection that goes from right to left in the text, but it seems that the DOM Range API does not allow me to do this. (I don’t see anything about this in the specification, and not that I read it carefully, but all implementations seem to agree not to support it.)

For example, given a very minimal document:

data:text/html,<div> this is a test </div>

I can use this script to enable editing and create a normal selection (for example, from a bookmarklet, but adding a line is added for clarity):

javascript:document.designMode='on';
var r=document.createRange(),d=document.getElementsByTagName('div')[0]; 
r.setStart(d.firstChild, 3); 
r.setEnd(d.firstChild, 7); 
window.getSelection().addRange(r); void(0);

However, if I change 3 and 7, the selection will not be created.

Does anyone know how to do this?

+3
source share
1 answer

, IE, extend() Selection. , :

function selectRangeBackwards(range) {
    var sel = window.getSelection();
    var endRange = range.cloneRange();
    endRange.collapse(false);
    sel.removeAllRanges();
    sel.addRange(endRange);
    sel.extend(range.startContainer, range.startOffset);
}

IE ( 11). IE 9 DOM Level 2 Range HTML5 Text Selection ( WHATWG Range), , extend(), IE 9 (. ).

extend() IE: https://connect.microsoft.com/IE/feedback/details/737106/implement-missing-extend-method-of-selection

IE API .

+5

Source: https://habr.com/ru/post/1787872/


All Articles