Skip to content
Permalink
Browse files

Switch to using isNaN instead of RegExp for data-.

  • Loading branch information...
jeresig committed Sep 21, 2010
1 parent e0b2430 commit 8ebb9b22df32fe5739a48087b6e33abb9f5cda49
Showing with 1 addition and 2 deletions.
  1. +1 −2 src/data.js
@@ -1,7 +1,6 @@
(function( jQuery ) {

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

jQuery.extend({
@@ -158,7 +157,7 @@ jQuery.fn.extend({
data = data === "true" ? true :
data === "false" ? false :
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 ) :
data;
} catch( e ) {}

1 comment on commit 8ebb9b2

@mdumic

This comment has been minimized.

Copy link

commented on 8ebb9b2 Sep 21, 2010

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

Please sign in to comment.
You can’t perform that action at this time.