Skip to content

Commit

Permalink
Bugfix: correct handling of void elements without / in endtag
Browse files Browse the repository at this point in the history
  • Loading branch information
OleJoik committed Jun 12, 2024
1 parent b59ea12 commit be15515
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion htpy/html2htpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

__all__ = ["html2htpy"]

_void_elements = [
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"param",
"source",
"track",
"wbr",
]


class Tag:
def __init__(
Expand Down Expand Up @@ -144,7 +161,8 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None
else:
self._current.children.append(t)

self._current = t
if tag not in _void_elements:
self._current = t

def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
t = Tag(tag, attrs, parent=self._current)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_html2htpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@ def test_convert_empty_elements() -> None:
assert actual == "[div,p,span]"


def test_convert_void_elements() -> None:
input = """
<div>
<div>
<input type="text" />
</div>
<div>
<input type="text">
</div>
</div>
"""

actual = html2htpy(input)
assert actual == 'div[div[input(type="text")],div[input(type="text")]]'


def test_convert_custom_tag() -> None:
input = """
<custom-element attribute="value">Custom content</custom-element>
Expand Down

0 comments on commit be15515

Please sign in to comment.