]> source.dussan.org Git - jquery.git/commitdiff
Core: make isNumeric limited to strings and numbers
authorTimmy Willison <timmywillisn@gmail.com>
Sun, 18 Oct 2015 19:50:43 +0000 (15:50 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Sun, 25 Oct 2015 19:15:59 +0000 (15:15 -0400)
Fixes gh-2662

(cherry picked from commit 15ac848868e993dfe5ccd7751a94f5c8edc288bc)

src/core.js
test/unit/core.js

index 4a1820b3d38b1deaa23bcfb932f7fe7f430fe330..d7a6b97699be057ca3991fd6076e70c3f01fed2b 100644 (file)
@@ -217,12 +217,12 @@ jQuery.extend( {
 
        isNumeric: function( obj ) {
 
-               // parseFloat NaNs numeric-cast false positives (null|true|false|"")
-               // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
-               // subtraction forces infinities to NaN
-               // adding 1 corrects loss of precision from parseFloat (#15100)
-               var realStringObj = obj && obj.toString();
-               return !jQuery.isArray( obj ) && ( realStringObj - parseFloat( realStringObj ) + 1 ) >= 0;
+               // As of jQuery 3.0, isNumeric is limited to
+               // strings and numbers (primitives or objects)
+               // that can be coerced to finite numbers (gh-2662)
+               var type = jQuery.type( obj );
+               return ( type === "number" || type === "string" ) &&
+                       ( obj - parseFloat( obj ) + 1 ) >= 0;
        },
 
        isEmptyObject: function( obj ) {
index c0edd3dcdf28438bb119913d121505766cff1bf0..768519889e0d551c7af8b1649c052c91f86c16ab 100644 (file)
@@ -479,8 +479,8 @@ QUnit.test( "isNumeric", function( assert ) {
        assert.ok( t( 1.5999999999999999 ), "Very precise floating point number" );
        assert.ok( t( 8e5 ), "Exponential notation" );
        assert.ok( t( "123e-2" ), "Exponential notation string" );
-       assert.ok( t( new ToString( "42" ) ), "Custom .toString returning number" );
 
+       assert.equal( t( new ToString( "42" ) ), false, "Custom .toString returning number" );
        assert.equal( t( "" ), false, "Empty string" );
        assert.equal( t( "        " ), false, "Whitespace characters string" );
        assert.equal( t( "\t\t" ), false, "Tab characters string" );