Skip to content

Commit

Permalink
change rule: jshint, csslint
Browse files Browse the repository at this point in the history
add test: default rule
  • Loading branch information
yaniswang committed Mar 30, 2013
1 parent b9251a5 commit d6b5007
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 35 deletions.
4 changes: 3 additions & 1 deletion jshint.json → .jshintrc
Expand Up @@ -23,6 +23,8 @@
"process",
"describe",
"it",
"console"
"console",
"JSHINT",
"CSSLint"
]
}
20 changes: 17 additions & 3 deletions CHANGE.md
Expand Up @@ -5,8 +5,8 @@ HTMLHint change log

add:

1. add csslint rule
2. add jshint rule
1. add rule: csslint
2. add rule: jshint

fix:

Expand All @@ -19,4 +19,18 @@ fix:

## ver 0.9.1 (2013-3-23)

first version
add:

1. add rule: attr-lowercase
2. add rule: attr-value-double-quotes
3. add rule: attr-value-not-empty
4. add rule: doctype-first
5. add rule: doctype-html5
6. add rule: head-script-disabled
7. add rule: id-class-value
8. add rule: img-alt-require
9. add rule: spec-char-escape
10. add rule: style-disabled
11. add rule: tagname-lowercase
12. add rule: tag-pair
13. add rule: tag-self-close
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -21,7 +21,7 @@ module.exports = function(grunt) {
all: {
src: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
jshintrc: "jshint.json"
jshintrc: ".jshintrc"
}
}
},
Expand Down
34 changes: 34 additions & 0 deletions bin/htmlhint.js
@@ -0,0 +1,34 @@
#!/usr/bin/env node

var program = require('commander');

function map(val) {
var objMap = {};
val.split(',').forEach(function(item){
var arrItem = item.split(/\s*=\s*/);
objMap[arrItem[0]] = arrItem[1]?arrItem[1]:true;
});
return objMap;
}

program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' htmlhint -l');
console.log(' htmlhint -r tag-pair,id-class-value=underline test.html');
console.log(' htmlhint -c .htmlhintrc test.html');
console.log('');
});

program
.version('0.9.2')
.usage('[options] <file ...>')
.option('-l, --list', 'show all of the rules available.')
.option('-c, --config <file>', 'custom configuration file.')
.option('-r, --rules <ruleid, ruleid=value ...>', 'set all of the rules available.', map)
.parse(process.argv);

