|
9 | 9 | from lxml import etree |
10 | 10 | from lxml.html import fromstring |
11 | 11 |
|
12 | | -from reactpy import html |
| 12 | +from reactpy import h |
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 isinstance(html, str): |
109 | 109 | msg = f"Expected html to be a string, not {type(html).__name__}" |
110 | 110 | raise TypeError(msg) |
| 111 | + if not html.strip(): |
| 112 | + return h.fragment() |
| 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(h, str(node.tag)) |
157 | 162 | el = constructor(attributes, children) |
158 | 163 |
|
159 | 164 | # Perform necessary transformations on the VDOM attributes to meet VDOM spec |
@@ -236,13 +241,13 @@ def component_to_vdom(component: Component) -> VdomDict: |
236 | 241 | """Convert the first render of a component into a VDOM dictionary""" |
237 | 242 | result = component.render() |
238 | 243 |
|
| 244 | + if result is None: |
| 245 | + return h.fragment() |
239 | 246 | if isinstance(result, dict): |
240 | 247 | return result |
241 | 248 | if hasattr(result, "render"): |
242 | 249 | return component_to_vdom(cast(Component, result)) |
243 | | - elif isinstance(result, str): |
244 | | - return html.div(result) |
245 | | - return html() |
| 250 | + return h.div(result) if isinstance(result, str) else h.div() |
246 | 251 |
|
247 | 252 |
|
248 | 253 | def _react_attribute_to_html(key: str, value: Any) -> tuple[str, str]: |
|
0 commit comments