Commit 96501d38 authored by timmywil's avatar timmywil

Allow similarly named classes (regression from 9499) and switch class...

Allow similarly named classes (regression from 9499) and switch class retrieval to property when passing class to value functions. Fixes #9617.
parent 124817e6
...@@ -37,12 +37,12 @@ jQuery.fn.extend({ ...@@ -37,12 +37,12 @@ jQuery.fn.extend({
}, },
addClass: function( value ) { addClass: function( value ) {
var classNames, i, l, elem, setClass, c, cl; var classNames, i, l, elem,
setClass, c, cl;
if ( jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
return this.each(function( j ) { return this.each(function( j ) {
var self = jQuery( this ); jQuery( this ).addClass( value.call(this, j, this.className) );
self.addClass( value.call(this, j, self.attr("class") || "") );
}); });
} }
...@@ -57,11 +57,11 @@ jQuery.fn.extend({ ...@@ -57,11 +57,11 @@ jQuery.fn.extend({
elem.className = value; elem.className = value;
} else { } else {
setClass = elem.className; setClass = " " + elem.className + " ";
for ( c = 0, cl = classNames.length; c < cl; c++ ) { for ( c = 0, cl = classNames.length; c < cl; c++ ) {
if ( !~setClass.indexOf(classNames[ c ]) ) { if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {
setClass += " " + classNames[ c ]; setClass += classNames[ c ] + " ";
} }
} }
elem.className = jQuery.trim( setClass ); elem.className = jQuery.trim( setClass );
...@@ -78,8 +78,7 @@ jQuery.fn.extend({ ...@@ -78,8 +78,7 @@ jQuery.fn.extend({
if ( jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
return this.each(function( j ) { return this.each(function( j ) {
var self = jQuery( this ); jQuery( this ).removeClass( value.call(this, j, this.className) );
self.removeClass( value.call(this, j, self.attr("class")) );
}); });
} }
...@@ -112,9 +111,8 @@ jQuery.fn.extend({ ...@@ -112,9 +111,8 @@ jQuery.fn.extend({
isBool = typeof stateVal === "boolean"; isBool = typeof stateVal === "boolean";
if ( jQuery.isFunction( value ) ) { if ( jQuery.isFunction( value ) ) {
return this.each(function(i) { return this.each(function( i ) {
var self = jQuery(this); jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal );
self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
}); });
} }
......
...@@ -762,7 +762,8 @@ test("val(select) after form.reset() (Bug #2551)", function() { ...@@ -762,7 +762,8 @@ test("val(select) after form.reset() (Bug #2551)", function() {
}); });
var testAddClass = function(valueObj) { var testAddClass = function(valueObj) {
expect(7); expect(9);
var div = jQuery("div"); var div = jQuery("div");
div.addClass( valueObj("test") ); div.addClass( valueObj("test") );
var pass = true; var pass = true;
...@@ -791,10 +792,16 @@ var testAddClass = function(valueObj) { ...@@ -791,10 +792,16 @@ var testAddClass = function(valueObj) {
div.addClass( valueObj("bar baz") ); div.addClass( valueObj("bar baz") );
equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." ); equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
div.removeAttr("class"); div.removeClass();
div.addClass( valueObj("foo") ).addClass( valueObj("foo") ) div.addClass( valueObj("foo") ).addClass( valueObj("foo") )
equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." ); equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
div.removeAttr("class");
div.addClass( valueObj("fo") );
equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
div.removeClass().addClass("wrap2");
ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
div.removeClass();
div.addClass( valueObj("bar bar") ); div.addClass( valueObj("bar bar") );
equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." ); equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
}; };
...@@ -959,7 +966,7 @@ test("toggleClass(Function[, boolean])", function() { ...@@ -959,7 +966,7 @@ test("toggleClass(Function[, boolean])", function() {
test("toggleClass(Fucntion[, boolean]) with incoming value", function() { test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
expect(14); expect(14);
var e = jQuery("#firstp"), old = e.attr("class"); var e = jQuery("#firstp"), old = e.attr("class") || "";
ok( !e.is(".test"), "Assert class not present" ); ok( !e.is(".test"), "Assert class not present" );
e.toggleClass(function(i, val) { e.toggleClass(function(i, val) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment