NicEdit Error in Chrome
Asked Answered
B

3

13

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.

Bacillary answered 21/3, 2011 at 6:50 Comment(0)
E
24

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.

Easterly answered 2/4, 2011 at 1:24 Comment(0)
M
16

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

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

Melodist answered 3/10, 2011 at 20:22 Comment(1)
Thanks! Anyone contacted the author to have these fixes merged?Heterolecithal
A
4

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();
},
Adrenocorticotropic answered 3/8, 2011 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.