Skip to content

Commit

Permalink
Stop falling back to reading properties in fn.attr()
Browse files Browse the repository at this point in the history
That's what `prop()` is for. This breaking change is for jQuery 1.6
compatibility. Also, missing properties now return `undefined` instead
of `null`.

Fixes #1080
  • Loading branch information
mislav committed Jul 13, 2016
1 parent 26c59c3 commit 85d475a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,7 @@ var Zepto = (function() {
attr: function(name, value){
var result
return (typeof name == 'string' && !(1 in arguments)) ?
(!this.length || this[0].nodeType !== 1 ? undefined :
(!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result
) :
(0 in this && this[0].nodeType == 1 && (result = this[0].getAttribute(name)) != null ? result : undefined) :
this.each(function(idx){
if (this.nodeType !== 1) return
if (isObject(name)) for (key in name) setAttribute(this, key, name[key])
Expand Down
23 changes: 12 additions & 11 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ <h1>Zepto Core unit tests</h1>
t.assertEqual('someOtherName', els.attr("data-name"))
t.assertEqual('someOtherId', $('#attr_2').attr('data-id'))

t.assertNull(els.attr("nonExistentAttribute"))
t.assertUndefined(els.attr("nonExistentAttribute"))

els.attr("data-id", false)
t.assertEqual("false", els.attr("data-id"))
Expand All @@ -1822,7 +1822,7 @@ <h1>Zepto Core unit tests</h1>
var el = $('<div data-name="foo">')
t.assertIdentical(el, el.attr('data-name', undefined), 'setter should return self')
t.assertNull(el.get(0).getAttribute('data-name'), 'attribute should be erased')
t.assertNull(el.attr('data-name'), 'attr should reflect erased attribute')
t.assertUndefined(el.attr('data-name'), 'attr should reflect erased attribute')
},

testProp: function(t){
Expand Down Expand Up @@ -1909,18 +1909,18 @@ <h1>Zepto Core unit tests</h1>
testAttrNullUnset: function(t){
var el = $('<div id=hi>')
el.attr('id', null)
t.assertIdentical('', el.attr('id'))
t.assertUndefined(el.attr('id'))

el.attr('id', 'hello')
el.attr({ id:null })
t.assertIdentical('', el.attr('id'))
t.assertUndefined(el.attr('id'))
},

testRemoveAttr: function(t) {
var el = $('#attr_remove')
t.assertEqual('boom', el.attr('data-name'))
el.removeAttr('data-name')
t.assertNull(el.attr('data-name'))
t.assertUndefined(el.attr('data-name'))
},

testRemoveMultipleAttr: function(t) {
Expand All @@ -1929,8 +1929,8 @@ <h1>Zepto Core unit tests</h1>
t.assertEqual('someName1', el.attr('data-name'))

el.removeAttr('data-id data-name')
t.assertNull(el.attr('data-id'))
t.assertNull(el.attr('data-name'))
t.assertUndefined(el.attr('data-id'))
t.assertUndefined(el.attr('data-name'))
},

testRemoveAttrNoElement: function(t){
Expand All @@ -1939,9 +1939,9 @@ <h1>Zepto Core unit tests</h1>

var els = $('<b rel=up></b> <i rel=next></i>')
t.assertIdentical(els, els.removeAttr('rel'))
t.assertNull(els.eq(0).attr('rel'))
t.assertUndefined(els.eq(0).attr('rel'))
t.assertUndefined(els.eq(1).attr('rel'))
t.assertNull(els.eq(2).attr('rel'))
t.assertUndefined(els.eq(2).attr('rel'))
},

testData: function(t) {
Expand Down Expand Up @@ -2615,8 +2615,9 @@ <h1>Zepto Core unit tests</h1>
},

testBoolAttr: function (t) {
t.assertEqual($('#BooleanInput').attr('required'), true)
t.assertEqual($('#BooleanInput').attr('non_existant_attr'), undefined)
var el = $('#BooleanInput')
t.assertEqual('', el.attr('required'))
t.assertUndefined(el.attr('non_existant_attr'))
},

testDocumentReady: function (t) {
Expand Down

0 comments on commit 85d475a

Please sign in to comment.