Skip to main content

Yo! Check your HTML!

Project description

django-html-validator
=====================

A tool to do validation of your HTML generated from your Django app.

License: [MPL 2](http://www.mozilla.org/MPL/2.0/)


Warning!
--------

If you don't download a local `vnu.har` file, it will use
`http://html5.validator.nu` and send your HTML there over HTTP.
Not over HTTPS.

If you use `htmlvalidator` to validate tests it's unlikely your HTML contains
anything sensitive or personally identifyable but if you use the middleware
option there's a potential risk.

Settings
--------

Independent of how you use `htmlvalidator` you need to switch it on.
It's not on by default. The setting to do that is:

```python
HTMLVALIDATOR_ENABLED = True
```

What this does, is that it prints all validation errors to `stdout`.
But it doesn't stop the execution from running. Even if there are errors.

To make it so that the execution stops as soon as there is any validation
error switch this on in your settings:

```python
HTMLVALIDATOR_FAILFAST = True
```

Now, if there's any validation error going through the client you'll
get a `htmlvalidator.exceptions.ValidationError` exception raised.

When validation errors and warnings are encountered, `htmlvalidator` will
dump the HTML to a file and the errors in a file with the same name except
with the extension `.txt` instead. It will dump this into, by default, the
systems tmp directory and in sub-directory called `htmlvalidator`.
E.g. `/tmp/htmlvalidator/`. If you want to override that change:

```python
HTMLVALIDATOR_DUMPDIR = '~/validationerrors/'
```
Whatever you set, the directory doesn't need to exist but its parent does.

By default when `htmlvalidator` encounters validation errors it stores
the relevant HTML file in the `HTMLVALIDATOR_DUMPDIR` together with a file
with the extension `.txt` in the same directory. Alternatively you can just let
it dump the validation errors and warnings straight onto stdout with:

```python
HTMLVALIDATOR_OUTPUT = 'stdout' # default is 'file'
```



Setting the vnu.jar path
------------------------

By default, all validation is done by sending your HTML with HTTP POST to
[html5.validator.nu](http://html5.validator.nu/).

Not only does this put a lot of stress on their server. Especially if you have
a lot of tests. It's also slow because it depends on network latency. A much
better way is to download the `vnu.jar` file from their
[latest release](https://github.com/validator/validator.github.io/releases/latest) on
[GitHub page](https://github.com/validator/).

You set it up simply like this:

```python
HTMLVALIDATOR_VNU_JAR = '~/downloads/vnu.jar'
```

This also **requires java to be installed** because that's how `.jar` files are
executed on the command line.

Be aware that calling this `vnu.jar` file is quite slow. Over 2 seconds is
not unusual.


Validating during running the server
------------------------------------

A way to do HTML validation is to do it during running the
server. E.g. with `./manage.py runserver`.

To do that you need to enable the middleware. In your settings module,
in the `MIDDLEWARE_CLASSES` add
`htmlvalidator.middleware.HTMLValidator` so it looks something like
this:

```python
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'htmlvalidator.middleware.HTMLValidator', # <-- note!
)
```

Note! See the note above about `HTMLVALIDATOR_ENABLED` to actually
make it do something.
Also, if you enable `HTMLVALIDATOR_FAILFAST`, when running the
`htmlvalidator` middleware it will raise an exception as soon as it
sees some invalid HTML.


Validating HTML in tests
------------------------

Suppose you have a class that does tests. By default it already has a
`self.client` which you use to make requests. All you need to do is to
replace it with the `htmlvalidator.client.ValidatingClient`
class. For example:

```python

from django.test import TestCase
from htmlvalidator.client import ValidatingClient


class MyAppTests(TestCase):

def setUp(self):
super(MyAppTests, self).setUp()
self.client = ValidatingClient()

def test_homepage(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-html-validator-0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_html_validator-0.1-py2-none-any.whl (9.5 kB view details)

Uploaded Python 2

File details

Details for the file django-html-validator-0.1.tar.gz.

File metadata

File hashes

Hashes for django-html-validator-0.1.tar.gz
Algorithm Hash digest
SHA256 a69c6be81ac2a3bae9e5fd6b78fc61f7d615ea028410aac9397ba5513c66d2a4
MD5 a281a87a3883b87d442b06180fb82926
BLAKE2b-256 820fb207b19ecf8232214de800c10a09e80bf5ec46643c42e93810bc76217584

See more details on using hashes here.

File details

Details for the file django_html_validator-0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for django_html_validator-0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 48ec28f220c70926be81cb2c96e64b9c8a4c55c31f5217303ea4ea006a6867b1
MD5 c22852a608ced3d353fd07e0c495781d
BLAKE2b-256 a693fb7aedbc024b6581e8e56ff59ba4abce767fe625bb24bf6c1e5bb5da515e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page