Skip to content

Instantly share code, notes, and snippets.

@indivisible
Created May 2, 2017 16:36
Show Gist options
  • Select an option

  • Save indivisible/c31f800e9c0b463009f5f69f65aecba7 to your computer and use it in GitHub Desktop.

Select an option

Save indivisible/c31f800e9c0b463009f5f69f65aecba7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import html5lib
def test_parse_serialize(html, tree_type):
walker = html5lib.getTreeWalker(tree_type)
serializer = html5lib.serializer.HTMLSerializer()
#root = html5lib.parseFragment(html, namespaceHTMLElements=False, treebuilder=tree_type)
root = html5lib.parseFragment(html, treebuilder=tree_type)
stream = walker(root)
return serializer.render(stream)
def test_html(html, tree_types=('etree', 'lxml')):
print ('\ntesting %r:' % html)
for tt in ('etree', 'lxml'):
print (' with %r:\n %r' % (tt, test_parse_serialize(html, tt)))
if __name__ == '__main__':
# works
test_html('<a>one</a>')
# errors out for 'lxml' ('etree' works fine)
test_html('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment