Skip to content

Commit 556d3bb

Browse files
committed
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 choojs#28
1 parent b73b141 commit 556d3bb

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var ATTR_EQ = 11, ATTR_BREAK = 12
99
module.exports = function (h, opts) {
1010
h = attrToProp(h)
1111
if (!opts) opts = {}
12+
13+
if (opts.vdom) {h = fixVdom(h)}
14+
1215
var concat = opts.concat || function (a, b) {
1316
return String(a) + String(b)
1417
}
@@ -261,3 +264,21 @@ var closeRE = RegExp('^(' + [
261264
'vkern'
262265
].join('|') + ')(?:[\.#][a-zA-Z0-9\u007F-\uFFFF_:-]+)*$')
263266
function selfClosing (tag) { return closeRE.test(tag) }
267+
268+
function fixVdom (h) {
269+
return function (tagName, props, children) {
270+
271+
if (!props.attributes) props.attributes = {}
272+
273+
Object.keys(props).forEach(function (key) {
274+
var isDataAria = /^(data|aria)-/.test(key)
275+
var isStyleString = /^style$/.test(key) && typeof props[key] === 'string'
276+
if (isDataAria || isStyleString) {
277+
props.attributes[key] = props[key]
278+
delete props[key]
279+
}
280+
})
281+
282+
return h(tagName, props, children)
283+
}
284+
}

test/key.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var test = require('tape')
22
var vdom = require('virtual-dom')
33
var hyperx = require('../')
4-
var hx = hyperx(vdom.h)
4+
var hx = hyperx(vdom.h, {vdom: true})
55

66
test('key', function (t) {
77
var key = 'type'
@@ -50,8 +50,8 @@ test('multiple keys', function (t) {
5050
}
5151
var key = 'data-'
5252
var value = 'bar'
53-
var tree = hx`<input ${props} ${key}foo=${value}>`
54-
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" />')
53+
var tree = hx`<input ${props} ${key}foo=${value} data-bar="baz">`
54+
t.equal(vdom.create(tree).toString(), '<input type="text" data-special="true" data-foo="bar" data-bar="baz" />')
5555
t.end()
5656
})
5757

test/vdom.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var test = require('tape')
22
var vdom = require('virtual-dom')
33
var hyperx = require('../')
4-
var hx = hyperx(vdom.h)
4+
var hx = hyperx(vdom.h, {vdom: true})
55

66
var expected = `<div>
77
<h1 y="ab3cd">hello world!</h1>
@@ -24,3 +24,28 @@ test('vdom', function (t) {
2424
t.equal(vdom.create(tree).toString(), expected)
2525
t.end()
2626
})
27+
28+
test('vdom - style as object', function (t) {
29+
var styleObj = {backgroundColor: 'red'}
30+
var tree = hx`<div class="box" style=${styleObj}></div>`
31+
var expected = '<div style="background-color:red;" class="box"></div>'
32+
var actual = vdom.create(tree).toString()
33+
t.equal(actual, expected)
34+
t.end()
35+
})
36+
37+
test('vdom - style as string', function (t) {
38+
var tree = hx`<div class="box" style="background-color:red;"></div>`
39+
var expected = '<div style="background-color:red;" class="box"></div>'
40+
var actual = vdom.create(tree).toString()
41+
t.equal(actual, expected)
42+
t.end()
43+
})
44+
45+
test('vdom - accessibility attributes', function (t) {
46+
var tree = hx`<div aria-hidden="${true}" aria-label="test-label" role="dialog" tabindex=4></div>`
47+
var expected = '<div role="dialog" tabindex="4" aria-hidden="true" aria-label="test-label"></div>'
48+
var actual = vdom.create(tree).toString()
49+
t.equal(actual, expected)
50+
t.end()
51+
})

0 commit comments

Comments
 (0)