Switch to using String.prototype.trim from String.trim as it's more-widely available.
[jquery.git] / src / core.js
index 02d8269..699b87a 100644 (file)
@@ -18,7 +18,7 @@ var jQuery = function( selector, context ) {
 
        // A simple way to check for HTML strings or ID strings
        // (both of which we optimize for)
-       quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/,
+       quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/,
 
        // Is it a simple selector
        isSimple = /^.[^:#\[\.,]*$/,
@@ -27,7 +27,8 @@ var jQuery = function( selector, context ) {
        rnotwhite = /\S/,
 
        // Used for trimming whitespace
-       rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
+       trimLeft = /^\s+/,
+       trimRight = /\s+$/,
 
        // Match a standalone tag
        rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
@@ -49,9 +50,10 @@ var jQuery = function( selector, context ) {
 
        // Save a reference to some core methods
        toString = Object.prototype.toString,
-       hasOwnProperty = Object.prototype.hasOwnProperty,
+       hasOwn = Object.prototype.hasOwnProperty,
        push = Array.prototype.push,
        slice = Array.prototype.slice,
+       trim = String.prototype.trim,
        indexOf = Array.prototype.indexOf;
 
 jQuery.fn = jQuery.prototype = {
@@ -450,9 +452,9 @@ jQuery.extend({
                }
                
                // Not own constructor property must be Object
-               if ( obj.constructor
-                       && !hasOwnProperty.call(obj, "constructor")
-                       && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
+               if ( obj.constructor &&
+                       !hasOwn.call(obj, "constructor") &&
+                       !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
                        return false;
                }
                
@@ -462,7 +464,7 @@ jQuery.extend({
                var key;
                for ( key in obj ) {}
                
-               return key === undefined || hasOwnProperty.call( obj, key );
+               return key === undefined || hasOwn.call( obj, key );
        },
 
        isEmptyObject: function( obj ) {
@@ -480,6 +482,9 @@ jQuery.extend({
                if ( typeof data !== "string" || !data ) {
                        return null;
                }
+
+               // Make sure leading/trailing whitespace is removed (IE can't handle it)
+               data = jQuery.trim( data );
                
                // Make sure the incoming data is actual JSON
                // Logic borrowed from http://json.org/json2.js
@@ -564,9 +569,20 @@ jQuery.extend({
                return object;
        },
 
-       trim: function( text ) {
-               return (text || "").replace( rtrim, "" );
-       },
+       // Use native String.trim function wherever possible
+       trim: trim ?
+               function( text ) {
+                       return text == null ?
+                               "" :
+                               trim.call( text );
+               } :
+
+               // Otherwise use our own trimming functionality
+               function( text ) {
+                       return text == null ?
+                               "" :
+                               text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
+               },
 
        // results is for internal usage only
        makeArray: function( array, results ) {
@@ -620,12 +636,14 @@ jQuery.extend({
        },
 
        grep: function( elems, callback, inv ) {
-               var ret = [];
+               var ret = [], retVal;
+               inv = !!inv;
 
                // Go through the array, only saving the items
                // that pass the validator function
                for ( var i = 0, length = elems.length; i < length; i++ ) {
-                       if ( !inv !== !callback( elems[ i ], i ) ) {
+                       retVal = !!callback( elems[ i ], i );
+                       if ( inv !== retVal ) {
                                ret.push( elems[ i ] );
                        }
                }
@@ -690,7 +708,7 @@ jQuery.extend({
                        /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
                        /(msie) ([\w.]+)/.exec( ua ) ||
                        !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
-                       [];
+                       [];
 
                return { browser: match[1] || "", version: match[2] || "0" };
        },
@@ -715,6 +733,13 @@ if ( indexOf ) {
        };
 }
 
+// Verify that \s matches non-breaking spaces
+// (IE fails on this test)
+if ( !/\s/.test( "\xA0" ) ) {
+       trimLeft = /^[\s\xA0]+/;
+       trimRight = /[\s\xA0]+$/;
+}
+
 // All jQuery objects should point back to these
 rootjQuery = jQuery(document);
 
@@ -745,7 +770,7 @@ function doScrollCheck() {
                // If IE is used, use the trick by Diego Perini
                // http://javascript.nwbox.com/IEContentLoaded/
                document.documentElement.doScroll("left");
-       } catch( error ) {
+       } catch(e) {
                setTimeout( doScrollCheck, 1 );
                return;
        }
@@ -796,9 +821,9 @@ function access( elems, key, value, exec, fn, pass ) {
        }
        
        // Getting an attribute
-       return length ? fn( elems[0], key ) : null;
+       return length ? fn( elems[0], key ) : undefined;
 }
 
 function now() {
-       return (new Date).getTime();
+       return (new Date()).getTime();
 }