Skip to content

Commit

Permalink
Switch to using isNaN instead of RegExp for data-.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Sep 21, 2010
1 parent e0b2430 commit 8ebb9b2
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/data.js
@@ -1,7 +1,6 @@
(function( jQuery ) { (function( jQuery ) {


var windowData = {}, var windowData = {},
rnum = /^-?\d+(?:\.\d+)?(?:E\d+)?$/,
rbrace = /^(?:{.*}|\[.*\])$/; rbrace = /^(?:{.*}|\[.*\])$/;


jQuery.extend({ jQuery.extend({
Expand Down Expand Up @@ -158,7 +157,7 @@ jQuery.fn.extend({
data = data === "true" ? true : data = data === "true" ? true :
data === "false" ? false : data === "false" ? false :
data === "null" ? null : data === "null" ? null :
rnum.test( data ) ? parseFloat( data ) : !isNaN( data ) ? parseFloat( data ) :

This comment has been minimized.

Copy link
@cms

cms Sep 22, 2010

A small problem with isNaN and strings is that the Numeric String Grammar used by the internal ToNumber operation -which isNaN also uses- when applied on string values considers that a string which is empty or contains only whitespace characters is converted to 0.

Therefore isNaN("") == false and isNaN(" ") == false, and that can give a problem when a data-* attribute contains such empty string, it will be detected as if it were a number, but parseFloat will actually produce NaN.

I've posted an example with a recent build.

This comment has been minimized.

Copy link
@jeresig

jeresig Sep 22, 2010

Author Member

Good call! Fixed: 9ad7c21

rbrace.test( data ) ? jQuery.parseJSON( data ) : rbrace.test( data ) ? jQuery.parseJSON( data ) :
data; data;
} catch( e ) {} } catch( e ) {}
Expand Down

1 comment on commit 8ebb9b2

@mdumic
Copy link

@mdumic mdumic commented on 8ebb9b2 Sep 21, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint complains of unescaped curly braces in regexp in line: rbrace = /^(?:{.*}|\[.*\])$/;.
Should be: rbrace = /^(?:\{.*\}|\[.*\])$/;

Please sign in to comment.