Closed
Description
Sample implementation:
from html import escape as html_escape
def vdom_to_html(vdom: Union[str, VdomDict]) -> str:
"""Convert a VDOM dictionary into an HTML string
Only the following keys are translated to HTML:
- ``tagName``
- ``attributes``
- ``children`` (must be strings or more VDOM dicts)
"""
if isinstance(vdom, str):
return vdom
try:
tag = vdom["tagName"]
except TypeError as error:
raise TypeError(f"Expected a VDOM dictionary or string, not {vdom}") from error
if "attributes" in vdom:
attributes = " " + " ".join(
f'{k}="{html_escape(v)}"' for k, v in vdom["attributes"].items()
)
else:
attributes = ""
if "children" in vdom:
children = "".join(map(vdom_to_html, vdom["children"]))
else:
children = ""
return f"<{tag}{attributes}>{children}</{tag}>"