Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented Jan 20, 2022

Summary

This change makes jQuery skip falsy values in addClass( array )
& removeClass( array ) instead of stopping iteration when the first falsy
value is detected. This makes code like:

elem.addClass( [ "a", "", "b" ] );

add both the a & b classes.

The code was also optimized for size a bit.

Fixes gh-4998

+0 bytes thanks to the optimizations 🎉; initially it was at +8 bytes. In toggleClass we now also avoid needlessly recomputing the array of classes.

I'm yet to check how much can these optimizations be backported to 3.x-stable as we have some deprecated toggleClass logic there but I'll check after this lands.

Checklist

This change makes jQuery skip falsy values in `addClass( array )`
& `removeClass( array )` instead of stopping iteration when the first falsy
value is detected. This makes code like:
```js
elem.addClass( [ "a", "", "b" ] );
```
add both the `a` & `b` classes.

The code was also optimized for size a bit.

Fixes jquerygh-4998
@mgol mgol added the CSS label Jan 20, 2022
@mgol mgol added this to the 3.6.1 milestone Jan 20, 2022
@mgol mgol requested review from gibson042 and timmywil January 20, 2022 16:23
@mgol mgol added the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Jan 24, 2022
@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Jan 24, 2022
Copy link
Member

@timmywil timmywil left a comment

Choose a reason for hiding this comment

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

I like the change to use each.

@mgol mgol removed the Needs review label Jan 24, 2022
@mgol mgol merged commit a338b40 into jquery:main Jan 24, 2022
@mgol mgol deleted the addClass-falsy branch January 24, 2022 17:56
mgol added a commit that referenced this pull request Jan 24, 2022
This change makes jQuery skip falsy values in `addClass( array )`
& `removeClass( array )` instead of stopping iteration when the first falsy
value is detected. This makes code like:
```js
elem.addClass( [ "a", "", "b" ] );
```
add both the `a` & `b` classes.

The code was also optimized for size a bit so it doesn't increase the
minified gzipped size.

Fixes gh-4998
Closes gh-5003

(partially cherry picked from commit a338b40)
@mgol
Copy link
Member Author

mgol commented Jan 24, 2022

Landed on main in a338b40 and on 3.x-stable in a slightly modified version in 9b34bdb.

The 3.x-stable version has fewer modifications in toggleClass as it needs to support the deprecated value-less signature but even in that state it's also at +0 bytes.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Development

Successfully merging this pull request may close these issues.

addClass() called with an array doesn't add all of the classes if one of them is an empty string

2 participants