django-html-validator

20 October 2014   0 comments   Python, Web development, Django

https://github.com/peterbe/django-html-validator

Powered by Fusion×

In action
A couple of weeks ago we had accidentally broken our production server (for a particular report) because of broken HTML. It was an unclosed tag which rendered everything after that tag to just plain white. Our comprehensive test suite failed to notice it because it didn't look at details like that. And when it was tested manually we simply missed the conditional situation when it was caused. Neither good excuses. So it got me thinking how can we incorporate HTML (html5 in particular) validation into our test suite.

So I wrote a little gist and used it a bit on a couple of projects and was quite pleased with the results. But I thought this might be something worthwhile to keep around for future projects or for other people who can't just copy-n-paste a gist.

With that in mind I put together a little package with a README and a setup.py and now you can use it too.

There are however some caveats. Especially if you intend to run it as part of your test suite.

Caveat number 1

You can't flood htmlvalidator.nu. Well, you can I guess. It would be really evil of you and kittens will die. If you have a test suite that does things like response = self.client.get(reverse('myapp:myview')) and there are many tests you might be causing an obscene amount of HTTP traffic to them. Which brings us on to...

Caveat number 2

The htmlvalidator.nu site is written in Java and it's open source. You can basically download their validator and point django-html-validator to it locally. Basically the way it works is java -jar vnu.jar myfile.html. However, it's slow. Like really slow. It takes about 2 seconds to run just one modest HTML file. So, you need to be patient.

Comments

Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
Premailer on Python 3 08 October 2014
Next:
localForage vs. XHR 22 October 2014
Related:
Difference between $.data('foo') and $.attr('data-foo') in jQuery 10 June 2012
Going real simple on HTML5 audio 14 October 2011
EmailInput HTML5 friendly for Django 02 August 2011
A script tag's type in HTML5 10 May 2011
RequireJS versus HeadJS 09 January 2011
Comparing jsmin and slimmer 17 September 2009
Interesting float/int casting in Python 25 April 2006
Robots.txt Validator 24 January 2004