Skip to content

Remove BOM in jQuery.trim. #897

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

Closed
wants to merge 1 commit into from
Closed

Conversation

wwalser
Copy link

@wwalser wwalser commented Aug 17, 2012

In response to pull request #896, see discussion.

This adds 7 bytes


equal( jQuery.trim(" ").length, 0, "space should be trimmed");
equal( jQuery.trim("\xA0").length, 0, "nbsp should be trimmed");
equal( jQuery.trim("\uFEFF").length, 0, "zwsp should be trimmed");
Copy link
Member

Choose a reason for hiding this comment

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

Just curious as to the reasoning for not comparing to the empty string. If trim was to return an empty array when it encounters a BOM character, these tests would still pass.

Also, it would probably be nice to have tests against strings that would not end up empty, like "\xA0hello" and some other variations.

Copy link
Author

Choose a reason for hiding this comment

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

I suddenly understand why the typed language guys are all high and mighty. Yeah, I'll assert against the empty string.

Yup, I'll write an assertion or three that doesn't trim to zero length.

Thanks @jaubourg

@FagnerMartinsBrack
Copy link

Since jQuery.trim uses a polyfill for String.trim it would be nice if it could match the full spec.
If this is used just internally for jQuery as @dmethvin said there's no reason to expose as an utility, right?

@timmywil
Copy link
Member

Let's not go overboard here. jQuery.trim is a perfectly useful public utility that happens to utilize native trim where it can. We don't need to drop it just because it doesn't trim all random unicode space characters in every browser. Let's be honest and make up statistics. 99.9% of users don't care about these characters. They care about " ".

@dmethvin
Copy link
Member

Since jQuery.trim uses a polyfill for String.trim

The documentation says nothing about this.

If this is used just internally for jQuery as @dmethvin said

I said " jQuery's internal utility methods are often intended for specific situations inside the library where the complex edge cases don't matter." It wasn't our intention to expose a polyfill of String.prototype.trim under the name of $.trim and be tied to a spec that requres things we don't need internally.

there's no reason to expose as an utility, right?

The only reason to continue to expose it is because the cow has left the barn, the cat is out of the bag, the condor has taken flight. We definitely would not have exposed it if we understood the level of bikeshedding it would induce.

@FagnerMartinsBrack
Copy link

The documentation says nothing about this.
99.9% of users don't care about these characters

Sure, that was just a personal thought based in the source and in the fact that is a public api...

@wwalser
Copy link
Author

wwalser commented Aug 18, 2012

Sorry to have brought the bikeshed back to the forefront. I obviously can't speak for the entire community but if this were moved out of core and into a plugin that wouldn't seem inappropriate.

@jaubourg
Copy link
Member

@wwalser Handling BOM seems like a reasonable endeavor to me. It's the idea of a full-fledged trim polyfill for the sake of it that's a bit overkill.

So thanks for the PR ;)

@dmethvin dmethvin closed this in 2b5b4eb Aug 20, 2012
@dmethvin
Copy link
Member

I updated the tests and landed this so it would get into 1.8.1, thanks!

dmethvin added a commit that referenced this pull request Aug 20, 2012
This reverts commit 2b5b4eb.

String.prototype.trim doesn't trim BOM in Safari 5.0 so this won't work without additional feature detects.

http://swarm.jquery.org/result/165379
@dmethvin dmethvin reopened this Aug 20, 2012
@dmethvin
Copy link
Member

Breaks in Safari 5.0. So now we not only need to detect a missing String.prototype.trim, but we need to detect a broken String.prototype.trim. I fear it will take some bytes to do this. http://swarm.jquery.org/result/165379

@gibson042
Copy link
Member

Wait, are you saying that Safari 5.0 recognizes BOM as whitespace in the regex engine but doesn't trim it?

@jaubourg
Copy link
Member

Seems like it... makes you wonder about this BOM/NBSP feature inference now, doesn't it? :P

@wwalser
Copy link
Author

wwalser commented Aug 21, 2012

Maybe. The new inference that if \s recognizes \xA0 it will also recognize \uFEFF may have failed or the existing inference that regexs \s recognition is the same as what's trimmed by prototype.trim might have failed. We can't actually tell from this test result.

It's not actually any of the inferences that are hurting us here. As Dave pointed out it's the existence of a String.prototype.trim which is broken that's hurting us.

    trim: core_trim ?
        function( text ) {
            return text == null ?
                "" :
                core_trim.call( text );
        } :

        // Otherwise use our own trimming functionality
        function( text ) {
            return text == null ?
                "" :
                text.toString().replace( rtrim, "" );
        },

core_trim is true. rtrim, the result of said inference is never used. String.prototype.trim exists and is not implemented to spec. Unfortunate, unsurprising, and true.

This would require additional bytes, scrap it.

Thanks guys! sorry it didn't work out.

@jaubourg
Copy link
Member

@wwalser the remark shouldn't be taken literally. What I'm saying is that, when you cannot even trust standard methods to be implemented correctly, you're probably better off not making any inference in your code, period.

It's not about the browsers that jQuery supports today, it's about the ones it will have to support tomorrow (FF & Chrome are very aggressive and very breaking in that department). Everytime we release a new version, we hope for it to be the latest in it's 1.x branch. If such is the case, then some sites will have this version for a looooong time (1.4.4 anyone?). Now, comes along a browser that does break a single assumption...

Feature detection should be as bullet proof as humanly possible (as in testing for the exact blank the workaround is supposed to fill), feature inference should be avoided at all cost, since it doesn't test for the blank itself and, thus, is just browser sniffing in disguise, even if it actually sniffs a large range of browsers: "this is true, and in every browser I know I can deduce this totally unrelated thing is true too so it will always be true no matter what".

I find it sad that we could actually feature detect here but just refuse to do so because "we know of no browser that would break the assumption" or "it saves 4 bytes". That doesn't sit well with me regarding how jQuery is supposed to endure a little past the few browser versions it supports right this instant.

And I know we cannot safeguard against all future bugs, but I sure hope we can properly handle the ones we fix today so that the code is adapted to variations on the same theme.

I hope I made my reasoning a little clearer.

@dmethvin
Copy link
Member

Landed gh-902 instead. @wwalser thanks for your work here, I hope it's a little clearer what we go through for even the simplest changes. 😓 When we're supporting so many (broken) browsers, nothing is simple! We are hoping to have automatic Testswarm runs for pull requests soon, which would have identified the problem with Safari 5.0 before the code landed in master.

@dmethvin dmethvin closed this Aug 21, 2012
@wwalser
Copy link
Author

wwalser commented Aug 22, 2012

Nice, thanks for the update.

mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
mescoda pushed a commit to mescoda/jquery that referenced this pull request Nov 4, 2014
This reverts commit 2b5b4eb.

String.prototype.trim doesn't trim BOM in Safari 5.0 so this won't work without additional feature detects.

http://swarm.jquery.org/result/165379
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants