Skip to content

Commit 141ad3c

Browse files
committed
Landing a faster trim method. Based upon the work by Travis Hardiman and DBJDBJ. More details here: http://forum.jquery.com/topic/faster-jquery-trim Fixes jquery#2279, jquery#4452, and jquery#4835.
1 parent 0a307b3 commit 141ad3c

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/core.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ var jQuery = function( selector, context ) {
2727
rnotwhite = /\S/,
2828

2929
// Used for trimming whitespace
30-
rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
30+
trimLeft = /^\s+/,
31+
trimRight = /\s+$/,
3132

3233
// Match a standalone tag
3334
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
@@ -567,9 +568,20 @@ jQuery.extend({
567568
return object;
568569
},
569570

570-
trim: function( text ) {
571-
return (text || "").replace( rtrim, "" );
572-
},
571+
// Use native String.trim function wherever possible
572+
trim: String.trim ?
573+
function( text ) {
574+
return text == null ?
575+
"" :
576+
String.trim( text );
577+
} :
578+
579+
// Otherwise use our own trimming functionality
580+
function( text ) {
581+
return text == null ?
582+
"" :
583+
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
584+
},
573585

574586
// results is for internal usage only
575587
makeArray: function( array, results ) {
@@ -720,6 +732,13 @@ if ( indexOf ) {
720732
};
721733
}
722734

735+
// Verify that \s matches non-breaking spaces
736+
// (IE fails on this test)
737+
if ( !/\s/.test( "\xA0" ) ) {
738+
trimLeft = /^[\s\xA0]+/;
739+
trimRight = /[\s\xA0]+$/;
740+
}
741+
723742
// All jQuery objects should point back to these
724743
rootjQuery = jQuery(document);
725744

src/support.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@
5656
// (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
5757
optSelected: document.createElement("select").appendChild( document.createElement("option") ).selected,
5858

59-
parentNode: div.removeChild( div.appendChild( document.createElement("div") ) ).parentNode === null,
60-
6159
// Will be defined later
62-
deleteExpando: true,
6360
checkClone: false,
6461
scriptEval: false,
6562
noCloneEvent: true,
@@ -69,7 +66,7 @@
6966
script.type = "text/javascript";
7067
try {
7168
script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
72-
} catch( scriptError ) {}
69+
} catch(e) {}
7370

7471
root.insertBefore( script, root.firstChild );
7572

@@ -81,15 +78,6 @@
8178
delete window[ id ];
8279
}
8380

84-
// Test to see if it's possible to delete an expando from an element
85-
// Fails in Internet Explorer
86-
try {
87-
delete script.test;
88-
89-
} catch( expandoError ) {
90-
jQuery.support.deleteExpando = false;
91-
}
92-
9381
root.removeChild( script );
9482

9583
if ( div.attachEvent && div.fireEvent ) {
@@ -120,7 +108,6 @@
120108
document.body.appendChild( div );
121109
jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
122110
document.body.removeChild( div ).style.display = 'none';
123-
124111
div = null;
125112
});
126113

test/unit/core.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,20 @@ test("noConflict", function() {
201201
});
202202

203203
test("trim", function() {
204-
expect(4);
204+
expect(9);
205205

206-
var nbsp = String.fromCharCode(160);
206+
var nbsp = String.fromCharCode(160);
207207

208-
equals( jQuery.trim("hello "), "hello", "trailing space" );
209-
equals( jQuery.trim(" hello"), "hello", "leading space" );
210-
equals( jQuery.trim(" hello "), "hello", "space on both sides" );
211-
equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", "&nbsp;" );
208+
equals( jQuery.trim("hello "), "hello", "trailing space" );
209+
equals( jQuery.trim(" hello"), "hello", "leading space" );
210+
equals( jQuery.trim(" hello "), "hello", "space on both sides" );
211+
equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", "&nbsp;" );
212+
213+
equals( jQuery.trim(), "", "Nothing in." );
214+
equals( jQuery.trim( undefined ), "", "Undefined" );
215+
equals( jQuery.trim( null ), "", "Null" );
216+
equals( jQuery.trim( 5 ), "5", "Number" );
217+
equals( jQuery.trim( false ), "false", "Boolean" );
212218
});
213219

214220
test("isPlainObject", function() {

0 commit comments

Comments
 (0)