Skip to content

Commit

Permalink
Add alt of area[href] and input[type=image] rule
Browse files Browse the repository at this point in the history
  • Loading branch information
takenspc committed Jul 28, 2014
1 parent f55b21d commit e25f187
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/rules/alt-not-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* Copyright (c) 2014, Takeshi Kurosawa <taken.spc@gmail.com>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'alt-not-empty',
description: 'Alt of input[type=image], area[href] must set value.',
init: function(parser, reporter){
var self = this;
parser.addListener('tagstart', function(event){
var tagName = event.tagName.toLowerCase(),
attrs = event.attrs,
attrMap = {},
attr,
col = event.col + tagName.length + 1,
selector;
if (tagName !== 'area' && tagName !== 'input'){
return;
}
for(var i=0, l=attrs.length;i<l;i++){
attr = attrs[i];
attrMap[attr.name.toLowerCase()] = attr.value;
}
if ((tagName === 'area' && 'href' in attrMap) ||
(tagName === 'input' && attrMap['type'] === 'image')) {
selector = tagName === 'area' ? 'area[href]' : 'input[type=image]';
if (!('alt' in attrMap) || attrMap['alt'] === '') {
reporter.warn('Alt of ' + tagName + ' must be set value.', event.line, col, self, event.raw);
}
}
});
}
});
94 changes: 94 additions & 0 deletions test/rules/alt-not-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* Copyright (c) 2014, Takeshi Kurosawa <taken.spc@gmail.com>
* MIT Licensed
*/

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

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

var ruldId = 'alt-not-empty',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

/* 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 attribute 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 attribute 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);
});
});

0 comments on commit e25f187

Please # to comment.