Skip to content

Commit

Permalink
interpolate function in attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
bredele committed Sep 3, 2016
1 parent 681125e commit ca986b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function attribute(node, values) {
// faster than split?
node.nodeValue = node.nodeValue.replace(/\$\{0\}/g, function() {
var value = values.shift();
if(typeof value === 'function') value = value()
if(typeof value === 'object') {
if(value instanceof Array) value = value.join(' ')
else value = styles(value)
Expand Down
13 changes: 12 additions & 1 deletion test/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ tape('should interpolate arrays', (test) => {
})


tape('shound interpolate objects', (test) => {
tape('should interpolate objects', (test) => {
test.plan(1)
var styles = {
width: 100 + 'px',
Expand All @@ -54,3 +54,14 @@ tape('shound interpolate objects', (test) => {
var btn = vomit`<button style="${styles}">hello</button>`
test.equal(btn.outerHTML, '<button style="width:100px;height:200px;">hello</button>')
})


tape('should interpolate function', (test) => {
test.plan(1)
var bool = true
var fn = function() {
return bool ? 'show' : 'hide'
}
var btn = vomit`<button class="${fn}">hello</button>`
test.equal(btn.outerHTML, '<button class="show">hello</button>')
})

0 comments on commit ca986b7

Please sign in to comment.