Skip to content

Commit

Permalink
fix data-* attributes not being set properly
Browse files Browse the repository at this point in the history
fixes #28
  • Loading branch information
tswaters committed Jul 9, 2016
1 parent b73b141 commit 2d8efe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ module.exports = function (h, opts) {
if (typeof parts[i][2] === 'object' && !key) {
for (copyKey in parts[i][2]) {
if (parts[i][2].hasOwnProperty(copyKey) && !cur[1][copyKey]) {
cur[1][copyKey] = parts[i][2][copyKey]
if (/data-/.test(copyKey)) data(cur[1], copyKey, parts[i][2][copyKey])
else cur[1][copyKey] = parts[i][2][copyKey]
}
}
} else {
Expand All @@ -71,11 +72,13 @@ module.exports = function (h, opts) {
var j = i
for (; i < parts.length; i++) {
if (parts[i][0] === ATTR_VALUE || parts[i][0] === ATTR_KEY) {
if (!cur[1][key]) cur[1][key] = strfn(parts[i][1])
if (/data-/.test(key)) data(cur[1], key, parts[i][1])
else if (!cur[1][key]) cur[1][key] = strfn(parts[i][1])
else cur[1][key] = concat(cur[1][key], parts[i][1])
} else if (parts[i][0] === VAR
&& (parts[i][1] === ATTR_VALUE || parts[i][1] === ATTR_KEY)) {
if (!cur[1][key]) cur[1][key] = strfn(parts[i][2])
if (/data-/.test(key)) data(cur[1], key, parts[i][2])
else if (!cur[1][key]) cur[1][key] = strfn(parts[i][2])
else cur[1][key] = concat(cur[1][key], parts[i][2])
} else {
if (key.length && !cur[1][key] && i === j
Expand Down Expand Up @@ -229,6 +232,11 @@ module.exports = function (h, opts) {
}
}

function data(cur, key, value) {
if (!cur.attributes) cur.attributes = {}
cur.attributes[key] = strfn(value)
}

function strfn (x) {
if (typeof x === 'function') return x
else if (typeof x === 'string') return x
Expand Down
4 changes: 2 additions & 2 deletions test/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ test('multiple keys', function (t) {
}
var key = 'data-'
var value = 'bar'
var tree = hx`<input ${props} ${key}foo=${value}>`
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" />')
var tree = hx`<input ${props} ${key}foo=${value} data-bar="baz">`
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" data-bar="baz" />')
t.end()
})

Expand Down

0 comments on commit 2d8efe1

Please sign in to comment.