diff --git a/htmltools/_jsx.py b/htmltools/_jsx.py index e8769e6..71e7b57 100644 --- a/htmltools/_jsx.py +++ b/htmltools/_jsx.py @@ -23,6 +23,7 @@ HTML, MetadataNode, HTMLDependency, + head_content, ) from ._versions import versions @@ -123,6 +124,12 @@ def tagify(self) -> Tag: # metadata nodes. This could be done in two separate passes, but it's more # efficient to do it in one pass. def tagify_tagifiable_and_get_metadata(x: Any) -> Any: + if isinstance(x, Tag) and x.name == "script": + # React.createElement("script") won't invoke it's contents in the same + # way ordinary HTML tags do, but we can circumvent that by 'hoisting' it + # as head_content. + metadata_nodes.append(head_content(x)) + x = MetadataNode() if isinstance(x, Tagifiable) and not isinstance(x, (Tag, JSXTag)): x = x.tagify() else: diff --git a/tests/test_jsx_tags.py b/tests/test_jsx_tags.py index bb88ff8..b7dbe86 100644 --- a/tests/test_jsx_tags.py +++ b/tests/test_jsx_tags.py @@ -75,6 +75,7 @@ def test_jsx_tags(): jsx("`childexpression`"), Foo(), [Foo(), Bar()], + tags.script("alert('hello')"), TagList(Foo(), Bar()), span(Foo(span()), Bar()), int=1, @@ -84,9 +85,19 @@ def test_jsx_tags(): string="string", list=[1, 2, 3], ) - assert str(x) == textwrap.dedent( + assert HTMLDocument(x).render()["html"] == textwrap.dedent( """\ - + + + + + + """ + + + """ + % (react_ver, react_dom_ver, react_ver, react_dom_ver) ) x = Foo(