Backported changes to encloseSelection from latest DotClear release, less bugs in IE8

This commit is contained in:
Stephan Eckardt 2011-03-24 16:14:10 +01:00
parent 5592794d75
commit 0ace77081d

View File

@ -243,14 +243,11 @@ jsToolBar.prototype = {
this.encloseSelection(stag,etag); this.encloseSelection(stag,etag);
}, },
encloseLineSelection: function(prefix, suffix, fn) { encloseLineSelection: function (prefix, suffix, fn) {
this.textarea.focus(); this.textarea.focus();
prefix = prefix || ''; prefix = prefix || '';
suffix = suffix || ''; suffix = suffix || '';
var start, end, sel, scrollPos, subst, res; var start, end, sel, scrollPos, subst, res;
if (typeof(document["selection"]) != "undefined") { if (typeof(document["selection"]) != "undefined") {
sel = document.selection.createRange().text; sel = document.selection.createRange().text;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
@ -263,29 +260,21 @@ jsToolBar.prototype = {
end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length; end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length;
sel = this.textarea.value.substring(start, end); sel = this.textarea.value.substring(start, end);
} }
if (sel.match(/ $/)) {
if (sel.match(/ $/)) { // exclude ending space char, if any
sel = sel.substring(0, sel.length - 1); sel = sel.substring(0, sel.length - 1);
suffix = suffix + " "; suffix = suffix + " ";
} }
if (typeof(fn) == 'function') { if (typeof(fn) == 'function') {
res = (sel) ? fn.call(this,sel) : fn(''); res = (sel) ? fn.call(this, sel) : fn('');
} else { } else {
res = (sel) ? sel : ''; res = (sel) ? sel : '';
} }
subst = prefix + res + suffix; subst = prefix + res + suffix;
if (typeof(document["selection"]) != "undefined") { if (typeof(document["selection"]) != "undefined") {
document.selection.createRange().text = subst; var range = document.selection.createRange().text = subst;
var range = this.textarea.createTextRange(); this.textarea.caretPos -= suffix.length;
range.collapse(false);
range.move('character', -suffix.length);
range.select();
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end);
this.textarea.value.substring(end);
if (sel) { if (sel) {
this.textarea.setSelectionRange(start + subst.length, start + subst.length); this.textarea.setSelectionRange(start + subst.length, start + subst.length);
} else { } else {
@ -295,14 +284,11 @@ jsToolBar.prototype = {
} }
}, },
encloseSelection: function(prefix, suffix, fn) { encloseSelection: function (prefix, suffix, fn) {
this.textarea.focus(); this.textarea.focus();
prefix = prefix || ''; prefix = prefix || '';
suffix = suffix || ''; suffix = suffix || '';
var start, end, sel, scrollPos, subst, res; var start, end, sel, scrollPos, subst, res;
if (typeof(document["selection"]) != "undefined") { if (typeof(document["selection"]) != "undefined") {
sel = document.selection.createRange().text; sel = document.selection.createRange().text;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
@ -311,30 +297,21 @@ jsToolBar.prototype = {
scrollPos = this.textarea.scrollTop; scrollPos = this.textarea.scrollTop;
sel = this.textarea.value.substring(start, end); sel = this.textarea.value.substring(start, end);
} }
if (sel.match(/ $/)) {
if (sel.match(/ $/)) { // exclude ending space char, if any
sel = sel.substring(0, sel.length - 1); sel = sel.substring(0, sel.length - 1);
suffix = suffix + " "; suffix = suffix + " ";
} }
if (typeof(fn) == 'function') { if (typeof(fn) == 'function') {
res = (sel) ? fn.call(this,sel) : fn(''); res = (sel) ? fn.call(this, sel) : fn('');
} else { } else {
res = (sel) ? sel : ''; res = (sel) ? sel : '';
} }
subst = prefix + res + suffix; subst = prefix + res + suffix;
if (typeof(document["selection"]) != "undefined") { if (typeof(document["selection"]) != "undefined") {
document.selection.createRange().text = subst; var range = document.selection.createRange().text = subst;
var range = this.textarea.createTextRange(); this.textarea.caretPos -= suffix.length;
range.collapse(false);
range.move('character', -suffix.length);
range.select();
// this.textarea.caretPos -= suffix.length;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end);
this.textarea.value.substring(end);
if (sel) { if (sel) {
this.textarea.setSelectionRange(start + subst.length, start + subst.length); this.textarea.setSelectionRange(start + subst.length, start + subst.length);
} else { } else {