pythonhtmlformsvalidationhtml5lib

Validate an HTML fragment using html5lib


I'm using Python and html5lib to check if a bit of HTML code entered on a form field is valid.

I tried the following code to test a valid fragment but I'm getting an unexpected error (at least for me):

>>> import html5lib
>>> from html5lib.filters import lint
>>> fragment = html5lib.parseFragment('<p><script>alert("Boo!")</script></p>')
>>> walker = html5lib.getTreeWalker('etree')
>>> [i for i in lint.Filter(walker(fragment))]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/xyz/html5lib-1.0b3-py2.7.egg/html5lib/filters/lint.py", line 28, in __iter__
    raise LintError(_("Tag name is not a string: %(tag)r") % {"tag": name})
LintError: Tag name is not a string: u'p'

What I'm doing wrong?

My default encoding is utf-8:

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'

Solution

  • The lint filter doesn't attempt to validate HTML (uh, yeah, documentation is needed, badly… this is a large part of the reason there is no 1.0 release yet), it merely validates that the treewalker API is adhered to. Except it doesn't because it's broken because of issue #172.

    html5lib doesn't attempt to provide any validator, as it's a lot of work to implement an HTML validator.

    I'm unaware of any reasonably complete validator except for Validator.nu, though that is written in Java. It provides a web API which might be suitable for your purposes, however.