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

Commit d3cddaa

Browse files
committed
Convert dashed attribute names to underscores for component props
1 parent a2533ec commit d3cddaa

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

html_tstring/processor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,11 @@ def _invoke_component(
323323
raise TypeError(
324324
f"Expected a callable for component invocation, got {type(value).__name__}"
325325
)
326+
# Replace attr names hyphens with underscores for Python kwargs
327+
kwargs = {k.replace("-", "_"): v for k, v in new_attrs.items()}
328+
326329
# Call the component and return the resulting node
327-
result = value(*new_children, **new_attrs)
330+
result = value(*new_children, **kwargs)
328331
match result:
329332
case Node():
330333
return result

html_tstring/processor_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,14 @@ def test_interpolated_style_attribute():
476476

477477

478478
def TemplateComponent(
479-
*children: Node, first: str, second: int, third: str, **attrs: t.Any
479+
*children: Node, first: str, second: int, third_arg: str, **attrs: t.Any
480480
) -> Template:
481481
# Ensure type correctness of props at runtime for testing purposes
482482
assert isinstance(first, str)
483483
assert isinstance(second, int)
484-
assert isinstance(third, str)
484+
assert isinstance(third_arg, str)
485485
new_attrs = {
486-
"id": third,
486+
"id": third_arg,
487487
"data": {"first": first, "second": second},
488488
**attrs,
489489
}
@@ -492,7 +492,7 @@ def TemplateComponent(
492492

493493
def test_interpolated_template_component():
494494
node = html(
495-
t'<{TemplateComponent} first=1 second={99} third="comp1" class="my-comp">Hello, Component!</{TemplateComponent}>'
495+
t'<{TemplateComponent} first=1 second={99} third-arg="comp1" class="my-comp">Hello, Component!</{TemplateComponent}>'
496496
)
497497
assert node == Element(
498498
"div",
@@ -539,13 +539,13 @@ def test_fragment_from_component():
539539

540540

541541
def test_component_passed_as_attr_value():
542-
def WrapperComponent(
543-
*children: Node, another: ComponentCallable, **attrs: t.Any
542+
def Wrapper(
543+
*children: Node, sub_component: ComponentCallable, **attrs: t.Any
544544
) -> Template:
545-
return t"<{another} {attrs}>{children}</{another}>"
545+
return t"<{sub_component} {attrs}>{children}</{sub_component}>"
546546

547547
node = html(
548-
t'<{WrapperComponent} another={TemplateComponent} class="wrapped" first=1 second={99} third="comp1"><p>Inside wrapper</p></{WrapperComponent}>'
548+
t'<{Wrapper} sub-component={TemplateComponent} class="wrapped" first=1 second={99} third-arg="comp1"><p>Inside wrapper</p></{Wrapper}>'
549549
)
550550
assert node == Element(
551551
"div",

0 commit comments

Comments
 (0)