Skip to content

Commit

Permalink
support manipulating classnames on SVG elements
Browse files Browse the repository at this point in the history
Fixes #468
  • Loading branch information
mislav committed Sep 28, 2012
1 parent 74bd6f5 commit 122b014
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/zepto.js
Expand Up @@ -225,6 +225,15 @@ var Zepto = (function() {
value == null ? node.removeAttribute(name) : node.setAttribute(name, value) value == null ? node.removeAttribute(name) : node.setAttribute(name, value)
} }


// access className property while respecting SVGAnimatedString
function className(node, value){
var klass = node.className,
svg = klass && klass.baseVal !== undefined

if (value === undefined) return svg ? klass.baseVal : klass
svg ? (klass.baseVal = value) : (node.className = value)
}

$.isFunction = isFunction $.isFunction = isFunction
$.isObject = isObject $.isObject = isObject
$.isArray = isArray $.isArray = isArray
Expand Down Expand Up @@ -540,32 +549,31 @@ var Zepto = (function() {
}, },
hasClass: function(name){ hasClass: function(name){
if (this.length < 1) return false if (this.length < 1) return false
else return classRE(name).test(this[0].className) else return classRE(name).test(className(this[0]))
}, },
addClass: function(name){ addClass: function(name){
return this.each(function(idx){ return this.each(function(idx){
classList = [] classList = []
var cls = this.className, newName = funcArg(this, name, idx, cls) var cls = className(this), newName = funcArg(this, name, idx, cls)
newName.split(/\s+/g).forEach(function(klass){ newName.split(/\s+/g).forEach(function(klass){
if (!$(this).hasClass(klass)) classList.push(klass) if (!$(this).hasClass(klass)) classList.push(klass)
}, this) }, this)
classList.length && (this.className += (cls ? " " : "") + classList.join(" ")) classList.length && className(this, cls + (cls ? " " : "") + classList.join(" "))
}) })
}, },
removeClass: function(name){ removeClass: function(name){
return this.each(function(idx){ return this.each(function(idx){
if (name === undefined) if (name === undefined) return className(this, '')
return this.className = '' classList = className(this)
classList = this.className
funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){ funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){
classList = classList.replace(classRE(klass), " ") classList = classList.replace(classRE(klass), " ")
}) })
this.className = classList.trim() className(this, classList.trim())
}) })
}, },
toggleClass: function(name, when){ toggleClass: function(name, when){
return this.each(function(idx){ return this.each(function(idx){
var newName = funcArg(this, name, idx, this.className) var newName = funcArg(this, name, idx, className(this))
;(when === undefined ? !$(this).hasClass(newName) : when) ? ;(when === undefined ? !$(this).hasClass(newName) : when) ?
$(this).addClass(newName) : $(this).removeClass(newName) $(this).addClass(newName) : $(this).removeClass(newName)
}) })
Expand Down
16 changes: 16 additions & 0 deletions test/zepto.html
Expand Up @@ -224,6 +224,8 @@ <h1>Zepto DOM unit tests</h1>
</form> </form>
</div> </div>


<svg></svg>

</div><!-- fixtures --> </div><!-- fixtures -->


<script> <script>
Expand Down Expand Up @@ -1949,6 +1951,20 @@ <h1>Zepto DOM unit tests</h1>
t.assert($('#toggle_element').hasClass('yellow')) t.assert($('#toggle_element').hasClass('yellow'))
}, },


testClassSVG: function(t){
var svg = $('svg')
t.assert(!svg.hasClass('foo'))
svg.addClass('foo bar')
t.assert(svg.hasClass('foo'))
t.assert(svg.hasClass('bar'))
svg.removeClass('foo')
t.assert(!svg.hasClass('foo'))
t.assert(svg.hasClass('bar'))
svg.toggleClass('bar')
t.assert(!svg.hasClass('foo'))
t.assert(!svg.hasClass('bar'))
},

testIndex: function(t){ testIndex: function(t){
t.assertEqual($("p > span").index("#nay"), 2) t.assertEqual($("p > span").index("#nay"), 2)
t.assertEqual($("p > span").index(".yay"), 0) t.assertEqual($("p > span").index(".yay"), 0)
Expand Down

0 comments on commit 122b014

Please sign in to comment.