@@ -59,19 +59,21 @@ def __repr__(self) -> str:
59
59
return f"{ type (self ).__name__ } ({ current } )"
60
60
61
61
62
- def vdom_to_html (value : VdomDict ) -> str :
62
+ def vdom_to_html (vdom : VdomDict ) -> str :
63
63
"""Convert a VDOM dictionary into an HTML string
64
64
65
65
Only the following keys are translated to HTML:
66
66
67
67
- ``tagName``
68
68
- ``attributes``
69
69
- ``children`` (must be strings or more VDOM dicts)
70
+
71
+ Parameters:
72
+ vdom: The VdomDict element to convert to HTML
70
73
"""
71
74
temp_root = etree .Element ("__temp__" )
72
- _add_vdom_to_etree (temp_root , value )
75
+ _add_vdom_to_etree (temp_root , vdom )
73
76
html = cast (bytes , tostring (temp_root )).decode ()
74
-
75
77
# strip out temp root <__temp__> element
76
78
return html [10 :- 11 ]
77
79
@@ -208,11 +210,27 @@ def _add_vdom_to_etree(parent: etree._Element, vdom: VdomDict | dict[str, Any])
208
210
for c in vdom .get ("children" , []):
209
211
if isinstance (c , dict ):
210
212
_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 } "
214
213
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 } "
216
234
217
235
218
236
def _mutate_vdom (vdom : VdomDict ) -> None :
0 commit comments