Skip to content

Commit

Permalink
script is special
Browse files Browse the repository at this point in the history
  • Loading branch information
tammoippen committed Aug 18, 2023
1 parent 491c9d7 commit 1ad1eb9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/tagic/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,12 @@ class script(HTMLElement):
type: str | Literal["module", "importmap"] | None = None
blocking: bool = False

def do_render(self, indent: str | None) -> str:
# script-tags must have a closing tag, they cannot be self-closing
if self.children == []:
self.children = [""]
return super(script, self).do_render(indent)


@dataclass(kw_only=True, slots=True, repr=False)
class search(HTMLElement):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
main,
meta,
p,
script,
span,
title,
)
Expand All @@ -31,6 +32,11 @@ def test_str():
assert expect == str(html_tag())


def test_repr():
expect = "<!DOCTYPE html>\n<html />"
assert expect == repr(html_tag())


def test_html_with_content():
base.DOMConfig.FULL_XHTML = False
try:
Expand Down Expand Up @@ -268,3 +274,12 @@ def test_readme():
].render(
indent=True
)


def test_script_is_special():
assert '<script src="/static/htmx.min.js" type="text/javascript"></script>' == str(
script(type="text/javascript", src="/static/htmx.min.js")
)
assert "<script type=\"text/javascript\">console.log('test');</script>" == str(
script(type="text/javascript")[base.NoEscape("console.log('test');")]
)

0 comments on commit 1ad1eb9

Please sign in to comment.