@@ -59,19 +59,21 @@ def __repr__(self) -> str:
5959 return f"{ type (self ).__name__ } ({ current } )"
6060
6161
62- def vdom_to_html (value : VdomDict ) -> str :
62+ def vdom_to_html (vdom : VdomDict ) -> str :
6363 """Convert a VDOM dictionary into an HTML string
6464
6565 Only the following keys are translated to HTML:
6666
6767 - ``tagName``
6868 - ``attributes``
6969 - ``children`` (must be strings or more VDOM dicts)
70+
71+ Parameters:
72+ vdom: The VdomDict element to convert to HTML
7073 """
7174 temp_root = etree .Element ("__temp__" )
72- _add_vdom_to_etree (temp_root , value )
75+ _add_vdom_to_etree (temp_root , vdom )
7376 html = cast (bytes , tostring (temp_root )).decode ()
74-
7577 # strip out temp root <__temp__> element
7678 return html [10 :- 11 ]
7779
@@ -208,11 +210,27 @@ def _add_vdom_to_etree(parent: etree._Element, vdom: VdomDict | dict[str, Any])
208210 for c in vdom .get ("children" , []):
209211 if isinstance (c , dict ):
210212 _add_vdom_to_etree (element , c )
211- elif len (element ):
212- last_child = element [- 1 ]
213- last_child .tail = f"{ last_child .tail or '' } { c } "
214213 else :
215- element .text = f"{ element .text or '' } { c } "
214+ """
215+ LXML handles string children by storing them under `text` and `tail`
216+ attributes of Element objects. The `text` attribute, if present, effectively
217+ becomes that element's first child. Then the `tail` attribute, if present,
218+ becomes a sibling that follows that element. For example, consider the
219+ following HTML:
220+
221+ <p><a>hello</a>world</p>
222+
223+ In this code sample, "hello" is the `text` attribute of the `<a>` element
224+ and "world" is the `tail` attribute of that same `<a>` element. It's for
225+ this reason that, depending on whether the element being constructed has
226+ non-string a child element, we need to assign a `text` vs `tail` attribute
227+ to that element or the last non-string child respectively.
228+ """
229+ if len (element ):
230+ last_child = element [- 1 ]
231+ last_child .tail = f"{ last_child .tail or '' } { c } "
232+ else :
233+ element .text = f"{ element .text or '' } { c } "
216234
217235
218236def _mutate_vdom (vdom : VdomDict ) -> None :
0 commit comments