Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12350: jQuery.trim should remove BOM #902

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -605,7 +605,7 @@ jQuery.extend({
},

// Use native String.trim function wherever possible
trim: core_trim ?
trim: core_trim && !core_trim.call("\uFEFF\xA0") ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gibson042 this is really nice :)

function( text ) {
return text == null ?
"" :
Expand Down
7 changes: 6 additions & 1 deletion test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ test("noConflict", function() {
});

test("trim", function() {
expect(9);
expect(13);

var nbsp = String.fromCharCode(160);

Expand All @@ -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() {
Expand Down