Permalink
Browse files

fix `hasClass` in collections of multiple elements

It should return true if *any* of the elements has the class, not just
the first one.

Fixes #631
  • Loading branch information...
1 parent a5fb36e commit fd9d3b953ed2f387c4b786dd68d24b4dd65f1aea @mislav mislav committed Nov 25, 2012
Showing with 15 additions and 9 deletions.
  1. +3 −2 src/zepto.js
  2. +12 −7 test/zepto.html
View
5 src/zepto.js
@@ -600,8 +600,9 @@ var Zepto = (function() {
return element ? this.indexOf($(element)[0]) : this.parent().children().indexOf(this[0])
},
hasClass: function(name){
- if (this.length < 1) return false
- else return classRE(name).test(className(this[0]))
+ return emptyArray.some.call(this, function(el){
+ return this.test(className(el))
+ }, classRE(name))
},
addClass: function(name){
return this.each(function(idx){
View
19 test/zepto.html
@@ -2003,15 +2003,20 @@
},
testHasClass: function(t){
- var el = $('#some_element').get(0)
- $('#some_element').addClass('green')
+ var el = $('#some_element')
+ el.addClass('green')
+
+ t.assert(el.hasClass('green'))
+ t.assert(!el.hasClass('orange'))
- t.assert($('#some_element').hasClass('green'))
- t.assert(!$('#some_element').hasClass('orange'))
+ el.addClass('orange')
+ t.assert(el.hasClass('green'))
+ t.assert(el.hasClass('orange'))
- $('#some_element').addClass('orange')
- t.assert($('#some_element').hasClass('green'))
- t.assert($('#some_element').hasClass('orange'))
+ el = $(document.body)
+ t.assert(!el.hasClass('orange'), "body shouldn't have the class")
+ el = el.add('#some_element')
+ t.assert(el.hasClass('orange'), "an element in collection has the class")
},
testHasClassEmpty: function(t){

0 comments on commit fd9d3b9

Please sign in to comment.