Skip to content

fix: preserve double underscores in attribute names#174

Open
tompassarelli wants to merge 1 commit intopelme:mainfrom
tompassarelli:fix/preserve-double-underscores
Open

fix: preserve double underscores in attribute names#174
tompassarelli wants to merge 1 commit intopelme:mainfrom
tompassarelli:fix/preserve-double-underscores

Conversation

@tompassarelli
Copy link
Copy Markdown

Problem

_python_to_html_name() replaces all underscores with hyphens via name.replace("_", "-"). This makes it impossible to express attribute names containing literal double underscores (__) through kwargs.

Frameworks like Datastar use __ as a modifier delimiter in attribute names (e.g. data-on:click__debounce.300ms, data-on:submit__prevent). Currently these render incorrectly as data-on:click--debounce.300ms.

Users can work around this by passing a positional dict instead of kwargs, but this is surprising and undocumented.

Fix

Replace the blanket replace("_", "-") with a sentinel-based approach that preserves __:

html_name.replace("__", "\x00").replace("_", "-").replace("\x00", "__")

Single underscores still convert to hyphens (existing behavior). Double underscores pass through unchanged.

Test

Added test_double_underscore_preserved verifying div(data_on_click__window="alert()") renders as <div data-on-click__window="alert()">.

All 433 existing tests pass.

Replace blanket replace("_", "-") with a sentinel-based approach that
preserves __ in attribute names. This fixes compatibility with frameworks
like Datastar that use __ as a modifier delimiter (e.g.
data-on:click__debounce.300ms, data-on:submit__prevent).
@gazpachoking
Copy link
Copy Markdown
Contributor

There is the attribute helper in the datastar sdk which works nicely with htpy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants