aboutsummaryrefslogtreecommitdiffstats
path: root/src/core.js
diff options
context:
space:
mode:
authorjeresig <jeresig@gmail.com>2010-03-09 09:14:27 -0500
committerjeresig <jeresig@gmail.com>2010-03-09 09:14:27 -0500
commit141ad3c3e21e7734e67e37b5fb39782fe11b3c18 (patch)
treecd4b57ea632b6767034e53bd20ba1261a09e51f2 /src/core.js
parent0a307b332e896b6b480952abb5d3bf03819893c8 (diff)
downloadjquery-141ad3c3e21e7734e67e37b5fb39782fe11b3c18.tar.gz
jquery-141ad3c3e21e7734e67e37b5fb39782fe11b3c18.zip
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.
Diffstat (limited to 'src/core.js')
-rw-r--r--src/core.js27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/core.js b/src/core.js
index 50e167657..3f156a5fd 100644
--- a/src/core.js
+++ b/src/core.js
@@ -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);