Skip to content

Commit 9e246dd

Browse files
gibson042dmethvin
authored andcommitted
Fix #12350: jQuery.trim should remove BOM
1 parent 465959e commit 9e246dd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/core.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ var
3737
core_rnotwhite = /\S/,
3838
core_rspace = /\s+/,
3939

40-
// IE doesn't match non-breaking spaces with \s
41-
rtrim = core_rnotwhite.test("\xA0") ? (/^[\s\xA0]+|[\s\xA0]+$/g) : /^\s+|\s+$/g,
40+
// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
41+
rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
4242

4343
// A simple way to check for HTML strings
4444
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
@@ -605,7 +605,7 @@ jQuery.extend({
605605
},
606606

607607
// Use native String.trim function wherever possible
608-
trim: core_trim ?
608+
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
609609
function( text ) {
610610
return text == null ?
611611
"" :

test/unit/core.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ test("noConflict", function() {
264264
});
265265

266266
test("trim", function() {
267-
expect(9);
267+
expect(13);
268268

269269
var nbsp = String.fromCharCode(160);
270270

@@ -278,6 +278,11 @@ test("trim", function() {
278278
equal( jQuery.trim( null ), "", "Null" );
279279
equal( jQuery.trim( 5 ), "5", "Number" );
280280
equal( jQuery.trim( false ), "false", "Boolean" );
281+
282+
equal( jQuery.trim(" "), "", "space should be trimmed" );
283+
equal( jQuery.trim("ipad\xA0"), "ipad", "nbsp should be trimmed" );
284+
equal( jQuery.trim("\uFEFF"), "", "zwsp should be trimmed" );
285+
equal( jQuery.trim("\uFEFF \xA0! | \uFEFF"), "! |", "leading/trailing should be trimmed" );
281286
});
282287

283288
test("type", function() {

0 commit comments

Comments
 (0)