Landing a faster trim method. Based upon the work by Travis Hardiman and DBJDBJ....
[jquery.git] / src / core.js
index 50e1676..3f156a5 100644 (file)
@@ -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>)?$/,
@@ -567,9 +568,20 @@ jQuery.extend({
                return object;
        },
 
-       trim: function( text ) {
-               return (text || "").replace( rtrim, "" );
-       },
+       // Use native String.trim function wherever possible
+       trim: String.trim ?
+               function( text ) {
+                       return text == null ?
+                               "" :
+                               String.trim( 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 ) {
@@ -720,6 +732,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);