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 #2279, #4452, and #4835.

This commit is contained in:
jeresig 2010-03-09 09:14:27 -05:00
parent 0a307b332e
commit 141ad3c3e2
3 changed files with 36 additions and 24 deletions

View 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);