Skip to content

Commit

Permalink
add rule: id-class-ad-disabled
Browse files Browse the repository at this point in the history
add inline ruleset support
  • Loading branch information
yaniswang committed Jun 18, 2014
1 parent 80dc0e7 commit 143d89f
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
HTMLHint change log
====================

## ver 0.9.6 (2014-6-14)
## ver 0.9.6 (2014-6-18)

1. add rule: attr-no-duplication
2. add rule: space-tab-mixed-disabled
2. add default rule: attr-no-duplication
3. add rule: id-class-ad-disabled
4. add default rule: attr-no-duplication
5. add inline ruleset support

## ver 0.9.4 (2013-9-27)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ HTMLHint is released under the MIT license:

> The MIT License
>
> Copyright (c) 2013 Yanis Wang \< [email protected] \>
> Copyright (c) 2014 Yanis Wang \< [email protected] \>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ TODO

1. add rule: Relative path
2. add rule: Absolute path
3. add rule: adblock
4. add comment support: `<!-- htmlhint doctype-first:false-->`
4. reporter support
5. w3c rule
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.

33 changes: 28 additions & 5 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,46 @@ var HTMLHint = (function (undefined) {
'spec-char-escape': true,
'id-unique': true,
'src-not-empty': true,
'attr-no-duplication': true // added: 2014-6-14
'attr-no-duplication': true
};

HTMLHint.addRule = function(rule){
HTMLHint.rules[rule.id] = rule;
};

HTMLHint.verify = function(html, ruleset){
HTMLHint.verify = function(html, newRuleset){
var id;
var ruleset;
// parse inline ruleset
html = html.replace(/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->\s*/i, function(all, strRuleset){
ruleset = {};
strRuleset.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g, function(all, key, value){
if(value === 'false'){
value = false;
}
else if(value === 'true'){
value = true;
}
ruleset[key] = value;
});
return '';
});
if(newRuleset !== undefined){
ruleset = ruleset || {};
for (id in newRuleset){
ruleset[id] = newRuleset[id];
}
}
if(ruleset === undefined){
ruleset = HTMLHint.defaultRuleset;
}
var parser = new HTMLParser(),
reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset);

var parser = new HTMLParser();
var reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset);

var rules = HTMLHint.rules,
rule;
for (var id in ruleset){
for (id in ruleset){
rule = rules[id];
if (rule !== undefined && ruleset[id] !== false){
rule.init(parser, reporter, ruleset[id]);
Expand Down
27 changes: 27 additions & 0 deletions src/rules/id-class-ad-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'id-class-ad-disabled',
description: 'Id and class can not use ad keyword, it will blocked by adblock software.',
init: function(parser, reporter){
var self = this;
parser.addListener('tagstart', function(event){
var attrs = event.attrs;
var attr;
var attrName;
var col = event.col + event.tagName.length + 1;

for(var i=0, l=attrs.length;i<l;i++){
attr = attrs[i];
attrName = attr.name;
if(/^(id|class)$/i.test(attrName)){
if(/(^|[-\_])ad([-\_]|$)/i.test(attr.value)){
reporter.warn('The value of '+attrName+' can not use ad keyword.', event.line, col + attr.index, self, attr.raw);
}
}
}
});
}
});
33 changes: 33 additions & 0 deletions test/core-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* MIT Licensed
*/

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

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

describe('Core', function(){

it('Default ruleset not worked should result in an error', function(){
var code = '<P ATTR=\'1\' id="a">><div id="a"><img src="" a="1" a="2"/></div>';
var messages = HTMLHint.verify(code);
expect(messages.length).to.be(9);
});

it('Inline ruleset not worked should result in an error', function(){
var code = '<!-- htmlhint img-alt-require:true-->\r\n<img src="test.gif" />';
var messages = HTMLHint.verify(code);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be('img-alt-require');
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(1);

// overwrite inline ruleset
messages = HTMLHint.verify(code, {
'img-alt-require': false
});
expect(messages.length).to.be(0);
});

});
134 changes: 134 additions & 0 deletions test/rules/id-class-ad-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* MIT Licensed
*/

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

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

var ruldId = 'id-class-ad-disabled',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Id use ad keyword should result in an error', function(){
var code = '<div id="ad">test</div>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].type).to.be('warning');
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(5);

code = '<div id="ad-222">test</div>';
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);

code = '<div id="ad_222">test</div>';
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);

code = '<div id="111-ad">test</div>';
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);

code = '<div id="111_ad">test</div>';
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);

code = '<div id="111-ad-222">test</div>';
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);

code = '<div id="111_ad_222">test</div>';
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);
});

it('Class use ad keyword should result in an error', function(){
var code = '<div class="ad">test</div>';
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);

code = '<div class="ad-222">test</div>';
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);

code = '<div class="ad_222">test</div>';
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);

code = '<div class="111-ad">test</div>';
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);

code = '<div class="111_ad">test</div>';
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);

code = '<div class="111-ad-222">test</div>';
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);

code = '<div class="111_ad_222">test</div>';
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);
});

it('Id and class no ad keyword used should not result in an error', function(){
var code = '<div id="ad1" class="ad2">test</div>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);

code = '<div id="ad1-222" class="ad2-222">test</div>';
messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);

code = '<div id="111-ad1" class="111-ad2">test</div>';
messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

});

0 comments on commit 143d89f

Please sign in to comment.