The Rangy Selection and Range APIs are supersets of the standard DOM Selection and Range APIs, so the documentation is from places like MDN ( Range , Selection ).
The problem you are facing is that the range bounds are expressed as an offset inside the containing DOM node. For example, in the HTML below, where the carriage is indicated as a channel symbol:
<p>Foo<br>b|ar</p>
... the boundaries of the beginning and end of the carriage range are identical and are set to offset 1 in the node text string.
If you want to set the position as an offset in the text content of the <p> element, you need to do a DOM traversal. I wrote an implementation of this based on another answer . This is a naive implementation: it does not take into account any text that may be invisible (for example, using CSS or, for example, inside or an element) and may have browser deviations (IE compared to the others) with line breaks and accepts without regard to collapsed spaces ( for example, two or more consecutive run characters that match the same visible space on the page). It is a difficult thing to get right, so I would not recommend it at all. I plan to write a text module for Rangy that will handle all this, but I haven't started it yet.
http://jsfiddle.net/ee93P/2/
code:
function setCaretCharIndex(containerEl, index) { var charIndex = 0, stop = {}; function traverseNodes(node) { if (node.nodeType == 3) { var nextCharIndex = charIndex + node.length; if (index >= charIndex && index <= nextCharIndex) { rangy.getSelection().collapse(node, index - charIndex); throw stop; } charIndex = nextCharIndex; }
source share