Skip to content

Commit

Permalink
add rule: title-require, <title> must be present in <head> tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Oct 7, 2015
1 parent 96abbe2 commit 2247853
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 494 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add:
5. tag-pair(rule): Show the line of the start tag
6. space-tab-mixed-disabled(rule): Support space and tab mode, for check only space or tab
7. Make cli logs more clear to people
8. add rule: title-require, `<title>` must be present in `<head>` tag.

fix:

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

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var HTMLHint = (function (undefined) {
'spec-char-escape': true,
'id-unique': true,
'src-not-empty': true,
'attr-no-duplication': true
'attr-no-duplication': true,
'title-require': true
};

HTMLHint.addRule = function(rule){
Expand Down
28 changes: 28 additions & 0 deletions src/rules/title-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015, Yanis Wang <[email protected]>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'title-require',
description: '<title> must be present in <head> tag.',
init: function(parser, reporter){
var self = this;
var hasTitle = false;
function onTagStart(event){
if(event.tagName.toLowerCase() === 'title'){
hasTitle = true;
}
}
function onTagEnd(event){
if(event.tagName.toLowerCase() === 'head'){
if(hasTitle === false){
reporter.error('<title> must be present in <head> tag.', event.line, event.col, self, event.raw);
}
parser.removeListener('tagstart', onTagStart);
parser.removeListener('tagstart', onTagEnd);
}
}
parser.addListener('tagstart', onTagStart);
parser.addListener('tagend', onTagEnd);
}
});
232 changes: 116 additions & 116 deletions test/rules/alt-require.js → test/rules/alt-require.spec.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,116 @@
/**
* Copyright (c) 2015, Yanis Wang <[email protected]>
* Copyright (c) 2014, Takeshi Kurosawa <[email protected]>
* MIT Licensed
*/

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

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

var ruldId = 'alt-require',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Img tag have empty alt attribute should not result in an error', function(){
var code = '<img width="200" height="300" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Img tag have non empty alt attribute should not result in an error', function(){
var code = '<img width="200" height="300" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Img tag have not alt attribute should result in an error', function(){
var code = '<img width="200" height="300">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(5);
expect(messages[0].type).to.be('warning');
});

/* A tag can have shape and coords attributes and not have alt attribute */
it('A tag have not alt attribute should not result in an error', function(){
var code = '<a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Area tag have not href and alt attributes should not result in an error', function(){
var code = '<area>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Area[href] tag have not alt attribute should result in an error', function(){
var code = '<area href="#test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(6);
expect(messages[0].type).to.be('warning');
});

it('Area[href] tag have empty alt attribute should result in an error', function(){
var code = '<area href="#test" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(6);
expect(messages[0].type).to.be('warning');
});

it('Area[href] tag have non emtpy alt attribute should not result in an error', function(){
var code = '<area href="#test" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input tag have not type and alt attributes should not result in an error', function(){
var code = '<input>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input[type="text"] tag have not alt attribute should not result in an error', function(){
var code = '<input type="text">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input[type="image"] tag have not alt attribute should result in an error', function(){
var code = '<input type="image">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
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('Input[type="image"] tag have empty alt attribute should result in an error', function(){
var code = '<input type="image" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
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('Input[type="image"] tag have non emtpy alt attribute should not result in an error', function(){
var code = '<input type="image" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
});
/**
* Copyright (c) 2015, Yanis Wang <[email protected]>
* Copyright (c) 2014, Takeshi Kurosawa <[email protected]>
* MIT Licensed
*/

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

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

var ruldId = 'alt-require',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Img tag have empty alt attribute should not result in an error', function(){
var code = '<img width="200" height="300" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Img tag have non empty alt attribute should not result in an error', function(){
var code = '<img width="200" height="300" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Img tag have not alt attribute should result in an error', function(){
var code = '<img width="200" height="300">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(5);
expect(messages[0].type).to.be('warning');
});

/* A tag can have shape and coords attributes and not have alt attribute */
it('A tag have not alt attribute should not result in an error', function(){
var code = '<a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Area tag have not href and alt attributes should not result in an error', function(){
var code = '<area>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Area[href] tag have not alt attribute should result in an error', function(){
var code = '<area href="#test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(6);
expect(messages[0].type).to.be('warning');
});

it('Area[href] tag have empty alt attribute should result in an error', function(){
var code = '<area href="#test" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(6);
expect(messages[0].type).to.be('warning');
});

it('Area[href] tag have non emtpy alt attribute should not result in an error', function(){
var code = '<area href="#test" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input tag have not type and alt attributes should not result in an error', function(){
var code = '<input>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input[type="text"] tag have not alt attribute should not result in an error', function(){
var code = '<input type="text">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Input[type="image"] tag have not alt attribute should result in an error', function(){
var code = '<input type="image">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
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('Input[type="image"] tag have empty alt attribute should result in an error', function(){
var code = '<input type="image" alt="">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
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('Input[type="image"] tag have non emtpy alt attribute should not result in an error', function(){
var code = '<input type="image" alt="test">';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* MIT Licensed
*/

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

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

var ruldId = 'attr-no-duplication',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Attribute name been duplication should result in an error', function(){
var code = '<a href="a" href="b">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(12);
});

it('Attribute name not been duplication should not result in an error', function(){
var code = '<a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* MIT Licensed
*/

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

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

var ruldId = 'attr-no-duplication',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Attribute name been duplication should result in an error', function(){
var code = '<a href="a" href="b">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(12);
});

it('Attribute name not been duplication should not result in an error', function(){
var code = '<a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

});
Loading

0 comments on commit 2247853

Please sign in to comment.