github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jquery / jquery

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1,856
    • 174
  • Source
  • Commits
  • Network (174)
  • Graphs
  • Tree: 141ad3c

click here to add a description

click here to add a homepage

  • Switch Branches (3)
    • master
    • mobile
    • omgrequire
  • Switch Tags (41)
    • 1.4rc1
    • 1.4a2
    • 1.4a1
    • 1.4.2
    • 1.4.1
    • 1.4
    • 1.3rc1
    • 1.3b2
    • 1.3b1
    • 1.3.2
    • 1.3.1rc1
    • 1.3.1
    • 1.3
    • 1.2.6
    • 1.2.5
    • 1.2.4b
    • 1.2.4a
    • 1.2.4
    • 1.2.3b
    • 1.2.3a
    • 1.2.3
    • 1.2.2b2
    • 1.2.2b
    • 1.2.2
    • 1.2.1
    • 1.2
    • 1.1b
    • 1.1a
    • 1.1.4
    • 1.1.3a
    • 1.1.3.1
    • 1.1.3
    • 1.1.2
    • 1.1.1
    • 1.1
    • 1.0a
    • 1.0.4
    • 1.0.3
    • 1.0.2
    • 1.0.1
    • 1.0
  • Comments
Sending Request…

jQuery JavaScript Library — Read more

  Cancel

http://jquery.com/

  Cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

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.
jeresig (author)
Tue Mar 09 06:14:27 -0800 2010
commit  141ad3c3e21e7734e67e37b5fb39782fe11b3c18
tree    cd4b57ea632b6767034e53bd20ba1261a09e51f2
parent  0a307b332e896b6b480952abb5d3bf03819893c8
M src/core.js 27 ••••
M src/support.js 15 ••••
M test/unit/core.js 18 ••••
0
src/core.js
...
27
28
29
30
 
 
31
32
33
...
567
568
569
570
571
572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
574
575
...
720
721
722
 
 
 
 
 
 
 
723
724
725
...
27
28
29
 
30
31
32
33
34
...
568
569
570
 
 
 
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
...
732
733
734
735
736
737
738
739
740
741
742
743
744
0
@@ -27,7 +27,8 @@ var jQuery = function( selector, context ) {
0
   rnotwhite = /\S/,
0
 
0
   // Used for trimming whitespace
0
-  rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
0
+  trimLeft = /^\s+/,
0
+  trimRight = /\s+$/,
0
 
0
   // Match a standalone tag
0
   rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
0
@@ -567,9 +568,20 @@ jQuery.extend({
0
     return object;
0
   },
0
 
0
-  trim: function( text ) {
0
-    return (text || "").replace( rtrim, "" );
0
-  },
0
+  // Use native String.trim function wherever possible
0
+  trim: String.trim ?
0
+    function( text ) {
0
+      return text == null ?
0
+        "" :
0
+        String.trim( text );
0
+    } :
0
+
0
+    // Otherwise use our own trimming functionality
0
+    function( text ) {
0
+      return text == null ?
0
+        "" :
0
+        text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
0
+    },
0
 
0
   // results is for internal usage only
0
   makeArray: function( array, results ) {
0
@@ -720,6 +732,13 @@ if ( indexOf ) {
0
   };
0
 }
0
 
0
+// Verify that \s matches non-breaking spaces
0
+// (IE fails on this test)
0
+if ( !/\s/.test( "\xA0" ) ) {
0
+  trimLeft = /^[\s\xA0]+/;
0
+  trimRight = /[\s\xA0]+$/;
0
+}
0
+
0
 // All jQuery objects should point back to these
0
 rootjQuery = jQuery(document);
0
 
0
src/support.js
...
56
57
58
59
60
61
62
63
64
65
...
69
70
71
72
 
73
74
75
...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
120
121
122
123
124
125
126
...
56
57
58
 
 
59
 
60
61
62
...
66
67
68
 
69
70
71
72
...
78
79
80
 
 
 
 
 
 
 
 
 
81
82
83
...
108
109
110
 
111
112
113
0
@@ -56,10 +56,7 @@
0
     // (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
0
     optSelected: document.createElement("select").appendChild( document.createElement("option") ).selected,
0
 
0
-    parentNode: div.removeChild( div.appendChild( document.createElement("div") ) ).parentNode === null,
0
-
0
     // Will be defined later
0
-    deleteExpando: true,
0
     checkClone: false,
0
     scriptEval: false,
0
     noCloneEvent: true,
0
@@ -69,7 +66,7 @@
0
   script.type = "text/javascript";
0
   try {
0
     script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
0
-  } catch( scriptError ) {}
0
+  } catch(e) {}
0
 
0
   root.insertBefore( script, root.firstChild );
0
 
0
@@ -81,15 +78,6 @@
0
     delete window[ id ];
0
   }
0
 
