Sharepoint - "a is null" error in vanilla SP2010 install

I have seen a bug in the SP.UI.RTE.js file that Microsoft has still not resolved. To fix it on environment where I have seen behavior like this, I add the following JavaScript to the master page:

    function fixRTEBug() {
    if (typeof RTE.Canvas.checkCurrentFocus !== 'undefined') {
        RTE.Canvas.checkCurrentFocus = function() {ULSkay:;
            var $v_0 = RTE.Selection.getSelectionRange();
            if ($v_0) { //this is different from SP to avoid error in console
                var $v_1 = $v_0.parentElement();
                if (RTE.Canvas.isInEditable($v_1) && !RTE.Cursor.get_range().isValid()) {
                    RTE.Cursor.updateRangeToCurrentSelection();
                    RTE.Cursor.update();
                }
            }
        }
    }

    // This Fix for parentElement bug in RTE should survive Service Packs and CU's
    function SubstituteRTERangeParentElement() {
        var originalRTERangeParentElement = RTE.Range.prototype.parentElement;
        RTE.Range.prototype.parentElement = function () {
            try {
                originalRTERangeParentElement();
            } catch (e) { }
        }
    }
    SubstituteRTERangeParentElement();
}

ExecuteOrDelayUntilScriptLoaded(fixRTEBug, "sp.ui.rte.js");

You can also fix in this way.

SP.SOD.executeFunc('sp.ui.rte.js', null, function () {
    if (/var \$v_0 = RTE\.Selection\.getSelectionRange\(\);[\r\n\t ]*var \$v_1 = \$v_0\.parentElement\(\);/.test(RTE.Canvas.checkCurrentFocus.toString())) {
        var _oldCheckCurrentFocus = RTE.Canvas.checkCurrentFocus;
        RTE.Canvas.checkCurrentFocus = function () {   
            if(RTE.Selection.getSelectionRange())
                return _oldCheckCurrentFocus();
        };
    }
});

Use SP.SOD.executeFunc to wait until RTE library is loaded, once it's loaded you first save the original implementation then test if it still has the bug using a Regular Expression, if the bug is there you override the original funcion with a version which will not cause the error.

But note that I did not override it completely, the new function check if RTE.Selection.getSelectionRange() returns a valid object, if it does the old implementation is executed, otherwise the function ends.

This way we do not mess so much with native code, and if Microsoft fix it your code will not override their funciton anymore automatically. And if Microsoft changes the function but keep the bug we guarantee that when the selection exists we will continue executing the newest implementation.