Added test for toggleClass, fixed jQuery.className.has to accept both node and className (#762)
This commit is contained in:
parent
d97397d38e
commit
76d6d445bb
9
src/jquery/coreTest.js
vendored
9
src/jquery/coreTest.js
vendored
|
@ -429,6 +429,15 @@ test("removeClass(String) - add three classes and remove again", function() {
|
||||||
ok( pass, "Remove multiple classes" );
|
ok( pass, "Remove multiple classes" );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("toggleClass(String)", function() {
|
||||||
|
var e = $("#firstp");
|
||||||
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
|
e.toggleClass("test");
|
||||||
|
ok( e.is(".test"), "Assert class present" );
|
||||||
|
e.toggleClass("test");
|
||||||
|
ok( !e.is(".test"), "Assert class not present" );
|
||||||
|
});
|
||||||
|
|
||||||
test("removeAttr(String", function() {
|
test("removeAttr(String", function() {
|
||||||
ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
|
ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
|
||||||
});
|
});
|
||||||
|
|
8
src/jquery/jquery.js
vendored
8
src/jquery/jquery.js
vendored
|
@ -1227,20 +1227,24 @@ jQuery.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
className: {
|
className: {
|
||||||
|
// internal only, use addClass("class")
|
||||||
add: function( elem, c ){
|
add: function( elem, c ){
|
||||||
jQuery.each( c.split(/\s+/), function(i, cur){
|
jQuery.each( c.split(/\s+/), function(i, cur){
|
||||||
if ( !jQuery.className.has( elem.className, cur ) )
|
if ( !jQuery.className.has( elem.className, cur ) )
|
||||||
elem.className += ( elem.className ? " " : "" ) + cur;
|
elem.className += ( elem.className ? " " : "" ) + cur;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
// internal only, use removeClass("class")
|
||||||
remove: function( elem, c ){
|
remove: function( elem, c ){
|
||||||
elem.className = c ?
|
elem.className = c ?
|
||||||
jQuery.grep( elem.className.split(/\s+/), function(cur){
|
jQuery.grep( elem.className.split(/\s+/), function(cur){
|
||||||
return !jQuery.className.has( c, cur );
|
return !jQuery.className.has( c, cur );
|
||||||
}).join(' ') : "";
|
}).join(' ') : "";
|
||||||
},
|
},
|
||||||
has: function( classes, c ){
|
// internal only, use is(".class")
|
||||||
return classes && new RegExp("(^|\\s)" + c + "(\\s|$)").test( classes );
|
has: function( t, c ) {
|
||||||
|
t = t.className || t;
|
||||||
|
return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t );
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue