Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leave interpreting attribute values to the renderer #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ module.exports = function (h, opts) {
else cur[1][key] = concat(cur[1][key], parts[i][1])
} else if (parts[i][0] === VAR
&& (parts[i][1] === ATTR_VALUE || parts[i][1] === ATTR_KEY)) {
if (!cur[1][key]) cur[1][key] = strfn(parts[i][2])
if (!cur[1][key])
if (parts[i][1] === ATTR_KEY) cur[1][key] = strfn(parts[i][2])
else cur[1][key] = parts[i][2]
else cur[1][key] = concat(cur[1][key], parts[i][2])
} else {
if (key.length && !cur[1][key] && i === j
Expand Down
28 changes: 28 additions & 0 deletions test/attr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var test = require('tape')
var vdom = require('virtual-dom')
var hyperscript = require('hyperscript')
var hyperx = require('../')
var hx = hyperx(vdom.h)

Expand All @@ -9,6 +10,33 @@ test('class', function (t) {
t.end()
})

test('undefined attribute value', function (t) {
var tree = hx`<div data-meh=${undefined}></div>`
t.equal(vdom.create(tree).toString(), '<div></div>')
t.end()
})

test('empty string attribute value', function (t) {
var tree = hx`<div data-meh=${''}></div>`
t.equal(vdom.create(tree).toString(), '<div data-meh=""></div>')
t.end()
})

// running this test with hyperscript because it
// breaks with virtual-dom (with "str.replace is not a function")
test('false attribute value', function (t) {
var hx = hyperx(hyperscript)
var tree = hx`<div data-meh=${false}></div>`
t.equal(tree.outerHTML, '<div data-meh=""></div>')
t.end()
})

test('null attribute value', function (t) {
var tree = hx`<div data-meh=${null}></div>`
t.equal(vdom.create(tree).toString(), '<div></div>')
t.end()
})

test('boolean attribute', function (t) {
var tree = hx`<video autoplay></video>`
t.equal(vdom.create(tree).toString(), '<video autoplay="autoplay"></video>')
Expand Down