Skip to content

Commit

Permalink
Support suffix underscores for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Sep 29, 2023
1 parent 606dadd commit 3f7de9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pyhtml/__util.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ def escape_property(property_name: str) -> str:
## Replacements
* Leading underscore `_` to '' (empty string), so Python keywords can be
used for property names
* Prefix and suffix underscores `_` to '' (empty string), so Python
keywords can be used for property names
* `_` (underscore) to `-` (hyphen), so that kwargs can be used effectively
"""
return property_name \
.removeprefix('_') \
.removesuffix('_') \
.replace('_', '-')


Expand Down
15 changes: 14 additions & 1 deletion tests/escape_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,23 @@ def test_property_names_escapes_dashes():
'keyword',
keyword.kwlist,
)
def test_property_names_escapes_python_keywords(keyword):
def test_property_names_escapes_python_keywords_prefix(keyword):
"""
Since Python keywords cannot be given as kwarg names, we need to use
escaped versions (eg _for => for)
"""
kwargs = {f"_{keyword}": 'hi'}
assert str(html(**kwargs)) == f'<html {keyword}="hi"></html>'


@pytest.mark.parametrize(
'keyword',
keyword.kwlist,
)
def test_property_names_escapes_python_keywords_suffix(keyword):
"""
Since Python keywords cannot be given as kwarg names, we need to use
escaped versions (eg for_ => for)
"""
kwargs = {f"{keyword}_": 'hi'}
assert str(html(**kwargs)) == f'<html {keyword}="hi"></html>'

0 comments on commit 3f7de9e

Please sign in to comment.