Landing a faster trim method. Based upon the work by Travis Hardiman and DBJDBJ....
authorjeresig <jeresig@gmail.com>
Tue, 9 Mar 2010 14:14:27 +0000 (09:14 -0500)
committerjeresig <jeresig@gmail.com>
Tue, 9 Mar 2010 14:14:27 +0000 (09:14 -0500)
src/core.js
src/support.js
test/unit/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);
 
index f9429f9..befc532 100644 (file)
                // (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
                optSelected: document.createElement("select").appendChild( document.createElement("option") ).selected,
 
-               parentNode: div.removeChild( div.appendChild( document.createElement("div") ) ).parentNode === null,
-
                // Will be defined later
-               deleteExpando: true,
                checkClone: false,
                scriptEval: false,
                noCloneEvent: true,
@@ -69,7 +66,7 @@
        script.type = "text/javascript";
        try {
                script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
-       } catch( scriptError ) {}
+       } catch(e) {}
 
        root.insertBefore( script, root.firstChild );
 
                delete window[ id ];
        }
 
-       // Test to see if it's possible to delete an expando from an element
-       // Fails in Internet Explorer
-       try {
-               delete script.test;
-       
-       } catch( expandoError ) {
-               jQuery.support.deleteExpando = false;
-       }
-
        root.removeChild( script );
 
        if ( div.attachEvent && div.fireEvent ) {
                document.body.appendChild( div );
                jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
                document.body.removeChild( div ).style.display = 'none';
-
                div = null;
        });
 
index d8aba16..eccf544 100644 (file)
@@ -201,14 +201,20 @@ test("noConflict", function() {
 });
 
 test("trim", function() {
-  expect(4);
+       expect(9);
 
-  var nbsp = String.fromCharCode(160);
+       var nbsp = String.fromCharCode(160);
 
-  equals( jQuery.trim("hello  "), "hello", "trailing space" );
-  equals( jQuery.trim("  hello"), "hello", "leading space" );
-  equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
-  equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
+       equals( jQuery.trim("hello  "), "hello", "trailing space" );
+       equals( jQuery.trim("  hello"), "hello", "leading space" );
+       equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
+       equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
+
+       equals( jQuery.trim(), "", "Nothing in." );
+       equals( jQuery.trim( undefined ), "", "Undefined" );
+       equals( jQuery.trim( null ), "", "Null" );
+       equals( jQuery.trim( 5 ), "5", "Number" );
+       equals( jQuery.trim( false ), "false", "Boolean" );
 });
 
 test("isPlainObject", function() {