Grunt plugin for html validation, using Mike Smith's vnu.jar.
Install this grunt plugin next to your project's Gruntfile.js gruntfile with: npm install grunt-html --save-dev
Then add this line to your project's Gruntfile.js
:
grunt.loadNpmTasks('grunt-html');
Then specify what files to validate in your config:
grunt.initConfig({
htmllint: {
all: ["demos/**/*.html", "tests/**/*.html"]
}
});
For fast validation, keep that in a single group, as the validator initialization takes a few seconds.
The validation engine outputs many different type of validation errors, however, only a subset is supported:
customAttributes - /Attribute "[^"]*" not allowed/
selfClosingTags - /Self-closing syntax ("/>") used on a non-void HTML element/
documentEncoding - /The character encoding of the document was not declared/
requiredChildren - /Element "[^"]" is missing a required instance of child element "[^"]"/
startTagBeforeDocType - /Start tag seen without seeing a doctype first/
strayEndTag - /Stray end tag "[^"]*"/
forLabelControl - /The "for" attribute of the "label" element must refer to a form control/
requiredAttribute - /An "[^"]" element must have an "[^"]" attribute/
invalidValue - /Bad value "[^"]" for attribute "[^"]" on element "[^"]*"/
unclosedElement - /Unclosed element "[^"]*"/
invalidChildElements - /Element "[^"]" not allowed as child of element "[^"]" in this context/
openElements - /End tag "[^"]*" seen, but there were open elements/
obsoleteAttribute - /The "[^"]" attribute on the "[^"]" element is obsolete/
obsoleteElement - /The "[^"]*" element is obsolete/
rcdataString - /RCDATA element "[^"]*" contained the string/
endTagInvalidNesting - /End tag "[^"]*" violates nesting rules/
sawExpecting - /Saw "[^"]*" when expecting an attribute name/,
selfClosingTagNotSelfClosed - /A slash was not immediately followed by "[^"]*"/
Using lint options requires the use of an alternate syntax in the htmllint config.
grunt.initConfig({
htmllint: {
all: {
options: {
customAttributes: false, // Disables the validation for customAttributes
documentEncoding: false, // Disables the validation for documentEncoding
startTagBeforeDocType: false, // Disables the validation for startTagBeforeDocType
validationFilters: { // Custom filters which return whether or not the error is truly a validation issue
requiredChildren: function (url, error) {
return url.indexOf(".tpl.html") < 0; // Errors about required children on files that end with tpl.html should be ignored.
},
invalidValue: function (url, error) {
return error.message.indexOf("{{") < 0; // Errors about invalid attribute values that contain {{ should be ignored.
},
forLabelControl: function (url, error) {
return error.extract.indexOf("{{") < 0;
},
unknownValidation: function (url, error) { // A catch all handler for all unknown validation errors, which allow for you to run your own validation code.
return false; // Ignore all unknown errors
}
}
},
files: {
src: [
'**/*.html',
'!vendor/**/*.html'
]
}
}
}
});
Copyright (c) 2012 Jörn Zaefferer Licensed under the MIT license.