- if (!currentSelection || !currentSelection.changed) return;
- var win = currentSelection.window, range = win.document.createRange();
+ var cs = currentSelection;
+ // on webkit-based browsers, it is apparently possible that the
+ // selection gets reset even when a node that is not one of the
+ // endpoints get messed with. the most common situation where
+ // this occurs is when a selection is deleted or overwitten. we
+ // check for that here.
+ function focusIssue() {
+ if (cs.start.node == cs.end.node && cs.start.offset == cs.end.offset) {
+ var selection = cs.window.getSelection();
+ if (!selection || selection.rangeCount == 0) return true;
+ var range = selection.getRangeAt(0), point = innerNode(range.startContainer, range.startOffset);
+ return cs.start.node != point.node || cs.start.offset != point.offset;
+ }
+ }
+ if (!cs || !(cs.changed || (webkit && focusIssue()))) return;
+ var win = cs.window, range = win.document.createRange();