-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add inline ruleset support
- Loading branch information
Showing
9 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | ||
* 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); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | ||
* 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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/** | ||
* Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | ||
* 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); | ||
}); | ||
|
||
}); |