Fixed replace

This commit is contained in:
benweet 2014-12-15 00:30:21 +00:00
parent a71ed769b9
commit 59f5044232

View File

@ -251,21 +251,21 @@ define([
if(selection.rangeCount > 0) { if(selection.rangeCount > 0) {
var selectionRange = selection.getRangeAt(0); var selectionRange = selection.getRangeAt(0);
var node = selectionRange.startContainer; var node = selectionRange.startContainer;
if((contentElt.compareDocumentPosition(node) & 0x10)) { if((contentElt.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_CONTAINED_BY) || contentElt === node) {
var offset = selectionRange.startOffset; var offset = selectionRange.startOffset;
if(node.hasChildNodes() && offset > 0) { if(node.hasChildNodes() && offset > 0) {
node = node.childNodes[offset - 1]; node = node.childNodes[offset - 1];
offset = node.textContent.length; offset = node.textContent.length;
} }
var container = node; var container = node;
do { while(node != contentElt) {
while(node = node.previousSibling) { while(node = node.previousSibling) {
if(node.textContent) { if(node.textContent) {
offset += node.textContent.length; offset += node.textContent.length;
} }
} }
node = container = container.parentNode; node = container = container.parentNode;
} while(node && node != inputElt); }
if(selection.isBackwards()) { if(selection.isBackwards()) {
selectionStart = offset + (selectionRange + '').length; selectionStart = offset + (selectionRange + '').length;
@ -435,12 +435,14 @@ define([
function replace(selectionStart, selectionEnd, replacement) { function replace(selectionStart, selectionEnd, replacement) {
undoMgr.currentMode = undoMgr.currentMode || 'replace'; undoMgr.currentMode = undoMgr.currentMode || 'replace';
var range = selectionMgr.createRange(selectionStart, selectionEnd); var range = selectionMgr.createRange(
if('' + range == replacement) { Math.min(selectionStart, selectionEnd),
return; Math.max(selectionStart, selectionEnd)
);
if('' + range != replacement) {
range.deleteContents();
range.insertNode(document.createTextNode(replacement));
} }
range.deleteContents();
range.insertNode(document.createTextNode(replacement));
var endOffset = selectionStart + replacement.length; var endOffset = selectionStart + replacement.length;
selectionMgr.setSelectionStartEnd(endOffset, endOffset); selectionMgr.setSelectionStartEnd(endOffset, endOffset);
selectionMgr.updateSelectionRange(); selectionMgr.updateSelectionRange();