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

fix some attributes not being set properly under vdom #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var ATTR_EQ = 11, ATTR_BREAK = 12
module.exports = function (h, opts) {
h = attrToProp(h)
if (!opts) opts = {}

if (opts.vdom) {h = fixVdom(h)}

var concat = opts.concat || function (a, b) {
return String(a) + String(b)
}
Expand Down Expand Up @@ -261,3 +264,21 @@ var closeRE = RegExp('^(' + [
'vkern'
].join('|') + ')(?:[\.#][a-zA-Z0-9\u007F-\uFFFF_:-]+)*$')
function selfClosing (tag) { return closeRE.test(tag) }

function fixVdom (h) {
return function (tagName, props, children) {

if (!props.attributes) props.attributes = {}

Object.keys(props).forEach(function (key) {
var isDataAria = /^(data|aria)-/.test(key)
var isStyleString = key === 'style' && typeof props[key] === 'string'
if (isDataAria || isStyleString) {
props.attributes[key] = props[key]
delete props[key]
}
})

return h(tagName, props, children)
}
}
3 changes: 3 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ returned by the concatenation function and can make specific use of them. This
is useful if you want to implement a pre-processor to generate javascript from
hyperx syntax.

* `opts.vdom` - pass `true` when you are using virtual-dom. this fixes issues using
data-\*, aria-\* and passing style as a string. see [this issue](https://github.com/substack/hyperx/issues/28)

# prior art

* http://www.2ality.com/2014/07/jsx-template-strings.html?m=1
Expand Down
6 changes: 3 additions & 3 deletions test/key.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var test = require('tape')
var vdom = require('virtual-dom')
var hyperx = require('../')
var hx = hyperx(vdom.h)
var hx = hyperx(vdom.h, {vdom: true})

test('key', function (t) {
var key = 'type'
Expand Down 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
27 changes: 26 additions & 1 deletion test/vdom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var test = require('tape')
var vdom = require('virtual-dom')
var hyperx = require('../')
var hx = hyperx(vdom.h)
var hx = hyperx(vdom.h, {vdom: true})

var expected = `<div>
<h1 y="ab3cd">hello world!</h1>
Expand All @@ -24,3 +24,28 @@ test('vdom', function (t) {
t.equal(vdom.create(tree).toString(), expected)
t.end()
})

test('vdom - style as object', function (t) {
var styleObj = {backgroundColor: 'red'}
var tree = hx`<div class="box" style=${styleObj}></div>`
var expected = '<div style="background-color:red;" class="box"></div>'
var actual = vdom.create(tree).toString()
t.equal(actual, expected)
t.end()
})

test('vdom - style as string', function (t) {
var tree = hx`<div class="box" style="background-color:red;"></div>`
var expected = '<div style="background-color:red;" class="box"></div>'
var actual = vdom.create(tree).toString()
t.equal(actual, expected)
t.end()
})

test('vdom - accessibility attributes', function (t) {
var tree = hx`<div aria-hidden="${true}" aria-label="test-label" role="dialog" tabindex=4></div>`
var expected = '<div role="dialog" tabindex="4" aria-hidden="true" aria-label="test-label"></div>'
var actual = vdom.create(tree).toString()
t.equal(actual, expected)
t.end()
})