Skip to content

Commit c7122d5

Browse files
committed
minor refactoring
1 parent 6d63e97 commit c7122d5

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/reactpy/utils.py

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

1111
from reactpy.core.types import ComponentType, VdomDict
12-
from reactpy.core.vdom import vdom
12+
from reactpy.core.vdom import vdom as make_vdom
1313

1414
_RefValue = TypeVar("_RefValue")
1515
_ModelTransform = Callable[[VdomDict], Any]
@@ -144,7 +144,7 @@ def _etree_to_vdom(
144144
children = _generate_vdom_children(node, transforms)
145145

146146
# 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)
148148

149149
# Perform any necessary mutations on the VDOM attributes to meet VDOM spec
150150
_mutate_vdom(el)
@@ -178,25 +178,25 @@ def _add_vdom_to_etree(parent: etree._Element, node: VdomDict | dict[str, Any])
178178
c = _component_to_vdom(cast(ComponentType, c))
179179
if isinstance(c, dict):
180180
_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}"
181198
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}"
200200

201201

202202
def _mutate_vdom(vdom: VdomDict) -> None:

tests/test_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def test_del_html_body_transform():
195195

196196
@component
197197
def example_parent():
198+
return example_middle()
199+
200+
201+
@component
202+
def example_middle():
198203
return html.div({"id": "sample", "style": {"padding": "15px"}}, example_child())
199204

200205

0 commit comments

Comments
 (0)