diff options
author | Michał Gołębiowski-Owczarek <m.goleb@gmail.com> | 2022-01-24 18:56:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-24 18:56:49 +0100 |
commit | a338b407f2479f82df40635055effc163835183f (patch) | |
tree | be2a2f2b68d74a716388ea0027ff4083e000bfc9 /test | |
parent | 9c6f64c7b51d50e334ef1183e2937ad77c0a68b0 (diff) | |
download | jquery-a338b407f2479f82df40635055effc163835183f.tar.gz jquery-a338b407f2479f82df40635055effc163835183f.zip |
CSS: Skip falsy values in `addClass( array )`, compress code
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
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/attributes.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 98fae06c7..5f2418c7e 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -1644,6 +1644,44 @@ QUnit.test( "addClass, removeClass, hasClass on elements with classes with non-H testMatches(); } ); +( function() { + var rnothtmlwhite = /[^\x20\t\r\n\f]+/g; + + function expectClasses( assert, elem, classes ) { + var actualClassesSorted = ( elem.attr( "class" ).match( rnothtmlwhite ) || [] ) + .sort().join( " " ); + var classesSorted = classes.slice() + .sort().join( " " ); + assert.equal( actualClassesSorted, classesSorted, "Expected classes present" ); + } + + QUnit.test( "addClass on arrays with falsy elements (gh-4998)", function( assert ) { + assert.expect( 3 ); + + var elem = jQuery( "<div class='a'></div>" ); + + elem.addClass( [ "b", "", "c" ] ); + expectClasses( assert, elem, [ "a", "b", "c" ] ); + elem.addClass( [ "", "d" ] ); + expectClasses( assert, elem, [ "a", "b", "c", "d" ] ); + elem.addClass( [ "e", "" ] ); + expectClasses( assert, elem, [ "a", "b", "c", "d", "e" ] ); + } ); + + QUnit.test( "removeClass on arrays with falsy elements (gh-4998)", function( assert ) { + assert.expect( 3 ); + + var elem = jQuery( "<div class='a b c d e'></div>" ); + + elem.removeClass( [ "e", "" ] ); + expectClasses( assert, elem, [ "a", "b", "c", "d" ] ); + elem.removeClass( [ "", "d" ] ); + expectClasses( assert, elem, [ "a", "b", "c" ] ); + elem.removeClass( [ "b", "", "c" ] ); + expectClasses( assert, elem, [ "a" ] ); + } ); +} )(); + QUnit.test( "contents().hasClass() returns correct values", function( assert ) { assert.expect( 2 ); |