Visitar URL original
etree serializer fails when serializing lxml.html element containing a comment · Issue #340 · html5lib/html5lib-python · GitHub
The Wayback Machine - https://web.archive.org/web/20201014222850/https://github.com/html5lib/html5lib-python/issues/340
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 serializer fails when serializing lxml.html element containing a comment #340

Closed
cjerdonek opened this issue May 1, 2017 · 2 comments
Closed

Comments

@cjerdonek
Copy link

@cjerdonek cjerdonek commented May 1, 2017

Calling html5lib.serialize() fails when passing tree='etree' and an element containing comments created by lxml.html.

This should work because lxml.html's documentation says about itself:

The main API is based on the lxml.etree API, and thus, on the ElementTree API.

Passing tree='lxml' doesn't work as a work-around in general because of this issue: #338

To reproduce:

html = """<!DOCTYPE html>
<html>
<body>
<!-- foo -->
</body>
</html>
"""
element = lxml.html.document_fromstring(html)
# Works.
print(html5lib.serialize(element, tree='lxml'))
# Fails with the exception below.
print(html5lib.serialize(element, tree='etree'))

Here is the exception:

Traceback (most recent call last):
  File "test-html5lib.py", line 46, in <module>
    print(html5lib.serialize(element, tree='etree'))
  File "/.../python3.6/site-packages/html5lib/serializer.py", line 78, in serialize
    return s.render(walker(input), encoding)
  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/filters/optionaltags.py", line 18, in __iter__
    for previous, token, next in self.slider():
  File "/.../python3.6/site-packages/html5lib/filters/optionaltags.py", line 9, in slider
    for token in self.source:
  File "/.../python3.6/site-packages/html5lib/treewalkers/base.py", line 94, in __iter__
    details = self.getNodeDetails(currentNode)
  File "/.../python3.6/site-packages/html5lib/treewalkers/etree.py", line 61, in getNodeDetails
    assert isinstance(node.tag, string_types), type(node.tag)
AssertionError: <class 'cython_function_or_method'>

I'm guessing this issue is caused by the type-checking being done in this line:

elif node.tag == ElementTreeCommentType:
    return base.COMMENT, node.text

Instead of exact type-checking which is more fragile, I wonder if some form of duck-typing can be used or using something in the ElementTree API itself.

@cjerdonek
Copy link
Author

@cjerdonek cjerdonek commented May 1, 2017

Okay, it looks like it might work to pass lxml.etree for the implementation argument. I'm not sure if this is supported though (it's not listed as an example value for the argument in the documentation):

from html5lib.serializer import HTMLSerializer

def render(element, implementation=None):
    walker_cls = html5lib.getTreeWalker('etree', implementation=implementation)
    walker = walker_cls(element)
    serializer = HTMLSerializer()
    print(serializer.render(walker))

try:
    render(element)
except Exception as err:
    print('err: {}'.format(err))

render(element, implementation=lxml.etree)

Outputs:

err: <class 'cython_function_or_method'>
<html>
<body>
<!-- foo -->
</body>
@cjerdonek
Copy link
Author

@cjerdonek cjerdonek commented May 1, 2017

Closing as the ElementTree API doesn't seem to expose a way to detect comments (other than checking the type directly). The documentation could perhaps be improved to list lxml.etree as a (third) example value in its documentation of the implementation argument.

@cjerdonek cjerdonek closed this May 1, 2017
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
1 participant
You can’t perform that action at this time.