Skip to content

Commit

Permalink
Fix attrs bug with 0-values
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsca committed May 29, 2024
1 parent 7bc12fc commit d523393
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/jinjax/html_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, attrs) -> None:
name = name.replace("_", "-")
if value is True:
properties.add(name)
elif value not in (False, None):
elif value is not False and value is not None:
attributes[name] = LazyString(value)

self.__attributes = attributes
Expand Down Expand Up @@ -98,7 +98,7 @@ def set(self, **kw) -> None:
"""
for name, value in kw.items():
name = name.replace("_", "-")
if value in (False, None):
if value is False or value is None:
self._remove(name)
continue

Expand Down
11 changes: 11 additions & 0 deletions tests/test_html_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def test_parse_initial_attrs():
"class": "z4 c3 a1 z4 b2",
"open": True,
"disabled": False,
"value": 0,
"foobar": None,
}
)
assert attrs.classes == "a1 b2 c3 z4"
Expand All @@ -20,7 +22,16 @@ def test_parse_initial_attrs():
assert attrs.get("title") == "hi"
assert attrs.get("open") is True
assert attrs.get("disabled", "meh") == "meh"
assert attrs.get("value") == "0"

assert attrs.get("disabled") is None
assert attrs.get("foobar") is None

attrs.set(data_value=0)
attrs.set(data_position=False)
assert attrs.get("data-value") == 0
assert attrs.get("data-position") is None
assert attrs.get("data_position") is None

def test_getattr():
attrs = HTMLAttrs(
Expand Down

0 comments on commit d523393

Please sign in to comment.