console.log(' list: %j', program.list);
console.log(' config: %j', program.config);
console.log(' rules: %j', program.rules);
console.log(' args: %j', program.args);
2 changes: 1 addition & 1 deletion coverage.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions package.json
Expand Up @@ -3,7 +3,11 @@
"version": "0.9.2",
"description": "A Static Code Analysis Tool for HTML",
"main": "./index",
"dependencies": {},
"dependencies": {
"commander": "~1.1.1",
"jshint": "~1.1.0",
"csslint": "~0.9.10"
},
"devDependencies": {
"grunt-cli": "~0.1.6",
"grunt": "~0.4.1",
Expand All @@ -17,9 +21,10 @@
"grunt-exec": "~0.4.0",
"mocha": "~1.8.2",
"expect.js": "~0.2.0",
"jscover": "~0.2.4",
"jshint": "~1.1.0",
"csslint": "~0.9.10"
"jscover": "~0.2.4"
},
"bin": {
"htmlhint": "./bin/htmlhint"
},
"repository": {
"type": "git",
Expand Down
14 changes: 10 additions & 4 deletions src/rules/csslint.js
Expand Up @@ -10,13 +10,19 @@ HTMLHint.addRule({
parser.addListener('cdata', function(event){
if(event.tagName.toLowerCase() === 'style'){

var cssVerify = options.verify,
cssOptions = options.options;
var cssVerify;

if(cssVerify !== undefined && cssOptions !== undefined){
if(typeof exports === 'object' && require){
cssVerify = require("csslint").CSSLint.verify;
}
else{
cssVerify = CSSLint.verify;
}

if(options !== undefined){
var styleLine = event.line - 1,
styleCol = event.col - 1;
var messages = cssVerify(event.raw, cssOptions).messages;
var messages = cssVerify(event.raw, options).messages;
messages.forEach(function(error){
var line = error.line;
reporter[error.type==='warning'?'warn':'error'](error.message, styleLine + line, (line === 1 ? styleCol : 0) + error.col, self, error.evidence);
Expand Down
14 changes: 10 additions & 4 deletions src/rules/jshint.js
Expand Up @@ -10,14 +10,20 @@ HTMLHint.addRule({
parser.addListener('cdata', function(event){
if(event.tagName.toLowerCase() === 'script'){

var jsVerify = options.verify,
jsOptions = options.options;
var jsVerify;

if(jsVerify !== undefined && jsOptions !== undefined){
if(typeof exports === 'object' && require){
jsVerify = require('jshint').JSHINT;
}
else{
jsVerify = JSHINT;
}

if(options !== undefined){
var styleLine = event.line - 1,
styleCol = event.col - 1;
var code = event.raw.replace(/\t/g,' ');
var status = jsVerify(code, jsOptions);
var status = jsVerify(code, options);
if(status === false){
jsVerify.errors.forEach(function(error){
var line = error.line;
Expand Down
4 changes: 2 additions & 2 deletions test/htmlparser.spec.js
Expand Up @@ -327,15 +327,15 @@ describe('HTMLParser: Object parse', function(){
expect(arrEvents[2]).to.event('cdata',
{
tagName: 'script',
raw: 'alert(1);\r\nalert(2);'
raw: 'alert(1);\r\nalert("</html>");'
});
expect(arrEvents[3]).to.event('tagend',
{
tagName: 'script'
});
done();
});
parser.parse('<script type="text/javascript">alert(1);\r\nalert(2);</script>');
parser.parse('<script type="text/javascript">alert(1);\r\nalert("</html>");</script>');
});


Expand Down
9 changes: 2 additions & 7 deletions test/rules/csslint.js
Expand Up @@ -5,20 +5,15 @@

var expect = require("expect.js");

var CSSLint = require("csslint").CSSLint;

var HTMLHint = require("../../index").HTMLHint;

describe('Rules: csslint', function(){

it('should result in an error', function(){
var code = 'a<style> \r\n body{color:red1;\r\ndisplay:inline;height:100px;}</style>b';
var messages = HTMLHint.verify(code, {'csslint': {
verify: CSSLint.verify,
options:{
"display-property-grouping": true,
"known-properties": true
}
"display-property-grouping": true,
"known-properties": true
}});
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('csslint');
Expand Down
18 changes: 18 additions & 0 deletions test/rules/default.spec.js
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/

var expect = require("expect.js");

var HTMLHint = require("../../index").HTMLHint;

describe('Rules: default', function(){

it('should result 3 errors', function(){
var code = '<p TEST="abc">';
var messages = HTMLHint.verify(code);
expect(messages.length).to.be(3);
});

});
24 changes: 24 additions & 0 deletions test/rules/id-class-value.spec.js
Expand Up @@ -65,4 +65,28 @@ describe('Rules: id-class-value', function(){
expect(messages.length).to.be(0);
});

it('Id and class value be not meet regexp should result in an error', function(){
var code = '<div id="aa-bb" class="ccc-ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': {
'regId': /^_[a-z\d]+(-[a-z\d]+)*$/,
'message': 'Id and class value must meet regexp'
}});
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('id-class-value');
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(5);
expect(messages[1].rule.id).to.be('id-class-value');
expect(messages[1].line).to.be(1);
expect(messages[1].col).to.be(16);
});

it('Id and class value be meet regexp should not result in an error', function(){
var code = '<div id="_aaa-bb" class="_ccc-ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': {
'regId': /^_[a-z\d]+(-[a-z\d]+)*$/,
'message': 'Id and class value must meet regexp'
}});
expect(messages.length).to.be(0);
});

});
9 changes: 2 additions & 7 deletions test/rules/jshint.js
Expand Up @@ -5,20 +5,15 @@

var expect = require("expect.js");

var JSHINT = require('jshint').JSHINT;

var HTMLHint = require("../../index").HTMLHint;

describe('Rules: jshint', function(){

it('should result in an error', function(){
var code = 'a<script>\r\nvar b;\r\n debugger;\r\na=1</script>b';
var messages = HTMLHint.verify(code, {'jshint': {
verify: JSHINT,
options: {
"undef": true,
"unused": true
}
"undef": true,
"unused": true
}});
expect(messages.length).to.be(4);
expect(messages[0].rule.id).to.be('jshint');
Expand Down

0 comments on commit d6b5007

Please sign in to comment.