What is HTML_CodeSniffer?
By default, HTML_CodeSniffer comes with standards that cover the three conformance levels of the W3C Web Content Accessibility Guidelines (WCAG) 2.0, and the U.S. Section 508 legislation. It also provides tools to write your own standards, which can be useful in situations where you wish to enforce consistency across a web site.
HTML_CodeSniffer can be called in multiple ways:
- It also comes with a pop-up auditor interface, accessible via a bookmarklet, letting you browse through messages emitted from one of the defined standards. Where possible, the auditor also points you to the HTML element causing the problem.
- It can also be run on the command line with the assistance of a headless browser app.
- Using npm:
$ npm i -g npm $ npm i --save html_codesniffer
HTML_CodeSniffer is released under a BSD-style licence. For more information, please see the file "licence.txt".
Using the source code
Building the auditor
The HTML_CodeSniffer auditor can be built using node.js and the Grunt tasker (http://gruntjs.com/). It has been tested with the latest version of node.js (at time of writing: version 6.0) and Grunt, but should also work with recent earlier versions.
- Install node.js with your package manager of choice.
- You may need to update the Node.js package manager (npm) itself:
npm update -g npm
- Install the Grunt CLI helper if you haven't already done so:
npm install -g grunt-cli
- Get node.js to install the dependencies Grunt needs:
- Run Grunt to build the auditor:
You should see two new directories:
node_modules (containing the node.js
build (containing your auditor). You can then move
(or symlink as appropriate) your
build directory to a web-accessible
If you are developing using HTML_CodeSniffer and require the code not minified for
debugging purposes, follow the above steps, but run
(instead of just build). This will combine the files as normal, but not minify them.
Note: These examples assume a built version of HTMLCS exported to
You will need PhantomJS installed if you wish to use the contributed command-line script. PhantomJS provides a headless Webkit-based browser to run the scripts in, so it should provide results that are similar to recent (or slightly less than recent) versions of Safari.
Contrib/PhantomJS/HTMLCS_Run.js file for more information.
Node & JSDom
HTML_CodeSniffer requires a dom to run, however, it is possible to run it entirely server side without a headless browser using Node on arbitrary fragments of HTML using an environment wrapper like JSDom.
An example node script:
var jsdom = ;var fs = ;var vConsole = jsdom;// Forward messages to the console.vConsolejsdom;
HTML_CodeSniffer supports very basic string translations. The auditor will use the current language of the document it is being run in (e.g.
<html lang="en">). A language code can be supplied if you need to tell HTML_CodeSniffer which language you want to use.
Note: HTML_CodeSniffer only has english (default) and polish language.
If other language support is required a custom version can be built by adding more translations in
Translations/<code>.js and using the grunt build process described above.
Contributing and reporting issues
To report any issues with using HTML_CodeSniffer, please use the HTML_CodeSniffer Issue Tracker.
Contributions to the HTML_CodeSniffer code base are also welcome: please create a fork of the main repository, then submit your modified code through a Pull Request for review. A Pull Request also automatically creates an issue in the Issue Tracker, so if you have code to contribute, you do not need to do both.
More information on HTML_CodeSniffer can be found on its GitHub site, http://squizlabs.github.io/HTML_CodeSniffer/. This site provides:
- Information on the tests performed (and messages emitted) by HTML_CodeSniffer's standards, organised by conformance level and Success Criterion;
- A source test area that allows you to try out HTML_CodeSniffer with your own HTML source code; and
- A link to the HTML_CodeSniffer bookmarklet, letting you check other pages using the pop-up auditor interface.