0
-  // Test to see if it's possible to delete an expando from an element
0
-  // Fails in Internet Explorer
0
-  try {
0
-    delete script.test;
0
-  
0
-  } catch( expandoError ) {
0
-    jQuery.support.deleteExpando = false;
0
-  }
0
-
0
   root.removeChild( script );
0
 
0
   if ( div.attachEvent && div.fireEvent ) {
0
@@ -120,7 +108,6 @@
0
     document.body.appendChild( div );
0
     jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
0
     document.body.removeChild( div ).style.display = 'none';
0
-
0
     div = null;
0
   });
0
 
0
test/unit/core.js
...
201
202
203
204
 
205
206
 
207
208
209
210
211
 
 
 
 
 
 
 
 
 
 
212
213
214
...
201
202
203
 
204
205
 
206
207
 
 
 
 
208
209
210
211
212
213
214
215
216
217
218
219
220
0
@@ -201,14 +201,20 @@ test("noConflict", function() {
0
 });
0
 
0
 test("trim", function() {
0
-  expect(4);
0
+  expect(9);
0
 
0
-  var nbsp = String.fromCharCode(160);
0
+  var nbsp = String.fromCharCode(160);
0
 
0
-  equals( jQuery.trim("hello  "), "hello", "trailing space" );
0
-  equals( jQuery.trim("  hello"), "hello", "leading space" );
0
-  equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
0
-  equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
0
+  equals( jQuery.trim("hello  "), "hello", "trailing space" );
0
+  equals( jQuery.trim("  hello"), "hello", "leading space" );
0
+  equals( jQuery.trim("  hello   "), "hello", "space on both sides" );
0
+  equals( jQuery.trim("  " + nbsp + "hello  " + nbsp + " "), "hello", "&nbsp;" );
0
+
0
+  equals( jQuery.trim(), "", "Nothing in." );
0
+  equals( jQuery.trim( undefined ), "", "Undefined" );
0
+  equals( jQuery.trim( null ), "", "Null" );
0
+  equals( jQuery.trim( 5 ), "5", "Number" );
0
+  equals( jQuery.trim( false ), "false", "Boolean" );
0
 });
0
 
0
 test("isPlainObject", function() {

Comments

DBJDBJ Wed Mar 10 04:58:07 -0800 2010

@john : thanks for commendation ;o)

one note ? to avoid all browsers ES5 transitional period pains, I would rather test for trim() existence like so :

 var native_trim = "function" === typeof "".trim ;

Some browsers support and implement String.prototype.trim, but not String.trim, I think ?

Thanks: DBJ

DBJDBJ Wed Mar 10 05:02:39 -0800 2010

Also , I seem to remember there is a trim left and/or trim right somewhere in jQuery which now can be changed to use trimLeft ot trimRight , regular expressions ?

Thanks: Dusan

jeresig Wed Mar 10 08:01:56 -0800 2010

@DBJDBJ: I'm not sure what you mean regarding String.prototype.trim - we don't use it, we're only using the native String.trim method, so it's best for us to check to see if that method exists. Also, there is no left/right trim in jQuery - only the one trim.

DBJDBJ Wed Mar 10 09:01:45 -0800 2010

Sorry I was to busy for a comprehensive comment.

" .... Also, there is no left/right trim in jQuery - only the one trim....

inside jQuery.extend we have this comment :

          // Trim whitespace, otherwise indexOf won't work as expected

And this declaration :

        rleadingWhitespace = /^\s+/,

Which now obviously should be removed and (tne new and correct)

         trimLeft 

Should be used, I think ?

Second. String.prototype.trim ---- v.s.--- String.trim

               15.5.4.20 String.prototype.trim ( )

The above is the only legal ES5 , trim() . There is no String.trim() in ES5. This is why I would rather not use it ....

Thanks: DBJ

DBJDBJ Wed Mar 10 09:47:14 -0800 2010

Results from http://jsbin.com/ehoje/12
Opera

    userAgent :Opera/9.80 (Windows NT 6.1; U; en) Presto/2.6.22 Version/10.50
   String.trim() → undefined
   String.protoype.trim() → function trim() { [native code] }

Safari

          userAgent :Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10
         String.trim() → undefined
         String.protoype.trim() → undefined

Chrome

        userAgent :Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.2 Safari/533.2
       String.trim() → undefined
       String.protoype.trim() → function trim() { [native code] }

FireFox

      userAgent :Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6
     String.trim() → function trim() { [native code] }
     String.protoype.trim() → function trim() { [native code] }

With IE I have not bothered ...

I really think we should not rely on String.trim() , being universaly avaialble

Thanks: DBJ

DBJDBJ Thu Mar 11 06:43:03 -0800 2010

I hope it is not patronising if I offer this : http://gist.github.com/329172

Thanks : DBJ

Please log in to comment.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server