diff --git a/src/attributes.js b/src/attributes.js index b7c1f4a825..2043eaba79 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -141,14 +141,26 @@ jQuery.fn.extend({ this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || ""; } }); - }, - + }, + hasClass: function( selector ) { - var className = " " + selector + " ", + return this.hasAttrToken( "class", selector ); + }, + + hasAttrToken: function( attr, selector, ignoreCase ) { + var token = " " + selector + " ", + attrVal, + hasToken, i = 0, l = this.length; + + if(ignoreCase === true) { + token = new RegExp(token, "i"); + } + for ( ; i < l; i++ ) { - if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) { + attrVal = attr == "class" ? this[i].className : jQuery(this[i]).attr(attr); + if(attrVal !== undefined && this[i].nodeType === 1 && (" " + attrVal + " ").replace(rclass, " ").match( token )) { return true; } } diff --git a/test/unit/attributes.js b/test/unit/attributes.js index a9d79462d4..f9a94252f9 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -1122,7 +1122,7 @@ test("toggleClass(Fucntion[, boolean]) with incoming value", function() { }); test("addClass, removeClass, hasClass", function() { - expect(17); + expect(18); var jq = jQuery("
Hi
"), x = jq[0]; @@ -1150,6 +1150,8 @@ test("addClass, removeClass, hasClass", function() { ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" ); ok( jq.hasClass("class4"), "Check hasClass with carriage return" ); ok( jq.is(".class4"), "Check is with carriage return" ); + + ok( !jq.hasClass("CLASS4"), "Check case sensitivity"); jq.removeClass("class2"); ok( jq.hasClass("class2")==false, "Check the class has been properly removed" ); @@ -1170,3 +1172,32 @@ test("contents().hasClass() returns correct values", function() { ok( $contents.hasClass("foo"), "Found 'foo' in $contents" ); ok( !$contents.hasClass("undefined"), "Did not find 'undefined' in $contents (correctly)" ); }); + +test("hasAttrToken", function() { + + expect(13); + + var $div = jQuery(""); + + ok( $div.hasAttrToken("accesskey", "A"), "Check token is found"); + ok( $div.hasAttrToken("accesskey", "B"), "Check token is found"); + ok( $div.hasAttrToken("accesskey", "C"), "Check token is found"); + ok( !$div.hasAttrToken("accesskey", "a"), "Check token is not found (wrong case)"); + ok( !$div.hasAttrToken("accesskey", "b"), "Check token is not found (wrong case)"); + ok( !$div.hasAttrToken("accesskey", "c"), "Check token is not found (wrong case)"); + + var $a = jQuery("Hi"); + $a.attr("rel", "author nofollow next"); + + var $a2 = jQuery("Bye"); + $a2.attr("rel", "author next"); + $a.add($a2); + + ok( $a.hasAttrToken("rel", "author", true), "Check rel is found"); + ok( $a.hasAttrToken("rel", "nofollow", true), "Check rel is found"); + ok( $a.hasAttrToken("rel", "next", true), "Check rel is found"); + ok( $a.hasAttrToken("rel", "AUTHOR", true), "Check rel is found (not case sensitive)"); + ok( $a.hasAttrToken("rel", "NOFOLLOW", true), "Check rel is found (not case sensitive)"); + ok( $a.hasAttrToken("rel", "NEXT", true), "Check rel is found (not case sensitive)"); + ok( !$a.hasAttrToken("rel", "nofollownext", true), "Check rel is not found"); +}); \ No newline at end of file