Check classes passed for duplicates. Fixes #9499.
This commit is contained in:
parent
0a80be67f4
commit
db437be6e3
|
@ -37,29 +37,30 @@ jQuery.fn.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
addClass: function( value ) {
|
addClass: function( value ) {
|
||||||
|
var classNames, i, l, elem, setClass, c, cl;
|
||||||
|
|
||||||
if ( jQuery.isFunction( value ) ) {
|
if ( jQuery.isFunction( value ) ) {
|
||||||
return this.each(function(i) {
|
return this.each(function( j ) {
|
||||||
var self = jQuery( this );
|
var self = jQuery( this );
|
||||||
self.addClass( value.call(this, i, self.attr("class") || "") );
|
self.addClass( value.call(this, j, self.attr("class") || "") );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( value && typeof value === "string" ) {
|
if ( value && typeof value === "string" ) {
|
||||||
var classNames = (value || "").split( rspace );
|
classNames = value.split( rspace );
|
||||||
|
|
||||||
for ( var i = 0, l = this.length; i < l; i++ ) {
|
for ( i = 0, l = this.length; i < l; i++ ) {
|
||||||
var elem = this[i];
|
elem = this[ i ];
|
||||||
|
|
||||||
if ( elem.nodeType === 1 ) {
|
if ( elem.nodeType === 1 ) {
|
||||||
if ( !elem.className ) {
|
if ( !elem.className && classNames.length === 1 ) {
|
||||||
elem.className = value;
|
elem.className = value;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var className = " " + elem.className + " ",
|
|
||||||
setClass = elem.className;
|
setClass = elem.className;
|
||||||
|
|
||||||
for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
|
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
|
||||||
if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
|
if ( !~setClass.indexOf(classNames[ c ]) ) {
|
||||||
setClass += " " + classNames[ c ];
|
setClass += " " + classNames[ c ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,23 +74,25 @@ jQuery.fn.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
removeClass: function( value ) {
|
removeClass: function( value ) {
|
||||||
|
var classNames, i, l, elem, className, c, cl;
|
||||||
|
|
||||||
if ( jQuery.isFunction( value ) ) {
|
if ( jQuery.isFunction( value ) ) {
|
||||||
return this.each(function(i) {
|
return this.each(function( j ) {
|
||||||
var self = jQuery( this );
|
var self = jQuery( this );
|
||||||
self.removeClass( value.call(this, i, self.attr("class")) );
|
self.removeClass( value.call(this, j, self.attr("class")) );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( (value && typeof value === "string") || value === undefined ) {
|
if ( (value && typeof value === "string") || value === undefined ) {
|
||||||
var classNames = (value || "").split( rspace );
|
classNames = (value || "").split( rspace );
|
||||||
|
|
||||||
for ( var i = 0, l = this.length; i < l; i++ ) {
|
for ( i = 0, l = this.length; i < l; i++ ) {
|
||||||
var elem = this[i];
|
elem = this[ i ];
|
||||||
|
|
||||||
if ( elem.nodeType === 1 && elem.className ) {
|
if ( elem.nodeType === 1 && elem.className ) {
|
||||||
if ( value ) {
|
if ( value ) {
|
||||||
var className = (" " + elem.className + " ").replace(rclass, " ");
|
className = (" " + elem.className + " ").replace( rclass, " " );
|
||||||
for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
|
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
|
||||||
className = className.replace(" " + classNames[ c ] + " ", " ");
|
className = className.replace(" " + classNames[ c ] + " ", " ");
|
||||||
}
|
}
|
||||||
elem.className = jQuery.trim( className );
|
elem.className = jQuery.trim( className );
|
||||||
|
|
|
@ -119,5 +119,5 @@ sup { display: none; }
|
||||||
dfn { display: none; }
|
dfn { display: none; }
|
||||||
|
|
||||||
/* #9239 Attach a background to the body( avoid crashes in removing the test element in support ) */
|
/* #9239 Attach a background to the body( avoid crashes in removing the test element in support ) */
|
||||||
body, div { background: url(http://www.ctemploymentlawblog.com/test.jpg) no-repeat -1000px 0; }
|
body, div { background: url(http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif) no-repeat -1000px 0; }
|
||||||
|
|
||||||
|
|
|
@ -761,12 +761,14 @@ test("val(select) after form.reset() (Bug #2551)", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
var testAddClass = function(valueObj) {
|
var testAddClass = function(valueObj) {
|
||||||
expect(5);
|
expect(7);
|
||||||
var div = jQuery("div");
|
var div = jQuery("div");
|
||||||
div.addClass( valueObj("test") );
|
div.addClass( valueObj("test") );
|
||||||
var pass = true;
|
var pass = true;
|
||||||
for ( var i = 0; i < div.size(); i++ ) {
|
for ( var i = 0; i < div.size(); i++ ) {
|
||||||
if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
|
if ( !~div.get(i).className.indexOf("test") ) {
|
||||||
|
pass = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ok( pass, "Add Class" );
|
ok( pass, "Add Class" );
|
||||||
|
|
||||||
|
@ -787,6 +789,13 @@ var testAddClass = function(valueObj) {
|
||||||
div.attr("class", "foo");
|
div.attr("class", "foo");
|
||||||
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.addClass( valueObj("foo") ).addClass( valueObj("foo") )
|
||||||
|
equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
|
||||||
|
div.removeAttr("class");
|
||||||
|
div.addClass( valueObj("bar bar") );
|
||||||
|
equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
|
||||||
};
|
};
|
||||||
|
|
||||||
test("addClass(String)", function() {
|
test("addClass(String)", function() {
|
||||||
|
|
Loading…
Reference in a new issue