Skip to content

Commit 96501d3

Browse files
author
timmywil
committed
Allow similarly named classes (regression from 9499) and switch class retrieval to property when passing class to value functions. Fixes #9617.
1 parent 124817e commit 96501d3

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/attributes.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ jQuery.fn.extend({
3737
},
3838

3939
addClass: function( value ) {
40-
var classNames, i, l, elem, setClass, c, cl;
40+
var classNames, i, l, elem,
41+
setClass, c, cl;
4142

4243
if ( jQuery.isFunction( value ) ) {
4344
return this.each(function( j ) {
44-
var self = jQuery( this );
45-
self.addClass( value.call(this, j, self.attr("class") || "") );
45+
jQuery( this ).addClass( value.call(this, j, this.className) );
4646
});
4747
}
4848

@@ -57,11 +57,11 @@ jQuery.fn.extend({
5757
elem.className = value;
5858

5959
} else {
60-
setClass = elem.className;
60+
setClass = " " + elem.className + " ";
6161

6262
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
63-
if ( !~setClass.indexOf(classNames[ c ]) ) {
64-
setClass += " " + classNames[ c ];
63+
if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {
64+
setClass += classNames[ c ] + " ";
6565
}
6666
}
6767
elem.className = jQuery.trim( setClass );
@@ -78,8 +78,7 @@ jQuery.fn.extend({
7878

7979
if ( jQuery.isFunction( value ) ) {
8080
return this.each(function( j ) {
81-
var self = jQuery( this );
82-
self.removeClass( value.call(this, j, self.attr("class")) );
81+
jQuery( this ).removeClass( value.call(this, j, this.className) );
8382
});
8483
}
8584

@@ -112,9 +111,8 @@ jQuery.fn.extend({
112111
isBool = typeof stateVal === "boolean";
113112

114113
if ( jQuery.isFunction( value ) ) {
115-
return this.each(function(i) {
116-
var self = jQuery(this);
117-
self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
114+
return this.each(function( i ) {
115+
jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal );
118116
});
119117
}
120118

test/unit/attributes.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,8 @@ test("val(select) after form.reset() (Bug #2551)", function() {
762762
});
763763

764764
var testAddClass = function(valueObj) {
765-
expect(7);
765+
expect(9);
766+
766767
var div = jQuery("div");
767768
div.addClass( valueObj("test") );
768769
var pass = true;
@@ -791,10 +792,16 @@ var testAddClass = function(valueObj) {
791792
div.addClass( valueObj("bar baz") );
792793
equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
793794

794-
div.removeAttr("class");
795+
div.removeClass();
795796
div.addClass( valueObj("foo") ).addClass( valueObj("foo") )
796797
equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
797-
div.removeAttr("class");
798+
799+
div.addClass( valueObj("fo") );
800+
equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
801+
div.removeClass().addClass("wrap2");
802+
ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
803+
804+
div.removeClass();
798805
div.addClass( valueObj("bar bar") );
799806
equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
800807
};
@@ -959,7 +966,7 @@ test("toggleClass(Function[, boolean])", function() {
959966
test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
960967
expect(14);
961968

962-
var e = jQuery("#firstp"), old = e.attr("class");
969+
var e = jQuery("#firstp"), old = e.attr("class") || "";
963970
ok( !e.is(".test"), "Assert class not present" );
964971

965972
e.toggleClass(function(i, val) {

0 commit comments

Comments
 (0)