Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Jan 26, 2025
1 parent 6d63e97 commit c7122d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/reactpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from lxml.html import fromstring, tostring

from reactpy.core.types import ComponentType, VdomDict
from reactpy.core.vdom import vdom
from reactpy.core.vdom import vdom as make_vdom

_RefValue = TypeVar("_RefValue")
_ModelTransform = Callable[[VdomDict], Any]
Expand Down Expand Up @@ -144,7 +144,7 @@ def _etree_to_vdom(
children = _generate_vdom_children(node, transforms)

# Convert the lxml node to a VDOM dict
el = vdom(node.tag, dict(node.items()), *children)
el = make_vdom(node.tag, dict(node.items()), *children)

# Perform any necessary mutations on the VDOM attributes to meet VDOM spec
_mutate_vdom(el)
Expand Down Expand Up @@ -178,25 +178,25 @@ def _add_vdom_to_etree(parent: etree._Element, node: VdomDict | dict[str, Any])
c = _component_to_vdom(cast(ComponentType, c))
if isinstance(c, dict):
_add_vdom_to_etree(element, c)

# LXML handles string children by storing them under `text` and `tail`
# attributes of Element objects. The `text` attribute, if present, effectively
# becomes that element's first child. Then the `tail` attribute, if present,
# becomes a sibling that follows that element. For example, consider the
# following HTML:

# <p><a>hello</a>world</p>

# In this code sample, "hello" is the `text` attribute of the `<a>` element
# and "world" is the `tail` attribute of that same `<a>` element. It's for
# this reason that, depending on whether the element being constructed has
# non-string a child element, we need to assign a `text` vs `tail` attribute
# to that element or the last non-string child respectively.
elif len(element):
last_child = element[-1]
last_child.tail = f"{last_child.tail or ''}{c}"
else:
# LXML handles string children by storing them under `text` and `tail`
# attributes of Element objects. The `text` attribute, if present, effectively
# becomes that element's first child. Then the `tail` attribute, if present,
# becomes a sibling that follows that element. For example, consider the
# following HTML:

# <p><a>hello</a>world</p>

# In this code sample, "hello" is the `text` attribute of the `<a>` element
# and "world" is the `tail` attribute of that same `<a>` element. It's for
# this reason that, depending on whether the element being constructed has
# non-string a child element, we need to assign a `text` vs `tail` attribute
# to that element or the last non-string child respectively.
if len(element):
last_child = element[-1]
last_child.tail = f"{last_child.tail or ''}{c}"
else:
element.text = f"{element.text or ''}{c}"
element.text = f"{element.text or ''}{c}"


def _mutate_vdom(vdom: VdomDict) -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def test_del_html_body_transform():

@component
def example_parent():
return example_middle()


@component
def example_middle():
return html.div({"id": "sample", "style": {"padding": "15px"}}, example_child())


Expand Down

0 comments on commit c7122d5

Please sign in to comment.