Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 02a01ac

Browse files
committed
Handle aria dicts correctly according to the weird ARIA spec for boolean attrs
1 parent 720d8d9 commit 02a01ac

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

html_tstring/processor.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,28 @@ def _force_dict(value: t.Any, *, kind: str) -> dict:
9898
) from None
9999

100100

101-
def _substitute_attrs_dict(
102-
value: object, *, kind: str
103-
) -> t.Iterable[tuple[str, str | None]]:
104-
"""Substitute attributes based on the interpolated value being a dict."""
105-
d = _force_dict(value, kind=kind)
106-
for sub_k, sub_v in d.items():
107-
if sub_v is True:
108-
yield f"{kind}-{sub_k}", None
109-
elif sub_v not in (False, None):
110-
yield f"{kind}-{sub_k}", str(sub_v)
111-
112-
113101
def _substitute_aria_attrs(value: object) -> t.Iterable[tuple[str, str | None]]:
114102
"""Produce aria-* attributes based on the interpolated value for "aria"."""
115-
return _substitute_attrs_dict(value, kind="aria")
103+
d = _force_dict(value, kind="aria")
104+
for sub_k, sub_v in d.items():
105+
if sub_v is True:
106+
yield f"aria-{sub_k}", "true"
107+
elif sub_v is False:
108+
yield f"aria-{sub_k}", "false"
109+
elif sub_v is None:
110+
pass
111+
else:
112+
yield f"aria-{sub_k}", str(sub_v)
116113

117114

118115
def _substitute_data_attrs(value: object) -> t.Iterable[tuple[str, str | None]]:
119116
"""Produce data-* attributes based on the interpolated value for "data"."""
120-
return _substitute_attrs_dict(value, kind="data")
117+
d = _force_dict(value, kind="data")
118+
for sub_k, sub_v in d.items():
119+
if sub_v is True:
120+
yield f"data-{sub_k}", None
121+
elif sub_v not in (False, None):
122+
yield f"data-{sub_k}", str(sub_v)
121123

122124

123125
def _substitute_class_attr(value: object) -> t.Iterable[tuple[str, str | None]]:

html_tstring/processor_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ def test_interpolated_aria_attributes():
430430
node = html(t"<button aria={aria}>X</button>")
431431
assert node == Element(
432432
"button",
433-
attrs={"aria-label": "Close", "aria-hidden": None},
433+
attrs={"aria-label": "Close", "aria-hidden": "true"},
434434
children=[Text("X")],
435435
)
436-
assert str(node) == '<button aria-label="Close" aria-hidden>X</button>'
436+
assert str(node) == '<button aria-label="Close" aria-hidden="true">X</button>'
437437

438438

439439
def test_interpolated_style_attribute():

0 commit comments

Comments
 (0)