|
9 | 9 | from lxml import etree |
10 | 10 | from lxml.html import fromstring |
11 | 11 |
|
12 | | -from reactpy import html |
| 12 | +from reactpy import html as _html |
13 | 13 | from reactpy.transforms import RequiredTransforms, attributes_to_reactjs |
14 | 14 | from reactpy.types import Component, VdomDict |
15 | 15 |
|
@@ -105,9 +105,14 @@ def string_to_reactpy( |
105 | 105 | event handler that prevents the browser from navigating to the link. This is |
106 | 106 | useful if you would rather have `reactpy-router` handle your URL navigation. |
107 | 107 | """ |
108 | | - if not isinstance(html, str): # nocov |
| 108 | + if not html.strip(): |
| 109 | + return _html.div() |
| 110 | + if not isinstance(html, str): |
109 | 111 | msg = f"Expected html to be a string, not {type(html).__name__}" |
110 | 112 | 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) |
111 | 116 |
|
112 | 117 | # If the user provided a string, convert it to a list of lxml.etree nodes |
113 | 118 | try: |
@@ -153,7 +158,7 @@ def _etree_to_vdom( |
153 | 158 | attributes = attributes_to_reactjs(dict(node.items())) |
154 | 159 |
|
155 | 160 | # Convert the lxml node to a VDOM dict |
156 | | - constructor = getattr(html, str(node.tag)) |
| 161 | + constructor = getattr(_html, str(node.tag)) |
157 | 162 | el = constructor(attributes, children) |
158 | 163 |
|
159 | 164 | # Perform necessary transformations on the VDOM attributes to meet VDOM spec |
@@ -241,8 +246,8 @@ def component_to_vdom(component: Component) -> VdomDict: |
241 | 246 | if hasattr(result, "render"): |
242 | 247 | return component_to_vdom(cast(Component, result)) |
243 | 248 | elif isinstance(result, str): |
244 | | - return html.div(result) |
245 | | - return html() |
| 249 | + return _html.div(result) |
| 250 | + return _html.div() |
246 | 251 |
|
247 | 252 |
|
248 | 253 | def _react_attribute_to_html(key: str, value: Any) -> tuple[str, str]: |
|
0 commit comments