diff options
author | Timmy Willison <timmywillisn@gmail.com> | 2015-10-18 15:50:43 -0400 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2015-10-25 15:15:59 -0400 |
commit | ada073e9acfc9a103b13b9b69d26590e1c834d04 (patch) | |
tree | e8db8b10754c442c8e86ad60d0d5623b58d1b624 | |
parent | cf4092eeea01cd7bc87f1e99e79c3bb2bfcd7d8a (diff) | |
download | jquery-ada073e9acfc9a103b13b9b69d26590e1c834d04.tar.gz jquery-ada073e9acfc9a103b13b9b69d26590e1c834d04.zip |
Core: make isNumeric limited to strings and numbers
Fixes gh-2662
(cherry picked from commit 15ac848868e993dfe5ccd7751a94f5c8edc288bc)
-rw-r--r-- | src/core.js | 12 | ||||
-rw-r--r-- | test/unit/core.js | 2 |
2 files changed, 7 insertions, 7 deletions
diff --git a/src/core.js b/src/core.js index 4a1820b3d..d7a6b9769 100644 --- a/src/core.js +++ b/src/core.js @@ -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 ) { diff --git a/test/unit/core.js b/test/unit/core.js index c0edd3dcd..768519889 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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" ); |