Skip to content

Commit

Permalink
Fixed issue #55 undefined attribute values are converted to strings (#57
Browse files Browse the repository at this point in the history
)

* Fixed issue #55 undefined attribute values are converted to strings

* ignore attribute if injected value is null or undefined
  • Loading branch information
s9k authored and goto-bus-stop committed Feb 11, 2019
1 parent b5ef35c commit 24aeb68
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ module.exports = function (h, opts) {
if (typeof x === 'function') return x
else if (typeof x === 'string') return x
else if (x && typeof x === 'object') return x
else if (x === null || x === undefined) return x
else return concat('', x)
}
}
Expand Down
36 changes: 36 additions & 0 deletions test/attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,39 @@ test('strange inbetween character attributes', function (t) {
t.equal(vdom.create(tree).toString(), `<div f@o="bar" b&z="qux"></div>`)
t.end()
})

test('null and undefined attributes', function (t) {
var tree = hx`<div onclick="alert(1)" onmouseenter=${undefined} onmouseleave=${null}></div>`
t.equal(vdom.create(tree).toString(), `<div onclick="alert(1)"></div>`)
t.end()
})

test('undefined (with quotes) attribute value is evaluated', function (t) {
var tree = hx`<div foo='undefined'></div>`
t.equal(vdom.create(tree).toString(), `<div foo="undefined"></div>`)
t.end()
})

test('null (with quotes) attribute value is evaluated', function (t) {
var tree = hx`<div foo='null'></div>`
t.equal(vdom.create(tree).toString(), `<div foo="null"></div>`)
t.end()
})

test('undefined (without quotes) attribute value is evaluated', function (t) {
var tree = hx`<div foo=undefined></div>`
t.equal(vdom.create(tree).toString(), `<div foo="undefined"></div>`)
t.end()
})

test('null (without quotes) attribute value is evaluated', function (t) {
var tree = hx`<div foo=null></div>`
t.equal(vdom.create(tree).toString(), `<div foo="null"></div>`)
t.end()
})

test('null is ignored and adjacent attribute is evaluated', function (t) {
var tree = hx`<div foo=${null} t></div>`
t.equal(vdom.create(tree).toString(), `<div t="t"></div>`)
t.end()
})

0 comments on commit 24aeb68

Please sign in to comment.