IE 8 Bug
Firstly, thanks this awesome tool helping my day. Here is a bug report, hopes it can help others.
In IE 8, the return object of $().getSelection().text is buggy.
I found out the reason.
The reason is the new-line symbol in IE8 is \r\n and IE11 or FF or Chrome is \n
This makes setSelection() and getSelection() buggy.
/*
<textarea id="area"> a
b</textarea>
*/
$("#area").setSelection(1,3);
$("#area").getSelection();
Thanks.
https://github.com/timdown/rangyinputs/blob/master/rangyinputs-jquery-src.js#L161-L168
variable val need adding val = val.replace(/\r/g,"")
https://github.com/timdown/rangyinputs/blob/master/rangyinputs-jquery-src.js#L56-L63 need changing to
function makeSelection(el, start, end) {
var escapeIE = el.value.replace(/\r/g,""); // here
console.log("I'm here?");
return {
start: start,
end: end,
length: end - start,
text: escapeIE.slice(start, end) // and here
};
}
This is by design. The selection offsets are relative to the value of the textarea in all cases. It does mean that offsets can vary between browsers but I think this is preferable to the alternative, which is having selection offsets that are relative to a string that isn't necessarily the same as the textarea's value and therefore needing to normalize the textarea's value before using the offsets.
Having said that, I agree that it would be useful to be able to set a selection reliably with the same offsets in all browsers. I'm not sure what the best API for that is but I'll have a think and get back to you.
sorry, API is correct, I'm wrong.
jQuery val() return the string with new-line symbol \n , even if it's \r\n it will help us turn into \n.
At mean time, I use getSelection()'s position , which was calculated base on \r\n , spliting the val()'s string then that makes everything mess up.
Sorry, plz close this issue, and thanks for response !
p.s. If it can be normalize, it'll be great :)