You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
html_to_vdom breaks on whitespaces and null HTML tags #777
The issue I categorized as a "script tag" issue appears to be related to HTML doesn't have end tags (such as <hr>) break the HTMLParser implementation that is currently developed. It was just coincidence that my script tag was following a <hr>. I am looking into a solution.
Googling and testing shows me there doesn't appear to be a fool proof way of resolving this with python's built-in HtmlParser. It's technically a syntax tree parser and not a DOM tree generator. The combination of handle_starttag, handle_endtag, and handle_startendtag are not robust enough to handle these scenarios without committing some really ugly code.
In the interest of maintainability, I'm going to switch us to using lxml or BeautifulSoup using the lxml backend. A nice benefit of the switch is it technically should be much faster since lxml is C based.
That target parser interface of lxml seems like it maps nicely onto what we already do. I wasn't able to find something similar in bs4, but I didn't look that hard.
Using that interface would likely make us fall into the same pitfalls, since it doesn't provide access to the DOM tree. I've been testing out performing these transformations recursively.
Seems to work off my rudimentary test, and is a lot more readable.
fromioimportStringIOfrompprintimportpprintfromtypingimportUnionfromlxmlimportetreemy_html="""<div id="view_to_component_middleware"> view_to_component_middleware: Success <div class="inner"> this is text </div></div>\n<hr><script> var dog = 'memes';</script>"""def_set_if_val_exists(object, key, value):
"""Sets a key on a dictionary if the value's length is greater than 0."""iflen(value):
object[key] =valuedefhtml_to_vdom(html: Union[str, etree._Element], *transforms):
"""Convert an lxml.etree node tree into a VDOM dict."""# If the user provided a string, convert it to an lxml.etree node.ifisinstance(html, str):
parser=etree.HTMLParser()
tree=etree.parse(StringIO(html), parser)
node=tree.getroot()
elifisinstance(html, etree._Element):
node=htmlelse:
raiseTypeError("html_to_vdom expects a string or lxml.etree._Element")
# Convert the lxml.etree node to a VDOM dict.vdom= {"tagName": node.tag}
_set_if_val_exists(vdom, "attributes", dict(node.items()))
_set_if_val_exists(
vdom, "children", [html_to_vdom(child) forchildinnode.iterchildren(None)]
)
# Apply any transforms.fortransformintransforms:
vdom=transform(vdom)
returnvdompprint(html_to_vdom(my_html))
Activity
Archmonger commentedon Jul 1, 2022
@rmorshea Would like you to chime in on this one too, while you still have availability.
rmorshea commentedon Jul 1, 2022
I'll take a look at this over the weekend.
Archmonger commentedon Aug 2, 2022
The issue I categorized as a "script tag" issue appears to be related to HTML doesn't have end tags (such as
<hr>) break the HTMLParser implementation that is currently developed. It was just coincidence that my script tag was following a<hr>. I am looking into a solution.Archmonger commentedon Aug 3, 2022
Googling and testing shows me there doesn't appear to be a fool proof way of resolving this with python's built-in
HtmlParser. It's technically a syntax tree parser and not a DOM tree generator. The combination ofhandle_starttag,handle_endtag, andhandle_startendtagare not robust enough to handle these scenarios without committing some really ugly code.In the interest of maintainability, I'm going to switch us to using
lxmlorBeautifulSoupusing thelxmlbackend. A nice benefit of the switch is it technically should be much faster sincelxmlis C based.rmorshea commentedon Aug 3, 2022
That target parser interface of
lxmlseems like it maps nicely onto what we already do. I wasn't able to find something similar inbs4, but I didn't look that hard.Archmonger commentedon Aug 4, 2022
Using that interface would likely make us fall into the same pitfalls, since it doesn't provide access to the DOM tree. I've been testing out performing these transformations recursively.
Seems to work off my rudimentary test, and is a lot more readable.
[-]html_to_vdom breaks on Script tag and whitespaces[/-][+]html_to_vdom breaks on whitespaces and null HTML tags[/+]