Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JQuery(..).html() buggy in jQuery 1.3.2?

Tags:

jquery

The code here does not return what one expects:

jQuery('<div>Look here: [ jQuery0="null" ]</div>').html()

Rather, you get:

Look here: [ ]

The jQuery source code in question:

html: function( value ) {
        return value === undefined ?
                (this[0] ?
                        this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
                        null) :
                this.empty().append( value );
},

What would be the motivation behind the .replace? I do not have time to go over the rest of jQuery, but code like this makes me wonder if I should use jQuery in production at all.

like image 327
t0yv0 Avatar asked Aug 31 '09 11:08

t0yv0


2 Answers

What would be the motivation behind the .replace?

To hide attributes that jQuery is using for internal purposes.

code like this makes me wonder if I should use jQuery in production at all.

Yes, I had the exact same reaction. It's just incredibly sloppy. Trying to process HTML with regexp is the kind of naïve hack you expect from first-time question posters, not the sort of behaviour you'd hope to see in the framework so many SO users appear to worship.

It's not the only place where jQuery trips up by trying to parse markup with regex; some of the selector stuff is broken too. These may be obscure corner cases, but to me it's a huge red flag indicative of the wrong approach.

like image 169
bobince Avatar answered Nov 17 '22 18:11

bobince


This code is new in 1.3.2, it wasn't in 1.3.1. It looks to me like jQuery uses attributes whose names start with "jQuery" to store data on elements, and this is its way of not exposing that to you when you ask for the html back.

It clearly isn't a bug. The author intended to remove that HTML before returning you the string.

Does this affect your code? As with any library, you should test your production code thoroughly before deploying it.

like image 42
Ned Batchelder Avatar answered Nov 17 '22 19:11

Ned Batchelder