]> source.dussan.org Git - jquery.git/commitdiff
Fix #12350: jQuery.trim should remove BOM
authorRichard Gibson <richard.gibson@gmail.com>
Tue, 21 Aug 2012 12:59:51 +0000 (08:59 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Tue, 21 Aug 2012 12:59:51 +0000 (08:59 -0400)
src/core.js
test/unit/core.js

index e7b94acda9831bd7662865ab5af34c39848e6999..4652c5ad7d547923f831565fb1d206a1d9015f19 100644 (file)
@@ -37,8 +37,8 @@ var
        core_rnotwhite = /\S/,
        core_rspace = /\s+/,
 
-       // IE doesn't match non-breaking spaces with \s
-       rtrim = core_rnotwhite.test("\xA0") ? (/^[\s\xA0]+|[\s\xA0]+$/g) : /^\s+|\s+$/g,
+       // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
+       rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
 
        // A simple way to check for HTML strings
        // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
@@ -605,7 +605,7 @@ jQuery.extend({
        },
 
        // Use native String.trim function wherever possible
-       trim: core_trim ?
+       trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
                function( text ) {
                        return text == null ?
                                "" :
index 44920edcf9c13b60d8027798bc952d1b897b4fac..8b8b7afd80c4f9c192c44a3539d5261076d03269 100644 (file)
@@ -264,7 +264,7 @@ test("noConflict", function() {
 });
 
 test("trim", function() {
-       expect(9);
+       expect(13);
 
        var nbsp = String.fromCharCode(160);
 
@@ -278,6 +278,11 @@ test("trim", function() {
        equal( jQuery.trim( null ), "", "Null" );
        equal( jQuery.trim( 5 ), "5", "Number" );
        equal( jQuery.trim( false ), "false", "Boolean" );
+
+       equal( jQuery.trim(" "), "", "space should be trimmed" );
+       equal( jQuery.trim("ipad\xA0"), "ipad", "nbsp should be trimmed" );
+       equal( jQuery.trim("\uFEFF"), "", "zwsp should be trimmed" );
+       equal( jQuery.trim("\uFEFF \xA0! | \uFEFF"), "! |", "leading/trailing should be trimmed" );
 });
 
 test("type", function() {