Skip to content

Commit

Permalink
Add support alt of img and remove previous rule
Browse files Browse the repository at this point in the history
  • Loading branch information
takenspc committed Jul 29, 2014
1 parent dfb166f commit 2efc8e1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 73 deletions.
14 changes: 7 additions & 7 deletions src/rules/alt-not-empty.js → src/rules/alt-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* MIT Licensed
*/
HTMLHint.addRule({
id: 'alt-not-empty',
description: 'Alt of area[href] and input[type=image] must set value.',
id: 'alt-require',
description: 'Alt of img must be present and alt of area[href] and input[type=image] must be set value.',
init: function(parser, reporter){
var self = this;
parser.addListener('tagstart', function(event){
var tagName = event.tagName.toLowerCase(),
mapAttrs = parser.getMapAttrs(event.attrs),
col = event.col + tagName.length + 1,
selector;
if (tagName !== 'area' && tagName !== 'input'){
return;
if(tagName === 'img' && !('alt' in mapAttrs)){
reporter.warn('Alt of img tag must be present.', event.line, col, self, event.raw);
}
if ((tagName === 'area' && 'href' in mapAttrs) ||
(tagName === 'input' && mapAttrs['type'] === 'image')) {
if (!('alt' in mapAttrs) || mapAttrs['alt'] === '') {
else if((tagName === 'area' && 'href' in mapAttrs) ||
(tagName === 'input' && mapAttrs['type'] === 'image')){
if(!('alt' in mapAttrs) || mapAttrs['alt'] === ''){
selector = tagName === 'area' ? 'area[href]' : 'input[type=image]';
reporter.warn('Alt of ' + selector + ' must be set value.', event.line, col, self, event.raw);
}
Expand Down
26 changes: 0 additions & 26 deletions src/rules/img-alt-require.js

This file was deleted.

24 changes: 23 additions & 1 deletion test/rules/alt-not-empty.js → test/rules/alt-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,35 @@ var expect = require("expect.js");

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

var ruldId = 'alt-not-empty',
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>';
Expand Down
39 changes: 0 additions & 39 deletions test/rules/img-alt-require.spec.js

This file was deleted.

0 comments on commit 2efc8e1

Please sign in to comment.