Skip to content

Commit 3e79599

Browse files
gmaclennangoto-bus-stop
authored andcommitted
Set indeterminate prop directly, fixes #128 (#129)
1 parent 3a4f62c commit 3e79599

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

lib/babel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const camelCase = require('camel-case')
44
const hyperx = require('hyperx')
55
const SVG_TAGS = require('./svg-tags')
66
const BOOL_PROPS = require('./bool-props')
7+
const DIRECT_PROPS = require('./direct-props')
78

89
const SVGNS = 'http://www.w3.org/2000/svg'
910
const XLINKNS = 'http://www.w3.org/1999/xlink'
@@ -226,7 +227,7 @@ module.exports = (babel) => {
226227
}
227228

228229
// abc.onclick = xyz
229-
if (attrName.slice(0, 2) === 'on') {
230+
if (attrName.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(attrName) > -1) {
230231
const value = convertPlaceholders(props[propName]).filter(isNotEmptyString)
231232
result.push(setDomProperty(id, attrName,
232233
value.length === 1

lib/browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var hyperx = require('hyperx')
22
var appendChild = require('./append-child')
33
var SVG_TAGS = require('./svg-tags')
44
var BOOL_PROPS = require('./bool-props')
5+
// Props that need to be set directly rather than with el.setAttribute()
6+
var DIRECT_PROPS = require('./direct-props')
57

68
var SVGNS = 'http://www.w3.org/2000/svg'
79
var XLINKNS = 'http://www.w3.org/1999/xlink'
@@ -52,7 +54,7 @@ function nanoHtmlCreateElement (tag, props, children) {
5254
else if (val === 'false') continue
5355
}
5456
// If a property prefers being set directly vs setAttribute
55-
if (key.slice(0, 2) === 'on') {
57+
if (key.slice(0, 2) === 'on' || DIRECT_PROPS.indexOf(key) !== -1) {
5658
el[p] = val
5759
} else {
5860
if (ns) {

lib/browserify-transform.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ var BOOL_PROPS = require('./bool-props').reduce(function (o, key) {
1515
o[key] = 1
1616
return o
1717
}, {})
18+
// Props that need to be set directly rather than with el.setAttribute()
19+
var DIRECT_PROPS = require('./direct-props').reduce(function (o, key) {
20+
o[key] = 1
21+
return o
22+
}, {})
1823

1924
module.exports = function yoYoify (file, opts) {
2025
if (/\.json$/.test(file)) return through()
@@ -140,6 +145,8 @@ function processNode (node, args) {
140145
if (val.slice(0, 9) === 'arguments') {
141146
if (namespace) {
142147
res.push('if (' + val + ' && ' + key + ') ' + to + '.setAttributeNS(null, ' + key + ', ' + key + ')')
148+
} else if (DIRECT_PROPS[key.slice(1, -1)]) {
149+
res.push('if (' + val + ' && ' + key + ') ' + to + '[' + key + '] = true')
143150
} else {
144151
res.push('if (' + val + ' && ' + key + ') ' + to + '.setAttribute(' + key + ', ' + key + ')')
145152
}
@@ -149,7 +156,8 @@ function processNode (node, args) {
149156
else if (val === 'false') return
150157
}
151158
}
152-
if (key.slice(1, 3) === 'on') {
159+
160+
if (key.slice(1, 3) === 'on' || DIRECT_PROPS[key.slice(1, -1)]) {
153161
res.push(to + '[' + key + '] = ' + val)
154162
} else {
155163
if (key === '"xlink:href"') {

lib/direct-props.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
'indeterminate'
3+
]

tests/browser/elements.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ var test = require('tape')
22
var html = require('../../')
33

44
test('create inputs', function (t) {
5-
t.plan(5)
5+
t.plan(7)
66

77
var expected = 'testing'
88
var result = html`<input type="text" value="${expected}" />`
99
t.equal(result.tagName, 'INPUT', 'created an input')
1010
t.equal(result.value, expected, 'set the value of an input')
1111

12-
result = html`<input type="checkbox" checked="${true}" disabled="${false}" />`
12+
result = html`<input type="checkbox" checked="${true}" disabled="${false}" indeterminate="${true}" />`
1313
t.equal(result.getAttribute('type'), 'checkbox', 'created a checkbox')
1414
t.equal(result.getAttribute('checked'), 'checked', 'set the checked attribute')
1515
t.equal(result.getAttribute('disabled'), null, 'should not have set the disabled attribute')
16+
t.equal(result.indeterminate, true, 'should have set indeterminate property')
17+
18+
result = html`<input indeterminate />`
19+
t.equal(result.indeterminate, true, 'should have set indeterminate property')
1620

1721
t.end()
1822
})

0 commit comments

Comments
 (0)