From 39a1feb92058b9d415b57410503a616d0ba162c9 Mon Sep 17 00:00:00 2001 From: Tyler Waters Date: Fri, 8 Jul 2016 20:29:19 -0700 Subject: [PATCH] fixes to vdom rendering - 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 --- index.js | 21 +++++++++++++++++++++ readme.markdown | 3 +++ test/key.js | 6 +++--- test/vdom.js | 27 ++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 4 deletions(-) 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 = `

hello world!

@@ -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`
` + var expected = '
' + var actual = vdom.create(tree).toString() + t.equal(actual, expected) + t.end() +}) + +test('vdom - style as string', function (t) { + var tree = hx`
` + var expected = '
' + var actual = vdom.create(tree).toString() + t.equal(actual, expected) + t.end() +}) + +test('vdom - accessibility attributes', function (t) { + var tree = hx`
` + var expected = '' + var actual = vdom.create(tree).toString() + t.equal(actual, expected) + t.end() +})