Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for boolean HTML-attributes #90

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dist/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,11 @@ module.exports = {
style : require('./style'),

attr: function (value) {
this.el.setAttribute(this.arg, value)
if (value || value === 0) {
this.el.setAttribute(this.arg, value)
} else {
this.el.removeAttribute(this.arg)
}
},

text: function (value) {
Expand Down
4 changes: 2 additions & 2 deletions dist/vue.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/directives/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ module.exports = {
style : require('./style'),

attr: function (value) {
this.el.setAttribute(this.arg, value)
if (value || value === 0) {
this.el.setAttribute(this.arg, value)
} else {
this.el.removeAttribute(this.arg)
}
},

text: function (value) {
Expand Down
41 changes: 35 additions & 6 deletions test/unit/specs/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,42 @@ describe('UNIT: Directives', function () {

describe('attr', function () {

var dir = mockDirective('attr')
dir.arg = 'href'
var dir = mockDirective('attr', 'input'),
el = dir.el

it('should set an attribute', function () {
var url = 'http://a.b.com'
dir.update(url)
assert.strictEqual(dir.el.getAttribute('href'), url)
it('should set a truthy attribute value', function () {
var value = 'Arrrrrr!'

dir.arg = 'value'
dir.update(value)
assert.strictEqual(el.getAttribute('value'), value)
})

it('should set attribute value to `0`', function () {
dir.arg = 'value'
dir.update(0)
assert.strictEqual(el.getAttribute('value'), '0')
})

it('should remove an attribute if value is `false`', function () {
dir.arg = 'disabled'
el.setAttribute('disabled', 'disabled')
dir.update(false)
assert.strictEqual(el.getAttribute('disabled'), null)
})

it('should remove an attribute if value is `null`', function () {
dir.arg = 'disabled'
el.setAttribute('disabled', 'disabled')
dir.update(null)
assert.strictEqual(el.getAttribute('disabled'), null)
})

it('should remove an attribute if value is `undefined`', function () {
dir.arg = 'disabled'
el.setAttribute('disabled', 'disabled')
dir.update(undefined)
assert.strictEqual(el.getAttribute('disabled'), null)
})

})
Expand Down