Skip to content

exempt strings from being treated as known booleans unless empty or equal to name #46

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

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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
44 changes: 44 additions & 0 deletions test/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
}
)

// TODO: why? "hidden" is a valid value for the `hidden` attribute

Check warning on line 143 in test/attribute.js

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Unexpected 'todo' comment: 'TODO: why? "hidden" is a valid value for...'.

Check warning on line 143 in test/attribute.js

View workflow job for this annotation

GitHub Actions / node

Unexpected 'todo' comment: 'TODO: why? "hidden" is a valid value for...'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because hidden looks better? Because hidden="hidden" is longer and superfluous? Because it’s how people often write HTML?

This package is also used in combination of minifiers / pritifyers. So it’s not solely about what “works”.

🤔 I’d say this comment can be removed?

await t.test(
'should serialize known booleans set to their name without value',
async function () {
Expand All @@ -161,6 +162,49 @@
)
}
)

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
Loading