Skip to content

Commit 4fa5552

Browse files
authored
fix(script tag): explicit closing tag (#6)
* fix(script tag): explicit closing tag
1 parent 7dbd010 commit 4fa5552

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "python-hiccup"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.10"

src/python_hiccup/html/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def _to_bool_attributes(acc: str, attributes: set) -> str:
4343
return _join(acc, attrs)
4444

4545

46+
def _closing_tag(element: str) -> bool:
47+
specials = {"script"}
48+
49+
return str.lower(element) in specials
50+
51+
4652
def _suffix(element_data: str) -> str:
4753
specials = {"doctype"}
4854
normalized = str.lower(element_data)
@@ -68,6 +74,9 @@ def _to_html(tag: Mapping) -> list:
6874
if flattened or content:
6975
return [f"<{begin}>", *flattened, *content, f"</{element}>"]
7076

77+
if _closing_tag(element):
78+
return [f"<{begin}>", f"</{element}>"]
79+
7180
extra = _suffix(begin)
7281

7382
return [f"<{begin}{extra}>"]

test/test_render_html.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ def test_parses_attribute_shorthand() -> None:
4141
assert render(data) == expected
4242

4343

44+
def test_explicit_closing_tag() -> None:
45+
"""Assert that some html elements are parsed with a closing tag."""
46+
data = ["script"]
47+
48+
expected = "<script></script>"
49+
50+
assert render(data) == expected
51+
52+
4453
def test_parses_boolean_attributes() -> None:
4554
"""Assert that attributes without values, such as async or defer, is parsed as expected."""
4655
data = ["script", {"async"}, {"src": "path/to/script"}]
4756

48-
expected = '<script src="path/to/script" async />'
57+
expected = '<script src="path/to/script" async></script>'
4958

5059
assert render(data) == expected
5160

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)