2

I want to detect errors in my HTML during development.

Example:

<tr><td>foo</td><a href="...">...</a></tr>

This should be detected.

Required Features:

  • open source
  • self-hosting: the library should not need a service like https://validator.w3.org/
  • Unknown attributes should get detected (example "hrf" instead of "href").
  • extensible: There should be a way to configure it, so that non-standard attributes (example like https://htmx.org/) are ignored.
  • Command line interface or library which is usable via python.
  • Should work for html5

Background: I did a refactoring in my code and no automated test complained. But there were bugs (see above example). I would like to automate html checking to be sure this does not happen again.

BTW: It is enough to detect the error. No automatic fixing should be attempted.

6
  • I always get some good initial results with xmllint. For the w3 validator did you have a look at github.com/w3c/link-checker.git?
    – albert
    Commented Dec 22, 2020 at 14:02
  • @albert thank you for this hint, but html5 is not XML. For example <br> is valid, I don't want to write <br /> :-). And thank you for the link-checker, but in my case I just want to check html5 syntax.
    – guettli
    Commented Dec 22, 2020 at 15:11
  • HTML5 and XML have a "same base", with xmllint you can also specify a dtd (see files xhtml-lat1.ent xhtml-special.ent xhtml-symbol.ent xhtml1-strict.dtd xhtml1-transitional.dtd), o you could probably do some more checking.
    – albert
    Commented Dec 22, 2020 at 15:42
  • XML is a standard markup language that defines other markup languages like HTML.
    – convert
    Commented Jan 17, 2022 at 13:50
  • @convert html is no longer xml. For example <br> is fine. You don't need to write <br /> any more. I am so happy that html is no longer xml.
    – guettli
    Commented Jan 17, 2022 at 14:43

2 Answers 2

1

There is a Python library html5validator

html5validator is a command line tool that tests files for HTML5 validity. This was written with static site generators like Jekyll and Pelican in mind. Dynamic html content (for example from JS template engines) can be crawled (e.g. with localcrawl) and then validated.

MIT License

1

xmllint has a --html command line option : --html : use the HTML parser .

On Linux , xmllint is part of the libxml2-utils package.

3
  • This seams to fail for some html5 features: stackoverflow.com/questions/54717447/…
    – guettli
    Commented Dec 23, 2020 at 10:03
  • I speculate that the guy who asked that question has sent it his HTML through a pipe, calling tidy before xmllint . I think tidying up large <SVG> elements can indeed be problematic` but that's more a problem of tidy. -- What HTML5 features do you have in mind?Problems with some specific tags? SVG? css? javascript?
    – knb
    Commented Dec 23, 2020 at 10:44
  • A problem is that xmllint complains about non-HTML tags, which are allowed in HTML 5. It still processes and outputs them, so this just an annoyance. Commented Jun 3, 2022 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.