Visitar URL original
etree lxml.etree walker can't serialize full documents · Issue #345 · html5lib/html5lib-python · GitHub
The Wayback Machine - https://web.archive.org/web/20201014222847/https://github.com/html5lib/html5lib-python/issues/345
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etree lxml.etree walker can't serialize full documents #345

Open
cjerdonek opened this issue May 7, 2017 · 0 comments
Open

etree lxml.etree walker can't serialize full documents #345

cjerdonek opened this issue May 7, 2017 · 0 comments
Labels

Comments

@cjerdonek
Copy link

@cjerdonek cjerdonek commented May 7, 2017 •

The etree walker with implementation lxml.etree doesn't work when passed a full html document (having type lxml.etree._ElementTree).

To reproduce--

def serialize(element, treebuilder, implementation=None):
    walker_cls = html5lib.getTreeWalker(treebuilder, implementation=implementation)
    walker = walker_cls(element)
    serializer = HTMLSerializer(omit_optional_tags=False)
    html = serializer.render(walker)
    print(html)

html = """<!DOCTYPE html>
<html>
<head>
    <title>foo</title>
</head>
<body>
    <p>a</p><p>b</p>
</body>
</html>
"""

builder = html5lib.getTreeBuilder('lxml')
parser = html5lib.HTMLParser(builder, namespaceHTMLElements=False)
element = parser.parse(html)

serialize(element, 'lxml')
serialize(element, 'etree', implementation=lxml.etree)

The last line fails with the following error:

Traceback (most recent call last):
  File "test-html5lib.py", line 98, in <module>
    parse_and_serialize(element, 'etree', implementation=lxml.etree)
  File "test-html5lib.py", line 79, in serialize
    html = serializer.render(walker)
  File "/.../python3.6/site-packages/html5lib/serializer.py", line 323, in render
    return "".join(list(self.serialize(treewalker)))
  File "/.../python3.6/site-packages/html5lib/serializer.py", line 209, in serialize
    for token in treewalker:
  File "/.../python3.6/site-packages/html5lib/treewalkers/base.py", line 128, in __iter__
    firstChild = self.getFirstChild(currentNode)
  File "/.../python3.6/site-packages/html5lib/treewalkers/etree.py", line 88, in getFirstChild
    if element.text:
AttributeError: 'lxml.etree._ElementTree' object has no attribute 'text'

The walker should probably first be calling root = element.getroot(). This seems to be on the same wave length as the issue with treewalkers/etree.py I described in this comment: #338 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.