Better selection calculation

This commit is contained in:
benweet 2014-12-15 09:11:37 +00:00
parent 2d2cedd174
commit 848164fdf1

View File

@ -252,28 +252,22 @@ define([
var selectionRange = selection.getRangeAt(0); var selectionRange = selection.getRangeAt(0);
var node = selectionRange.startContainer; var node = selectionRange.startContainer;
if((contentElt.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY) || contentElt === node) { if((contentElt.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY) || contentElt === node) {
var offset = selectionRange.startOffset; var range = self.createRange({
if(node.hasChildNodes() && offset > 0) { container: contentElt,
node = node.childNodes[offset - 1]; offsetInContainer: 0
offset = node.textContent.length; }, {
} container: node,
var container = node; offsetInContainer: selectionRange.startOffset
while(node != contentElt) { });
while(node = node.previousSibling) { var offset = range.toString().length;
if(node.textContent) {
offset += node.textContent.length;
}
}
node = container = container.parentNode;
}
if(selection.isBackwards()) { if(selection.isBackwards()) {
selectionStart = offset + (selectionRange + '').length; selectionStart = offset + selectionRange.toString().length;
selectionEnd = offset; selectionEnd = offset;
} }
else { else {
selectionStart = offset; selectionStart = offset;
selectionEnd = offset + (selectionRange + '').length; selectionEnd = offset + selectionRange.toString().length;
} }
if(selectionStart === selectionEnd && selectionRange.startContainer.textContent == '\n' && selectionRange.startOffset == 1) { if(selectionStart === selectionEnd && selectionRange.startContainer.textContent == '\n' && selectionRange.startOffset == 1) {