Skip to content

Conversation

@erikgaas
Copy link
Contributor

@erikgaas erikgaas commented Dec 3, 2025

Relates to this issue in fasthtml: AnswerDotAI/fasthtml#567

Currently we suppress any attribute whose value is a 0 or an empty string. However there are legimitate use cases where these can these might exist.

For example

  • to_xml(Input(type="number", min=0, max=100))
  • to_xml(Option("select a value", value="")

The former example I think is purely a bug caused by:
v not in (False, None, '') Python evaluates 0 == False to true so this is an edge case

Meanwhile empty strings are more debatable. Google's dom manipulation strips out the empty string leaving the value which seems to be the same as setting it to True. https://stackoverflow.com/questions/14352113/setting-an-html-option-value-to-the-empty-string

It might be fair to say that we would just expect users to pass in None if they done want that attribute showing up in the final rendering.

@erikgaas erikgaas requested a review from jph00 December 3, 2025 06:19
@erikgaas erikgaas added the bug Something isn't working label Dec 5, 2025
@jph00
Copy link
Contributor

jph00 commented Dec 7, 2025

This seems reasonable. :) The current behavior of False causing an attr not to appear is widely used, so it's good we're keeping that behaviour.

@jph00 jph00 merged commit 307486c into main Dec 7, 2025
14 checks passed
@jph00 jph00 added enhancement New feature or request and removed bug Something isn't working labels Dec 7, 2025
@jph00 jph00 changed the title Zero and empty string treated as nullable for xml creation Support setting zero and empty string as element attrs Dec 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants