Merge branch 'finnlabs-305_jstoolbar_fix_for_ie8'

This commit is contained in:
Felix Schäfer 2011-03-25 17:28:08 +01:00
commit 8d6133f6db
1 changed files with 101 additions and 102 deletions

View File

@ -245,14 +245,33 @@ jsToolBar.prototype = {
encloseLineSelection: function (prefix, suffix, fn) {
this.textarea.focus();
prefix = prefix || '';
suffix = suffix || '';
var start, end, sel, scrollPos, subst, res;
if (typeof(document["selection"]) != "undefined") {
sel = document.selection.createRange().text;
// just makes it work in IE8 somehow
var range = document.selection.createRange();
var bookmark = range.getBookmark();
var origParent = range.parentElement();
// we move the starting point of the selection to the last newline
try {
while (range.text[0] != "\n" && range.text[0] != "\r") {
bookmark = range.getBookmark();
range.moveStart("character", -1);
if (origParent != range.parentElement()) {
throw "Outside of Textarea";
}
}
range.moveStart("character", 1);
} catch(err) {
if (err == "Outside of Textarea")
range.moveToBookmark(bookmark);
else
throw err;
}
if (range.text.match(/ $/))
range.moveEnd("character", -1);
sel = range.text;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
start = this.textarea.selectionStart;
end = this.textarea.selectionEnd;
@ -263,29 +282,21 @@ jsToolBar.prototype = {
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);
}
if (sel.match(/ $/)) { // exclude ending space char, if any
if (sel.match(/ $/)) {
sel = sel.substring(0, sel.length - 1);
suffix = suffix + " ";
}
if (typeof(fn) == 'function') {
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();
range.text = subst;
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);
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 {
@ -297,12 +308,9 @@ jsToolBar.prototype = {
encloseSelection: function (prefix, suffix, fn) {
this.textarea.focus();
prefix = prefix || '';
suffix = suffix || '';
var start, end, sel, scrollPos, subst, res;
if (typeof(document["selection"]) != "undefined") {
sel = document.selection.createRange().text;
} else if (typeof(this.textarea["setSelectionRange"]) != "undefined") {
@ -311,30 +319,21 @@ jsToolBar.prototype = {
scrollPos = this.textarea.scrollTop;
sel = this.textarea.value.substring(start, end);
}
if (sel.match(/ $/)) { // exclude ending space char, if any
if (sel.match(/ $/)) {
sel = sel.substring(0, sel.length - 1);
suffix = suffix + " ";
}
if (typeof(fn) == 'function') {
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;
var range = document.selection.createRange().text = subst;
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);
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 {