Skip to content

Commit

Permalink
fixes to vdom rendering
Browse files Browse the repository at this point in the history
- enabled by passing {vdom: true} flag to the hyperx contructor options

- fixes data-*, aria-* and style as a string.  vdom expects these to be
  under an 'attributes' property under props and will gladly ignore them
  otherwise

fixes #28
  • Loading branch information
tswaters committed Sep 3, 2016
1 parent b73b141 commit 39a1feb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
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()
})

0 comments on commit 39a1feb

Please sign in to comment.