When it comes to selecting text, there can be some inconsistency in where the selection starts and ends. Sometimes it begins at the end of the previous element, and other times at the start of the text node. I am working on standardizing this so that the selection always begins at the beginning of the element containing the text and ends at the end of the element containing the text, ensuring consistency across different browsers.
For example:
<b>mouse></b><i>cat</i>
When selecting "cat", Chrome consistently returns a selection with startContainer as cat and startOffset as 0. On the other hand, Firefox and occasionally IE8 tend to start at the end of the previous element (mouse) with startOffset as 5.
My attempts to fix this issue have not been successful:
var sr=rangy.getSelection().getRangeAt(0);
var sc=sr.startContainer;
if(sc.nodeType!=3||sr.startOffset==sc.length)
{
sr.setStartAfter(sc); //move start to next node in range
}
rangy.getSelection().setSingleRange(sr);
console.log(sr.inspect());
What could I be overlooking?