|
9 | 9 | from lxml.html import fromstring, tostring
|
10 | 10 |
|
11 | 11 | from reactpy.core.types import ComponentType, VdomDict
|
12 |
| -from reactpy.core.vdom import vdom |
| 12 | +from reactpy.core.vdom import vdom as make_vdom |
13 | 13 |
|
14 | 14 | _RefValue = TypeVar("_RefValue")
|
15 | 15 | _ModelTransform = Callable[[VdomDict], Any]
|
@@ -144,7 +144,7 @@ def _etree_to_vdom(
|
144 | 144 | children = _generate_vdom_children(node, transforms)
|
145 | 145 |
|
146 | 146 | # Convert the lxml node to a VDOM dict
|
147 |
| - el = vdom(node.tag, dict(node.items()), *children) |
| 147 | + el = make_vdom(node.tag, dict(node.items()), *children) |
148 | 148 |
|
149 | 149 | # Perform any necessary mutations on the VDOM attributes to meet VDOM spec
|
150 | 150 | _mutate_vdom(el)
|
@@ -178,25 +178,25 @@ def _add_vdom_to_etree(parent: etree._Element, node: VdomDict | dict[str, Any])
|
178 | 178 | c = _component_to_vdom(cast(ComponentType, c))
|
179 | 179 | if isinstance(c, dict):
|
180 | 180 | _add_vdom_to_etree(element, c)
|
| 181 | + |
| 182 | + # LXML handles string children by storing them under `text` and `tail` |
| 183 | + # attributes of Element objects. The `text` attribute, if present, effectively |
| 184 | + # becomes that element's first child. Then the `tail` attribute, if present, |
| 185 | + # becomes a sibling that follows that element. For example, consider the |
| 186 | + # following HTML: |
| 187 | + |
| 188 | + # <p><a>hello</a>world</p> |
| 189 | + |
| 190 | + # In this code sample, "hello" is the `text` attribute of the `<a>` element |
| 191 | + # and "world" is the `tail` attribute of that same `<a>` element. It's for |
| 192 | + # this reason that, depending on whether the element being constructed has |
| 193 | + # non-string a child element, we need to assign a `text` vs `tail` attribute |
| 194 | + # to that element or the last non-string child respectively. |
| 195 | + elif len(element): |
| 196 | + last_child = element[-1] |
| 197 | + last_child.tail = f"{last_child.tail or ''}{c}" |
181 | 198 | else:
|
182 |
| - # LXML handles string children by storing them under `text` and `tail` |
183 |
| - # attributes of Element objects. The `text` attribute, if present, effectively |
184 |
| - # becomes that element's first child. Then the `tail` attribute, if present, |
185 |
| - # becomes a sibling that follows that element. For example, consider the |
186 |
| - # following HTML: |
187 |
| - |
188 |
| - # <p><a>hello</a>world</p> |
189 |
| - |
190 |
| - # In this code sample, "hello" is the `text` attribute of the `<a>` element |
191 |
| - # and "world" is the `tail` attribute of that same `<a>` element. It's for |
192 |
| - # this reason that, depending on whether the element being constructed has |
193 |
| - # non-string a child element, we need to assign a `text` vs `tail` attribute |
194 |
| - # to that element or the last non-string child respectively. |
195 |
| - if len(element): |
196 |
| - last_child = element[-1] |
197 |
| - last_child.tail = f"{last_child.tail or ''}{c}" |
198 |
| - else: |
199 |
| - element.text = f"{element.text or ''}{c}" |
| 199 | + element.text = f"{element.text or ''}{c}" |
200 | 200 |
|
201 | 201 |
|
202 | 202 | def _mutate_vdom(vdom: VdomDict) -> None:
|
|
0 commit comments