Quantcast
Channel: John Chapman » Web | John Chapman
Viewing all articles
Browse latest Browse all 10

[SharePoint 2010] JavaScript Errors in sp.ui.rte.js and sp.ui.rte.debug.js

$
0
0

In SharePoint 2010, there is a bug in the SP.UI.RTE.js file (and it’s debug version) that causes a JavaScript error in a few different situations. For myself, it was occurring when using the TaxonomyWebTaggingControl on a custom application page. Others have noticed the error with form validation.

The issue is caused by a null reference exception that isn’t caught in a try-catch block or other type of error handling.

James Boman posted a solution that involves editing these files on the hard drive. I agree with his sentiment in the post that this is a really bad idea for a few reasons: 1) Microsoft doesn’t support your environment if you mess with their files, 2) any updates to SharePoint (and sometimes Windows) will override your changes, and 3) you could do more damage than good.

With that said, however, using his modified code, we can override the offending code with some additional JavaScript rather than editing Microsoft’s code. In this function, we first ensure that the function is not undefined. Then, we replace the specific function with a fixed version of the function. It’s not the prettiest fix, however it sure beats editing Microsoft’s files.

function fixRTEBug () {
    if ((typeof (RTE) !== 'undefined') && (typeof (RTE.Range) !== 'undefined') && (typeof (RTE.Range.prototype) !== 'undefined') && (typeof (RTE.Range.prototype.parentElement) !== 'undefined')) {
        RTE.Range.prototype.parentElement = (function () {
            ULSkay: ;
            var $v_0 = this.$8;
            if ($v_0.parentElement) {
                var $v_2 = $v_0.parentElement();
                var $v_3 = RTE.DomHelper.createRange(window.document);
                if ($v_2) {
                    try { // Added Try-Catch block around code that causes the JavaScript error to occur
                        $v_3.moveToElementText($v_2);
                    }
                    catch (err) {
                        // DO NOTHING!
                    }
                }
                while ($v_2 && !$v_3.inRange($v_0)) {
                    $v_2 = $v_2.parentNode;
                    try {
                        $v_3.moveToElementText($v_2);
                    }
                    catch ($$e_3_0) {
                    }
                }
                if ($v_2) {
                    var $v_4 = true;
                    while ($v_4) {
                        $v_4 = false;
                        var $v_5 = $v_2.childNodes.length;
                        for (var $v_6 = 0; $v_6 < $v_5; $v_6++) {
                            if (!SP.UI.UIUtility.isTextNode($v_2.childNodes[$v_6])) {
                                $v_3.moveToElementText($v_2.childNodes[$v_6]);
                                if ($v_3.inRange($v_0)) {
                                    $v_2 = $v_2.childNodes[$v_6];
                                    $v_4 = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                return $v_2;
            }
            var $v_1 = $v_0.commonAncestorContainer;
            if ($v_1) {
                return $v_1;
            }
            return null;
        });
    }
}

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

In my project, I am loading this JavaScript on every page using an AdditionalPageHead control.

Hopefully you find this useful.


Viewing all articles
Browse latest Browse all 10

Trending Articles