Skip to content

Commit

Permalink
add rule: src-not-empty-string
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Sep 27, 2013
1 parent a9bd8fc commit 766c433
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 113 deletions.
4 changes: 2 additions & 2 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
HTMLHint change log
====================

## ver 0.9.4 (2013-9-16)

## ver 0.9.4 (2013-9-27)

1. add rule: src-not-empty-string

## ver 0.9.3 (2013-5-24)

Expand Down
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.

26 changes: 26 additions & 0 deletions src/rules/src-not-empty-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2013, Yanis Wang <[email protected]>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'src-not-empty-string',
description: 'Src of img(script,link) must set value.',
init: function(parser, reporter){
var self = this;
parser.addListener('tagstart', function(event){
var tagName = event.tagName,
attrs = event.attrs,
attr,
col = event.col + tagName.length + 1;
for(var i=0, l=attrs.length;i<l;i++){
attr = attrs[i];
if(((/^(img|script|embed|bgsound|iframe)$/.test(tagName) === true && attr.name === 'src') ||
(tagName === 'link' && attr.name === 'href') ||
(tagName === 'object' && attr.name === 'data')) &&
attr.value === ''){
reporter.error('[ '+attr.name + '] of [ '+tagName+' ] must set value.', event.line, col + attr.index, self, attr.raw);
}
}
});
}
});
13 changes: 9 additions & 4 deletions test/rules/attr-lowercase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ var expect = require("expect.js");

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

