aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit
diff options
context:
space:
mode:
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2022-01-24 18:56:49 +0100
committerMichał Gołębiowski-Owczarek <m.goleb@gmail.com>2022-01-25 00:35:50 +0100
commit9b34bdb1c35c5c2bdb367dd80928825874d3ec3f (patch)
treee790760e2ede6510140f2d100b6ee7249179f650 /test/unit
parent95e34b69554cf9d3a52e4d932e581344990f60fa (diff)
downloadjquery-9b34bdb1c35c5c2bdb367dd80928825874d3ec3f.tar.gz
jquery-9b34bdb1c35c5c2bdb367dd80928825874d3ec3f.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 (partially cherry picked from commit a338b407f2479f82df40635055effc163835183f)
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/attributes.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/test/unit/attributes.js b/test/unit/attributes.js
index caec5c8c5..787dd3a02 100644
--- a/test/unit/attributes.js
+++ b/test/unit/attributes.js
@@ -1631,6 +1631,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 );