Skip to content

Commit 7be4f61

Browse files
committed
Prevent falsey user inputs from breaking string_to_reactpy
1 parent 8245b64 commit 7be4f61

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/reactpy/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from lxml import etree
1010
from lxml.html import fromstring
1111

12-
from reactpy import html
12+
from reactpy import html as _html
1313
from reactpy.transforms import RequiredTransforms, attributes_to_reactjs
1414
from reactpy.types import Component, VdomDict
1515

@@ -105,9 +105,14 @@ def string_to_reactpy(
105105
event handler that prevents the browser from navigating to the link. This is
106106
useful if you would rather have `reactpy-router` handle your URL navigation.
107107
"""
108-
if not isinstance(html, str): # nocov
108+
if not html.strip():
109+
return _html.div()
110+
if not isinstance(html, str):
109111
msg = f"Expected html to be a string, not {type(html).__name__}"
110112
raise TypeError(msg)
113+
if "<" not in html or ">" not in html:
114+
msg = "Expected html string to contain HTML tags, but no tags were found."
115+
raise ValueError(msg)
111116

112117
# If the user provided a string, convert it to a list of lxml.etree nodes
113118
try:
@@ -153,7 +158,7 @@ def _etree_to_vdom(
153158
attributes = attributes_to_reactjs(dict(node.items()))
154159

155160
# Convert the lxml node to a VDOM dict
156-
constructor = getattr(html, str(node.tag))
161+
constructor = getattr(_html, str(node.tag))
157162
el = constructor(attributes, children)
158163

159164
# Perform necessary transformations on the VDOM attributes to meet VDOM spec
@@ -241,8 +246,8 @@ def component_to_vdom(component: Component) -> VdomDict:
241246
if hasattr(result, "render"):
242247
return component_to_vdom(cast(Component, result))
243248
elif isinstance(result, str):
244-
return html.div(result)
245-
return html()
249+
return _html.div(result)
250+
return _html.div()
246251

247252

248253
def _react_attribute_to_html(key: str, value: Any) -> tuple[str, str]:

0 commit comments

Comments
 (0)