describe('Rules: attr-lowercase', function(){
var ruldId = 'attr-lowercase',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Not all lowercase attr should result in an error', function(){
var code = '<p TEST="abc">';
var messages = HTMLHint.verify(code, {'attr-lowercase': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('attr-lowercase');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(3);
});

it('Lowercase attr should not result in an error', function(){
var code = '<p test="abc">';
var messages = HTMLHint.verify(code, {'attr-lowercase': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
17 changes: 11 additions & 6 deletions test/rules/attr-value-double-quotes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@ var expect = require("expect.js");

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

describe('Rules: attr-value-double-quotes', function(){
var ruldId = 'attr-value-double-quotes',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Attribute value closed by single quotes should result in an error', function(){
var code = '<a href=\'abc\' title=abc>';
var messages = HTMLHint.verify(code, {'attr-value-double-quotes': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('attr-value-double-quotes');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(3);
expect(messages[1].rule.id).to.be('attr-value-double-quotes');
expect(messages[1].rule.id).to.be(ruldId);
expect(messages[1].line).to.be(1);
expect(messages[1].col).to.be(14);
});

it('Attribute value no closed should not result in an error', function(){
var code = '<input type="button" disabled>';
var messages = HTMLHint.verify(code, {'attr-value-double-quotes': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Lowercase attr should not result in an error', function(){
var code = '<a href="abc">';
var messages = HTMLHint.verify(code, {'attr-value-double-quotes': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
15 changes: 10 additions & 5 deletions test/rules/attr-value-not-empty.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ var expect = require("expect.js");

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

describe('Rules: attr-value-not-empty', function(){
var ruldId = 'attr-value-not-empty',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Attribute value have no value should result in an error', function(){
var code = '<input disabled>';
var messages = HTMLHint.verify(code, {'attr-value-not-empty': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('attr-value-not-empty');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(7);
expect(messages[0].type).to.be('warning');
});

it('Attribute value closed by quote but no value should not result in an error', function(){
var code = '<input disabled="">';
var messages = HTMLHint.verify(code, {'attr-value-not-empty': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Attribute value closed by quote and have value should not result in an error', function(){
var code = '<input disabled="disabled">';
var messages = HTMLHint.verify(code, {'attr-value-not-empty': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
19 changes: 12 additions & 7 deletions test/rules/csslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ var expect = require("expect.js");

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

describe('Rules: csslint', function(){
var ruldId = 'csslint',
ruleOptions = {};

ruleOptions[ruldId] = {
"display-property-grouping": true,
"known-properties": true
};

describe('Rules: '+ruldId, 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': {
"display-property-grouping": true,
"known-properties": true
}});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('csslint');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(2);
expect(messages[0].col).to.be(7);
expect(messages[0].type).to.be('warning');
expect(messages[1].rule.id).to.be('csslint');
expect(messages[1].rule.id).to.be(ruldId);
expect(messages[1].line).to.be(3);
expect(messages[1].col).to.be(16);
expect(messages[1].type).to.be('warning');
Expand Down
13 changes: 9 additions & 4 deletions test/rules/doctype-first.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ var expect = require("expect.js");

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

describe('Rules: doctype-first', function(){
var ruldId = 'doctype-first',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Doctype not be first should result in an error', function(){
var code = '<html></html>';
var messages = HTMLHint.verify(code, {'doctype-first': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('doctype-first');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(1);
});

it('Doctype be first should not result in an error', function(){
var code = '<!DOCTYPE HTML><html>';
var messages = HTMLHint.verify(code, {'doctype-first': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
13 changes: 9 additions & 4 deletions test/rules/doctype-html5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ var expect = require("expect.js");

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

describe('Rules: doctype-html5', function(){
var ruldId = 'doctype-html5',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Doctype not html5 should result in an error', function(){
var code = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html></html>';
var messages = HTMLHint.verify(code, {'doctype-html5': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('doctype-html5');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(1);
expect(messages[0].type).to.be('warning');
});

it('Doctype html5 should not result in an error', function(){
var code = '<!DOCTYPE HTML><html>';
var messages = HTMLHint.verify(code, {'doctype-html5': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
17 changes: 11 additions & 6 deletions test/rules/head-script-disabled.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,35 @@ var expect = require("expect.js");

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

describe('Rules: head-script-disabled', function(){
var ruldId = 'head-script-disabled',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('External script in head should result in an error', function(){
var code = '<head><script src="test.js"></script></head>';
var messages = HTMLHint.verify(code, {'head-script-disabled': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('head-script-disabled');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(7);
expect(messages[0].type).to.be('warning');
});

it('Internal Script in head should result in an error', function(){
var code = '<head><script>alert(1);</script></head>';
var messages = HTMLHint.verify(code, {'head-script-disabled': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('head-script-disabled');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(7);
});

it('Script in body not result in an error', function(){
var code = '<head></head><body><script src="test.js"></script></body>';
var messages = HTMLHint.verify(code, {'head-script-disabled': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
33 changes: 19 additions & 14 deletions test/rules/id-class-value.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,22 @@ var expect = require("expect.js");

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

describe('Rules: id-class-value', function(){
var ruldId = 'id-class-value',
ruleOptionsUnderline = {}, ruleOptionsDash = {}, ruleOptionsHump = {}, ruleOptionsReg = {};

ruleOptionsUnderline[ruldId] = 'underline';
ruleOptionsDash[ruldId] = 'dash';
ruleOptionsHump[ruldId] = 'hump';
ruleOptionsReg[ruldId] = {
'regId': /^_[a-z\d]+(-[a-z\d]+)*$/,
'message': 'Id and class value must meet regexp'
};

describe('Rules: '+ruldId, function(){

it('Id and class value be not lower case and split by underline should result in an error', function(){
var code = '<div id="aaaBBB" class="ccc-ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': 'underline'});
var messages = HTMLHint.verify(code, ruleOptionsUnderline);
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('id-class-value');
expect(messages[0].line).to.be(1);
Expand All @@ -25,7 +36,7 @@ describe('Rules: id-class-value', function(){

it('Id and class value be lower case and split by underline should not result in an error', function(){
var code = '<div id="aaa_bbb" class="ccc_ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': 'underline'});
var messages = HTMLHint.verify(code, ruleOptionsUnderline);
expect(messages.length).to.be(0);
});

Expand All @@ -43,13 +54,13 @@ describe('Rules: id-class-value', function(){

it('Id and class value be lower case and split by dash should not result in an error', function(){
var code = '<div id="aaa-bbb" class="ccc-ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': 'dash'});
var messages = HTMLHint.verify(code, ruleOptionsDash);
expect(messages.length).to.be(0);
});

it('Id and class value be not meet hump style should result in an error', function(){
var code = '<div id="aaa_bb" class="ccc-ddd">';
var messages = HTMLHint.verify(code, {'id-class-value': 'hump'});
var messages = HTMLHint.verify(code, ruleOptionsHump);
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('id-class-value');
expect(messages[0].line).to.be(1);
Expand All @@ -61,16 +72,13 @@ describe('Rules: id-class-value', function(){

it('Id and class value be meet hump style should not result in an error', function(){
var code = '<div id="aaaBbb" class="cccDdd">';
var messages = HTMLHint.verify(code, {'id-class-value': 'hump'});
var messages = HTMLHint.verify(code, ruleOptionsHump);
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'
}});
var messages = HTMLHint.verify(code, ruleOptionsReg);
expect(messages.length).to.be(2);
expect(messages[0].rule.id).to.be('id-class-value');
expect(messages[0].line).to.be(1);
Expand All @@ -82,10 +90,7 @@ describe('Rules: id-class-value', function(){

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'
}});
var messages = HTMLHint.verify(code, ruleOptionsReg);
expect(messages.length).to.be(0);
});

Expand Down
13 changes: 9 additions & 4 deletions test/rules/id-unique.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ var expect = require("expect.js");

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

describe('Rules: id-unique', function(){
var ruldId = 'id-unique',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: '+ruldId, function(){

it('Id redefine should result in an error', function(){
var code = '<div id="test"></div><div id="test"></div>';
var messages = HTMLHint.verify(code, {'id-unique': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('id-unique');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(26);
expect(messages[0].type).to.be('error');
});

it('Id no redefine should not result in an error', function(){
var code = '<div id="test1"></div><div id="test2"></div>';
var messages = HTMLHint.verify(code, {'id-unique': true});
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

Expand Down
Loading

0 comments on commit 766c433

Please sign in to comment.