Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

… retrieval to property when passing class to value functions. Fixes #9617.
  • Loading branch information...
commit 96501d38a935187461d45c40f17f8327b2e7cd91 1 parent 124817e
authored June 19, 2011
20  src/attributes.js
@@ -37,12 +37,12 @@ jQuery.fn.extend({
37 37
 	},
38 38
 
39 39
 	addClass: function( value ) {
40  
-		var classNames, i, l, elem, setClass, c, cl;
  40
+		var classNames, i, l, elem,
  41
+			setClass, c, cl;
41 42
 
42 43
 		if ( jQuery.isFunction( value ) ) {
43 44
 			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) );
46 46
 			});
47 47
 		}
48 48
 
@@ -57,11 +57,11 @@ jQuery.fn.extend({
57 57
 						elem.className = value;
58 58
 
59 59
 					} else {
60  
-						setClass = elem.className;
  60
+						setClass = " " + elem.className + " ";
61 61
 
62 62
 						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 ] + " ";
65 65
 							}
66 66
 						}
67 67
 						elem.className = jQuery.trim( setClass );
@@ -78,8 +78,7 @@ jQuery.fn.extend({
78 78
 
79 79
 		if ( jQuery.isFunction( value ) ) {
80 80
 			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) );
83 82
 			});
84 83
 		}
85 84
 
@@ -112,9 +111,8 @@ jQuery.fn.extend({
112 111
 			isBool = typeof stateVal === "boolean";
113 112
 
114 113
 		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 );
118 116
 			});
119 117
 		}
120 118
 
15  test/unit/attributes.js
@@ -762,7 +762,8 @@ test("val(select) after form.reset() (Bug #2551)", function() {
762 762
 }); 
763 763
 
764 764
 var testAddClass = function(valueObj) {
765  
-	expect(7);
  765
+	expect(9);
  766
+
766 767
 	var div = jQuery("div");
767 768
 	div.addClass( valueObj("test") );
768 769
 	var pass = true;
@@ -791,10 +792,16 @@ var testAddClass = function(valueObj) {
791 792
 	div.addClass( valueObj("bar baz") );
792 793
 	equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
793 794
 	
794  
-	div.removeAttr("class");
  795
+	div.removeClass();
795 796
 	div.addClass( valueObj("foo") ).addClass( valueObj("foo") )
796 797
 	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();
798 805
 	div.addClass( valueObj("bar bar") );
799 806
 	equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
800 807
 };
@@ -959,7 +966,7 @@ test("toggleClass(Function[, boolean])", function() {
959 966
 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
960 967
 	expect(14);
961 968
 
962  
-	var e = jQuery("#firstp"), old = e.attr("class");
  969
+	var e = jQuery("#firstp"), old = e.attr("class") || "";
963 970
 	ok( !e.is(".test"), "Assert class not present" );
964 971
 
965 972
 	e.toggleClass(function(i, val) {

2 notes on commit 96501d3

Timmy Willison
Collaborator

As the bugs site is currently down, I'm grouping the latter part with the first bug. For more information on what I'm fixing there, here's a test case:

Compare jQuery version 1.5.2 and 1.6. - http://jsfiddle.net/timmywil/HqCLn/2/
You'll see "" and "", then undefined and "".

When no class is present, empty string should be passed to value functions. This wasn't caught in the tests in 1.6 because it was just comparing what was returning by attr with what was returned by attr. Not a big one, but will now be consistent and slightly faster as there is no longer a call to attr.

I'll add this info to the bug ticket when the site is back.

Timmy Willison
Collaborator

Here's the second issue that was fixed in this commit: #9624

Timo Tijhof

What is .removeClass() supposed to do here?

edit: Ah, value === undefined -> className = "", I didn't know that. has been supported ever since 96dd06e. Nice :)

Please sign in to comment.
Something went wrong with that request. Please try again.