You can delete a range using its deleteContents()
method.
Rangy also deals with a lot of the cross-browser issues you're attempting to fix in your jsFiddle. It provides a createContextualFragment()
implementation and a selection object with getRangeAt()
. Also, you should use the ctrlKey
property of the keydown
event. Here's an updated jsFiddle:
http://jsfiddle.net/timdown/eJNKy/6/
The crucial function is
function paste(html) {
var sel = rangy.getSelection();
if (html && sel.rangeCount > 0) {
var range = sel.getRangeAt(0);
range.deleteContents();
var frag = range.createContextualFragment(html);
var lastChild = frag.lastChild;
range.insertNode(frag);
range.collapseAfter(lastChild);
sel.setSingleRange(range);
}
}