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

@ -242,108 +242,85 @@ 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;
if (typeof(document["selection"]) != "undefined") {
var start, end, sel, scrollPos, subst, res; sel = document.selection.createRange().text;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
if (typeof(document["selection"]) != "undefined") { start = this.textarea.selectionStart;
sel = document.selection.createRange().text; end = this.textarea.selectionEnd;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { scrollPos = this.textarea.scrollTop;
start = this.textarea.selectionStart; // go to the start of the line
end = this.textarea.selectionEnd; start = this.textarea.value.substring(0, start).replace(/[^\r\n]*$/g,'').length;
scrollPos = this.textarea.scrollTop; // go to the end of the line
// go to the start of the line end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length;
start = this.textarea.value.substring(0, start).replace(/[^\r\n]*$/g,'').length; sel = this.textarea.value.substring(start, end);
// go to the end of the line }
end = this.textarea.value.length - this.textarea.value.substring(end, this.textarea.value.length).replace(/^[^\r\n]*/, '').length; if (sel.match(/ $/)) {
sel = this.textarea.value.substring(start, end); sel = sel.substring(0, sel.length - 1);
} suffix = suffix + " ";
}
if (sel.match(/ $/)) { // exclude ending space char, if any if (typeof(fn) == 'function') {
sel = sel.substring(0, sel.length - 1); res = (sel) ? fn.call(this, sel) : fn('');
suffix = suffix + " "; } else {
} res = (sel) ? sel : '';
}
if (typeof(fn) == 'function') { subst = prefix + res + suffix;
res = (sel) ? fn.call(this,sel) : fn(''); if (typeof(document["selection"]) != "undefined") {
} else { var range = document.selection.createRange().text = subst;
res = (sel) ? sel : ''; this.textarea.caretPos -= suffix.length;
} } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end);
subst = prefix + res + suffix; if (sel) {
this.textarea.setSelectionRange(start + subst.length, start + subst.length);
if (typeof(document["selection"]) != "undefined") { } else {
document.selection.createRange().text = subst; this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
var range = this.textarea.createTextRange(); }
range.collapse(false); this.textarea.scrollTop = scrollPos;
range.move('character', -suffix.length); }
range.select(); },
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst + encloseSelection: function (prefix, suffix, fn) {
this.textarea.value.substring(end); this.textarea.focus();
if (sel) { prefix = prefix || '';
this.textarea.setSelectionRange(start + subst.length, start + subst.length); suffix = suffix || '';
} else { var start, end, sel, scrollPos, subst, res;
this.textarea.setSelectionRange(start + prefix.length, start + prefix.length); if (typeof(document["selection"]) != "undefined") {
} sel = document.selection.createRange().text;
this.textarea.scrollTop = scrollPos; } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
} start = this.textarea.selectionStart;
}, end = this.textarea.selectionEnd;
scrollPos = this.textarea.scrollTop;
encloseSelection: function(prefix, suffix, fn) { sel = this.textarea.value.substring(start, end);
this.textarea.focus(); }
if (sel.match(/ $/)) {
prefix = prefix || ''; sel = sel.substring(0, sel.length - 1);
suffix = suffix || ''; suffix = suffix + " ";
}
var start, end, sel, scrollPos, subst, res; if (typeof(fn) == 'function') {
res = (sel) ? fn.call(this, sel) : fn('');
if (typeof(document["selection"]) != "undefined") { } else {
sel = document.selection.createRange().text; res = (sel) ? sel : '';
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") { }
start = this.textarea.selectionStart; subst = prefix + res + suffix;
end = this.textarea.selectionEnd; if (typeof(document["selection"]) != "undefined") {
scrollPos = this.textarea.scrollTop; var range = document.selection.createRange().text = subst;
sel = this.textarea.value.substring(start, end); this.textarea.caretPos -= suffix.length;
} } else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst + this.textarea.value.substring(end);
if (sel.match(/ $/)) { // exclude ending space char, if any if (sel) {
sel = sel.substring(0, sel.length - 1); this.textarea.setSelectionRange(start + subst.length, start + subst.length);
suffix = suffix + " "; } else {
} this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
}
if (typeof(fn) == 'function') { this.textarea.scrollTop = scrollPos;
res = (sel) ? fn.call(this,sel) : fn(''); }
} else { },
res = (sel) ? sel : '';
}
subst = prefix + res + suffix;
if (typeof(document["selection"]) != "undefined") {
document.selection.createRange().text = subst;
var range = this.textarea.createTextRange();
range.collapse(false);
range.move('character', -suffix.length);
range.select();
// this.textarea.caretPos -= suffix.length;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
this.textarea.value = this.textarea.value.substring(0, start) + subst +
this.textarea.value.substring(end);
if (sel) {
this.textarea.setSelectionRange(start + subst.length, start + subst.length);
} else {
this.textarea.setSelectionRange(start + prefix.length, start + prefix.length);
}
this.textarea.scrollTop = scrollPos;
}
},
stripBaseURL: function(url) { stripBaseURL: function(url) {
if (this.base_url != '') { if (this.base_url != '') {
var pos = url.indexOf(this.base_url); var pos = url.indexOf(this.base_url);