From 238830d40016fc0dc29ae1817f9b12820ab6519e 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 +++++++++++++++++++++ test/key.js | 6 +++--- test/vdom.js | 27 ++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f254f12..b793aed 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 = /^style$/.test(key) && typeof props[key] === 'string' + if (isDataAria || isStyleString) { + props.attributes[key] = props[key] + delete props[key] + } + }) + + return h(tagName, props, children) + } +} 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() +})