From 76d6d445bb68009c010e0b91dbf21747bc2d35ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rn=20Zaefferer?= Date: Mon, 8 Jan 2007 16:12:20 +0000 Subject: [PATCH] Added test for toggleClass, fixed jQuery.className.has to accept both node and className (#762) --- src/jquery/coreTest.js | 9 +++++++++ src/jquery/jquery.js | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js index a18973903..7be53f738 100644 --- a/src/jquery/coreTest.js +++ b/src/jquery/coreTest.js @@ -429,6 +429,15 @@ test("removeClass(String) - add three classes and remove again", function() { 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() { ok( $('#mark').removeAttr("class")[0].className == "", "remove class" ); }); diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index c8dcdd217..4fb7f8e75 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -1227,20 +1227,24 @@ jQuery.extend({ }, className: { + // internal only, use addClass("class") add: function( elem, c ){ jQuery.each( c.split(/\s+/), function(i, cur){ if ( !jQuery.className.has( elem.className, cur ) ) elem.className += ( elem.className ? " " : "" ) + cur; }); }, + // internal only, use removeClass("class") remove: function( elem, c ){ elem.className = c ? jQuery.grep( elem.className.split(/\s+/), function(cur){ return !jQuery.className.has( c, cur ); }).join(' ') : ""; }, - has: function( classes, c ){ - return classes && new RegExp("(^|\\s)" + c + "(\\s|$)").test( classes ); + // internal only, use is(".class") + has: function( t, c ) { + t = t.className || t; + return t && new RegExp("(^|\\s)" + c + "(\\s|$)").test( t ); } }, -- 2.39.5