Skip to content

Commit 2d8efe1

Browse files
committed
fix data-* attributes not being set properly
fixes #28
1 parent b73b141 commit 2d8efe1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ module.exports = function (h, opts) {
5959
if (typeof parts[i][2] === 'object' && !key) {
6060
for (copyKey in parts[i][2]) {
6161
if (parts[i][2].hasOwnProperty(copyKey) && !cur[1][copyKey]) {
62-
cur[1][copyKey] = parts[i][2][copyKey]
62+
if (/data-/.test(copyKey)) data(cur[1], copyKey, parts[i][2][copyKey])
63+
else cur[1][copyKey] = parts[i][2][copyKey]
6364
}
6465
}
6566
} else {
@@ -71,11 +72,13 @@ module.exports = function (h, opts) {
7172
var j = i
7273
for (; i < parts.length; i++) {
7374
if (parts[i][0] === ATTR_VALUE || parts[i][0] === ATTR_KEY) {
74-
if (!cur[1][key]) cur[1][key] = strfn(parts[i][1])
75+
if (/data-/.test(key)) data(cur[1], key, parts[i][1])
76+
else if (!cur[1][key]) cur[1][key] = strfn(parts[i][1])
7577
else cur[1][key] = concat(cur[1][key], parts[i][1])
7678
} else if (parts[i][0] === VAR
7779
&& (parts[i][1] === ATTR_VALUE || parts[i][1] === ATTR_KEY)) {
78-
if (!cur[1][key]) cur[1][key] = strfn(parts[i][2])
80+
if (/data-/.test(key)) data(cur[1], key, parts[i][2])
81+
else if (!cur[1][key]) cur[1][key] = strfn(parts[i][2])
7982
else cur[1][key] = concat(cur[1][key], parts[i][2])
8083
} else {
8184
if (key.length && !cur[1][key] && i === j
@@ -229,6 +232,11 @@ module.exports = function (h, opts) {
229232
}
230233
}
231234

235+
function data(cur, key, value) {
236+
if (!cur.attributes) cur.attributes = {}
237+
cur.attributes[key] = strfn(value)
238+
}
239+
232240
function strfn (x) {
233241
if (typeof x === 'function') return x
234242
else if (typeof x === 'string') return x

test/key.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ test('multiple keys', function (t) {
5050
}
5151
var key = 'data-'
5252
var value = 'bar'
53-
var tree = hx`<input ${props} ${key}foo=${value}>`
54-
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" />')
53+
var tree = hx`<input ${props} ${key}foo=${value} data-bar="baz">`
54+
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" data-bar="baz" />')
5555
t.end()
5656
})
5757

0 commit comments

Comments
 (0)