Skip to content

Widget: Avoid adding extra spaces to the result of this._classes(...) #1372

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

Conversation

gabrielschulhof
Copy link

This is a PR on top of a PR (#1369).

@scottgonzalez
Copy link
Member

Why does this matter? Once the value is passed to .addClass() there won't be extraneous space anyway. And even if there was, I'm not sure why that would matter either.

@gabrielschulhof
Copy link
Author

Well, my Chrome Dev Tools show a superfluous space without this change, and no superfluous space with this change.

@gabrielschulhof
Copy link
Author

Good point though. I'll trace further into the code to see why the space survives .addClass().

@gabrielschulhof
Copy link
Author

OK, so when you do .addClass( this._classes( "some-structure-class" ) ) it does indeed work. Nevertheless, you can also use $( "<div class='" + this._classes( "some-structure-class" ) + "'></div>" );, and that should work too.

(edited)

@scottgonzalez
Copy link
Member

and that should work too

It does work.

@arschmitz
Copy link
Member

@gabrielschulhof your missing a " that should fail horribly :-)

@gabrielschulhof
Copy link
Author

http://jsbin.com/cefane/

@scottgonzalez
Copy link
Member

I'm not sure what you're trying to show. Nobody is saying there isn't a space, but that is extremely different than it not working.

@gabrielschulhof
Copy link
Author

Sorry! I was being ambiguous. By "working", I meant it should produce nice, clean output, just like core's class manipulation methods. I guess we can be lax about it but it looks extremely unprofessional when used in combination with HTML snippets. If core can take the trouble to make sure that class names in the class attribute are separated by exactly one space, with no leading or trailing whitespace, we should also take the same trouble.

// The empty string is a valid value for a class key, but pushing it into the array
// which we then join into a string below will result in our return value having
// superfluous spaces. Let's only add the value of the key if it's a non-empty string.
if ( typeof classes[ parts[ i ] ] === "string" && classes[ parts[ i ] ] !== "" ) {
Copy link
Member

Choose a reason for hiding this comment

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

Why can't this just be if ( classes[ parts[ i ] ] )?

Copy link
Author

Choose a reason for hiding this comment

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

I was actually hoping we might discuss this. I had exactly that simple check there at first, but then I thought to myself: do we want to be lax about checking the type here, or do we want to avoid ending up with "ns-structure-class [object Object]" or whatever, or things like "ns-structure-class true".

I mean, these are option values, so they come straight from the developer at worst (if the instance is created with options), or from the framework developer at best. Either way, we may want to consider them "outside, untrusted" or whatever.

Nevertheless, if we consider the simple check you mention to be sufficient, I'm certainly happy to update the PR.

Copy link
Member

Choose a reason for hiding this comment

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

garbage in = garbage out

Copy link
Author

Choose a reason for hiding this comment

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

So be it :)

Copy link
Member

Choose a reason for hiding this comment

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

Let's use the simple check and a short comment like this:

// Avoid adding extra spaces from empty parts
if ( classes[ parts[ i ] ] ) {

@gabrielschulhof gabrielschulhof force-pushed the classes-superfluous-spaces branch from 08c5b7d to 5404f7f Compare October 23, 2014 13:56
"test2": "class3"
}
},
_create: function() {
equal( this._classes( "test" ), "test class1 class2" );
equal( this._classes( "test2" ), "test2 class3" );
deepEqual( this._classes( "testEmpty" ), "testEmpty",
Copy link
Member

Choose a reason for hiding this comment

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

Use equal() to match the other assertions.

@gabrielschulhof gabrielschulhof force-pushed the classes-superfluous-spaces branch 2 times, most recently from 78b0e5e to 4613cec Compare October 23, 2014 14:03
@jzaefferer
Copy link
Member

Looks good to me.

@gabrielschulhof gabrielschulhof force-pushed the classes-superfluous-spaces branch from 4613cec to ba30279 Compare October 23, 2014 18:00
@scottgonzalez
Copy link
Member

@arschmitz has merged this locally; it will close when he pushes the rest of his updates for the classes branch.

@arschmitz
Copy link
Member

These changes are now part of PR #1369 so i'm going to close this.

@arschmitz arschmitz closed this Nov 5, 2014
@gabrielschulhof gabrielschulhof deleted the classes-superfluous-spaces branch June 14, 2016 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants