Skip to content

Commit

Permalink
Fix to allow other strings for boolean attributes
Browse files Browse the repository at this point in the history
Closes GH-45.
Closes GH-46.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
hesxenon authored Dec 13, 2024
1 parent fb74df8 commit 6000808
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/handle/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ function serializeAttribute(state, key, value) {
if (info.overloadedBoolean && (value === info.attribute || value === '')) {
value = true
} else if (
info.boolean ||
(info.overloadedBoolean && typeof value !== 'string')
(typeof value !== 'string' || value === '' || value === info.attribute) &&
(info.boolean || info.overloadedBoolean)
) {
value = Boolean(value)
}
Expand Down
43 changes: 43 additions & 0 deletions test/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,49 @@ test('`element` attributes', async (t) => {
)
}
)

await t.test(
'should serialize known booleans set to arbitrary strings with value',
async function () {
assert.deepEqual(
toHtml(
h('div', {
selected: 'some string value for a well known boolean attribute'
})
),
'<div selected="some string value for a well known boolean attribute"></div>'
)
}
)

await t.test(
'should serialize known booleans set to an empty string without value',
async function () {
assert.deepEqual(
toHtml(
h('div', {
selected: ''
})
),
'<div selected></div>'
)
}
)

await t.test(
'should serialize known overloaded booleans set to arbitrary strings with value',
async function () {
assert.deepEqual(
toHtml(
h('div', {
download:
'some string value for a well known overloaded boolean attribute'
})
),
'<div download="some string value for a well known overloaded boolean attribute"></div>'
)
}
)
})

await t.test('should support known overloaded booleans', async function (t) {
Expand Down

0 comments on commit 6000808

Please sign in to comment.