There is cheap, affordable and expensive. The vast majority of times one should strive for an affordable answer. Unfortunately it appears that cheap is appropriate in this circumstance. Using 1 millisecond delay with setTimeout
seems to have eliminated the issue.
var e = document.getElementById('example');
if (e.childNodes.length > 0)
{
window.getSelection().removeAllRanges();
setTimeout(function() {window.getSelection().selectAllChildren(e);},1);
}
This is clearly a Gecko bug on the internal timing mechanisms however after filing dozens of quality bug reports at Mozilla for over a decade that have fallen on deaf ears anyone encountering this issue will just have to waste a few more bytes until I take over Mozilla.
Similar Bugs...
The error message is very generic so to determine if the error has to do with the internal timing mechanisms of the JavaScript engine (that developers working with JavaScript should never have to be concerned with) it is critical to ensure that every mouse movement, click, etc is reproduced and in the same order. If it appears that the exact same actions sometimes fail without any predictability other than simply happening in non-deterministic fashion then using setTimeout
might help alleviate such issues though ultimately it is a browser bug that should be fixed.