Skip to content

Commit 4dfdbe4

Browse files
committed
Support for boolean HTML-attributes
1 parent c71c6a4 commit 4dfdbe4

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/directives/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ module.exports = {
1212
style : require('./style'),
1313

1414
attr: function (value) {
15-
this.el.setAttribute(this.arg, value)
15+
if (value || value === 0) {
16+
this.el.setAttribute(this.arg, value)
17+
} else {
18+
this.el.removeAttribute(this.arg)
19+
}
1620
},
1721

1822
text: function (value) {

test/unit/specs/directives.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,42 @@ describe('UNIT: Directives', function () {
22

33
describe('attr', function () {
44

5-
var dir = mockDirective('attr')
6-
dir.arg = 'href'
5+
var dir = mockDirective('attr', 'input'),
6+
el = dir.el
77

8-
it('should set an attribute', function () {
9-
var url = 'http://a.b.com'
10-
dir.update(url)
11-
assert.strictEqual(dir.el.getAttribute('href'), url)
8+
it('should set a truthy attribute value', function () {
9+
var value = 'Arrrrrr!'
10+
11+
dir.arg = 'value'
12+
dir.update(value)
13+
assert.strictEqual(el.getAttribute('value'), value)
14+
})
15+
16+
it('should set attribute value to `0`', function () {
17+
dir.arg = 'value'
18+
dir.update(0)
19+
assert.strictEqual(el.getAttribute('value'), '0')
20+
})
21+
22+
it('should remove an attribute if value is `false`', function () {
23+
dir.arg = 'disabled'
24+
el.setAttribute('disabled', 'disabled')
25+
dir.update(false)
26+
assert.strictEqual(el.getAttribute('disabled'), null)
27+
})
28+
29+
it('should remove an attribute if value is `null`', function () {
30+
dir.arg = 'disabled'
31+
el.setAttribute('disabled', 'disabled')
32+
dir.update(null)
33+
assert.strictEqual(el.getAttribute('disabled'), null)
34+
})
35+
36+
it('should remove an attribute if value is `undefined`', function () {
37+
dir.arg = 'disabled'
38+
el.setAttribute('disabled', 'disabled')
39+
dir.update(undefined)
40+
assert.strictEqual(el.getAttribute('disabled'), null)
1241
})
1342

1443
})

0 commit comments

Comments
 (0)