Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NicEdit Error in Chrome

I'm using the NicEdit WYSIWYG plugin on my site.

It's come to my attention that when NicEdit is instantiated in Chrome, the following Javascript error is generated:

Uncaught TypeError: Object  has no method 'createRange'

This doesn't stop the plugin from working, but I would like to prevent this if possible. Here is the offending method:

getRng : function() {
        var s = this.getSel();
        if(!s) { return null; }
        return (s.rangeCount > 0) ? s.getRangeAt(0) : s.createRange();
}

NicEdit seems to be pretty much dead as a project, which is why I am asking this question here instead of over at the NicEdit forums. I am hoping that someone knows of a 'quickfix' to this problem. In all other respects NicEdit works well for me, so I am reluctant to change over to a different WYISWYG plugin just yet...

Thanks (in advance) for your help.

like image 759
Trev Avatar asked Mar 21 '11 06:03

Trev


3 Answers

The problem is that the implementation of the selection object for Webkit does not define a createRange( ) method. That method seems to be specific to Internet Explorer. For Webkit and Gecko DOM implementations, the createRange( ) method is defined on the document object. With this knowledge, the fix for getRng( ) becomes:

getRng : function() {
    var s = this.getSel();
    var rng;        

    if(!s) { return null; } 
    if (s.rangeCount > 0) {
        rng = s.getRangeAt(0);
    } else if ( typeof s.createRange === 'undefined' ) {
        rng = document.createRange();
    } else {
        rng = s.createRange(); 
    }       
    return rng;
 },

I encountered this as I was evaluating a number of rich text editors for an upcoming project and had to create a sample page with nicEdit.

like image 96
P.J. Hinton Avatar answered Nov 20 '22 11:11

P.J. Hinton


The version at https://github.com/danishkhan/NicEdit contains this and other bugfixes.

This particular fix: https://github.com/danishkhan/NicEdit/commit/efa6a1e8867b745b841157e919a0055cb626d2c4

like image 27
Edward Anderson Avatar answered Nov 20 '22 10:11

Edward Anderson


Same code, written in nicEdit current design:

getRng : function() {
    var s = this.getSel();
    if(!s) { return null; }
    return (s.rangeCount > 0) ? s.getRangeAt(0) : (typeof s.createRange == 'undefined') ? document.createRange() : s.createRange();
},
like image 35
iMoses Avatar answered Nov 20 '22 10:11

iMoses