Fixed selection offset on element nodes. Fixes #518

This commit is contained in:
benweet 2014-12-14 22:10:43 +00:00
parent 88551b3c26
commit bd062a2103

View File

@ -250,18 +250,22 @@ define([
var selection = rangy.getSelection(); var selection = rangy.getSelection();
if(selection.rangeCount > 0) { if(selection.rangeCount > 0) {
var selectionRange = selection.getRangeAt(0); var selectionRange = selection.getRangeAt(0);
var element = selectionRange.startContainer; var node = selectionRange.startContainer;
if((contentElt.compareDocumentPosition(element) & 0x10)) { if((contentElt.compareDocumentPosition(node) & 0x10)) {
var container = element;
var offset = selectionRange.startOffset; var offset = selectionRange.startOffset;
if(node.hasChildNodes() && offset > 0) {
node = node.childNodes[offset - 1];
offset = node.textContent.length;
}
var container = node;
do { do {
while(element = element.previousSibling) { while(node = node.previousSibling) {
if(element.textContent) { if(node.textContent) {
offset += element.textContent.length; offset += node.textContent.length;
} }
} }
element = container = container.parentNode; node = container = container.parentNode;
} while(element && element != inputElt); } while(node && node != inputElt);
if(selection.isBackwards()) { if(selection.isBackwards()) {
selectionStart = offset + (selectionRange + '').length; selectionStart = offset + (selectionRange + '').length;