I solved this problem by reselecting the text.
On mouseup I capture the selection information:
$.getSelection = function() {
// ie has its own way of doing things from here on.
if($.browser.msie) return document.selection.createRange();
if (window.getSelection) {
var selection = window.getSelection();
}
else if (document.selection) { // should come last; Opera!
var selection = document.selection.createRange();
}
if (selection.getRangeAt)
var range = selection.getRangeAt(0);
else { // Safari!
var range = document.createRange();
range.setStart(selection.anchorNode, selection.anchorOffset);
range.setEnd(selection.focusNode, selection.focusOffset);
}
if (!range.toString().match(/[^\t\r\n ]/)) return false;
var ret = {};
// start end length text
ret.startContainer = range.startContainer;
ret.start = range.startOffset;
ret.endContainer = range.endContainer;
ret.end = range.endOffset;
ret.length = ret.end - ret.start;
ret.collapsed = range.collapsed;
return ret;
};
This is stored somewhere of your choosing.
I then either reselect the text if the browser is w3c compliant for DOM Range (i.e. all but ie) and perform the execCommand:
function handleReselection() {
if($.browser.msie) {
return this.selection;
}
else {
setSelection(this.selection);
return document
}
}
function setSelection(selection) {
var sel = window.getSelection();
sel.removeAllRanges();
var range = document.createRange();
range.setStart(selection.startContainer, selection.start);
range.setEnd(selection.endContainer, selection.end);
sel.addRange(range);
}
handleReselection().execCommand('bold', false, null);