Skip to content

Commit

Permalink
Added validation options
Browse files Browse the repository at this point in the history
  • Loading branch information
stopyoukid committed Jul 26, 2013
1 parent c26e87a commit 848428d
Showing 1 changed file with 84 additions and 21 deletions.
105 changes: 84 additions & 21 deletions tasks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,90 @@

var htmllint = require('../lib/htmllint');

module.exports = function(grunt) {
"use strict";

grunt.registerMultiTask('htmllint', 'Validate html files', function() {
var done = this.async(),
files = grunt.file.expand(this.filesSrc);

htmllint(grunt, files, function(error, result) {
if (error) {
grunt.log.error(error);
done(false);
return;
}
if (!result.length) {
grunt.log.writeln(files.length + ' file(s) valid');
done();
return;
}
grunt.log.writeln(result.join('\n'));
done(false);
module.exports = function (grunt) {
"use strict";

var defaultLintSettings = {
customAttributes: true, // Validate custom attributes
selfClosingTags: true, // Validate self closing tags,
documentEncoding: true, // Validates for whether or not the document has a character encoding declared,
requiredChildren: true, // Validates that elements have the required children,
startTagBeforeDocType: true, // Validates that start tags are not before doctypes,
strayEndTag: true, // Validates for stray end tags,
forLabelControl: true, // Validates that the for attribute on the label points to a control
imgAlt: true, // Validates that img tags have an alt attribute,
invalidValue: true, // Validates for invalid values for attributes
unclosedElement: true, // Validates for unclosed elements
invalidChildElements: true, // Validates that elements have valid child elements
openElements: true // Validates that there are open elements before a parents close tag
};

var filters = {
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/,
imgAlt: /An "img" element must have an "alt" 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/
};

function extend (target, source) {
target = target || {};
for (var prop in source) {
if (typeof source[prop] === 'object') {
target[prop] = extend(target[prop], source[prop]);
} else {
target[prop] = source[prop];
}
}
return target;
}

function addResults(filter, sourceResults, destResults) {
for (var i = 0; i < sourceResults.length; i++) {
if (sourceResults[i].match(filter)) {
destResults.push(sourceResults[i]);
}
}
}

grunt.registerMultiTask('htmllint', 'Validate html files', function () {
var done = this.async(),
files = grunt.file.expand(this.filesSrc),
options = this.options();

htmllint(grunt, files, function (error, result) {
if (error) {
grunt.log.error(error);
done(false);
return;
}

var lintOptions = extend(extend({}, defaultLintSettings), options);
var actualResult = [];
var filter;

for (var lintOption in lintOptions) {
filter = filters[lintOption];
if (lintOptions.hasOwnProperty(lintOption) && lintOptions[lintOption] && filter) {
addResults(filter, result, actualResult);
}
}

if (!actualResult.length) {
grunt.log.writeln(files.length + ' file(s) valid');
done();
return;
}
grunt.log.writeln(actualResult.join('\n'));
done(false);
});
});
});

};

0 comments on commit 848428d

Please sign in to comment.