Permalink
Browse files

Update to latest vnu.jar, make use of json output

This uses the updated vnu.jar from https://github.com/validator/validator.github.io This version supports JSON output, which the grunt task now makes use of.

Fixes #18
  • Loading branch information...
1 parent c7c8e11 commit 7653e64be251f83e280d84cdc08a3b7f9ebfb364 @jzaefferer committed Feb 21, 2014
Showing with 42 additions and 18 deletions.
  1. +7 −3 lib/htmllint.js
  2. +3 −0 package.json
  3. +9 −11 tasks/html.js
  4. +23 −4 test/html_test.js
  5. BIN vnu.jar
View
10 lib/htmllint.js
@@ -1,16 +1,20 @@
module.exports = function(grunt, files, done) {
+ var path = require('path');
var jar = __dirname + '/../vnu.jar';
grunt.util.spawn({
cmd: 'java',
- args: ['-Dnu.validator.client.quiet=yes', '-jar', jar].concat(files)
+ args: ['-jar', jar, '--format', 'json'].concat(files)
}, function(error, output) {
if (error) {
done(error);
return;
}
var result = [];
- if (output.stdout) {
- result = output.stdout.split('\n');
+ if (output.stderr) {
+ result = JSON.parse(output.stderr).messages;
+ result.forEach(function(message) {
+ message.file = path.relative('.', message.url.replace('file:', ''));
+ });
}
done(null, result);
});
View
3 package.json
@@ -27,6 +27,9 @@
"scripts": {
"test": "grunt nodeunit"
},
+ "dependencies": {
+ "chalk": "0.4.0"
+ },
"devDependencies": {
"grunt": "~0.4.0",
"grunt-contrib-jshint": "~0.6.4",
View
20 tasks/html.js
@@ -11,6 +11,8 @@ var htmllint = require('../lib/htmllint');
module.exports = function(grunt) {
"use strict";
+ var chalk = require('chalk');
+
grunt.registerMultiTask('htmllint', 'Validate html files', function() {
var done = this.async(),
files = grunt.file.expand(this.filesSrc);
@@ -26,17 +28,13 @@ module.exports = function(grunt) {
done();
return;
} else {
- var parts = [], loc = [];
- for (var i = 0, l = result.length; i < l; i++) {
- parts = result[i].split(':'); // 0=file, 1=line, 2=error, 3=msg
-
- if (parts.length < 4) {
- parts.splice(1, 0, '--.--.--');
- }
- loc = parts[1].split('.');
- grunt.log.writeln('Linting ' + parts[0].replace('"', '') + '...' + (parts[2].toUpperCase()).red);
- grunt.log.writeln('['.red + ('L' + loc[0]).yellow + ':'.red + ('C' + loc[2]).yellow + ']'.red + (parts[3]).yellow);
- }
+ result.forEach(function(message) {
+ var output = chalk.cyan(message.file) + ' ';
+ output += chalk.red('[') + chalk.yellow('L' + message.lastLine) +
+ chalk.red(':') + chalk.yellow('C' + message.lastColumn) + chalk.red('] ');
+ output += message.message;
+ grunt.log.writeln(output);
+ });
}
done(false);
});
View
27 test/html_test.js
@@ -10,11 +10,30 @@ exports['htmllint'] = {
if (error) {
throw error;
}
+ // copy only the properties we want to test
+ // url property is absolute, system-dependend path
+ result = result.map(function(message) {
+ return {
+ file: message.file,
+ message: message.message,
+ lastLine: message.lastLine,
+ lastColumn: message.lastColumn
+ };
+ });
test.deepEqual(result, [
- '"test/invalid.html":10.1-10.81: error: An "img" element must have an "alt" attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images.',
- '"test/invalid.html":12.1-12.19: error: The "clear" attribute on the "br" element is obsolete. Use CSS instead.',
- '"test/invalid.html": info warning: The character encoding of the document was not declared.'
- ], 'three errors from test/invalid.html');
+ {
+ lastLine: 10,
+ lastColumn: 81,
+ message: 'An “img” element must have an “alt” attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images.',
+ file: 'test/invalid.html'
+ },
+ {
+ lastLine: 12,
+ lastColumn: 19,
+ message: 'The “clear” attribute on the “br” element is obsolete. Use CSS instead.',
+ file: 'test/invalid.html'
+ }
+ ], 'two errors from test/invalid.html');
test.done();
});
}
View
BIN vnu.jar
Binary file not shown.

0 comments on commit 7653e64

Please sign in to comment.