diff --git a/index.js b/index.js index f254f12..916e6d9 100644 --- a/index.js +++ b/index.js @@ -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) } @@ -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) + } +} diff --git a/readme.markdown b/readme.markdown index b37ed83..4bc9a2d 100644 --- a/readme.markdown +++ b/readme.markdown @@ -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 diff --git a/test/key.js b/test/key.js index 774c269..85f8b3a 100644 --- a/test/key.js +++ b/test/key.js @@ -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' @@ -50,8 +50,8 @@ test('multiple keys', function (t) { } var key = 'data-' var value = 'bar' - var tree = hx`` - t.equal(vdom.create(tree).toString(), '') + var tree = hx`` + t.equal(vdom.create(tree).toString(), '') t.end() }) diff --git a/test/vdom.js b/test/vdom.js index 7753232..8365476 100644 --- a/test/vdom.js +++ b/test/vdom.js @@ -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 = `