Skip to content

Commit

Permalink
add rule: attr-unsafe-chars
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Jun 22, 2014
1 parent 05167ed commit f55b21d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Expand Up @@ -15,6 +15,7 @@
"browser": true,
"wsh": true,
"-W099": true,
"-W100": true,

"predef": [
"HTMLHint",
Expand Down
9 changes: 5 additions & 4 deletions CHANGE.md
Expand Up @@ -9,10 +9,11 @@ add:
2. add rule: space-tab-mixed-disabled
3. add rule: id-class-ad-disabled
4. add rule: href-abs-or-rel
5. add default rule: attr-no-duplication
6. add inline ruleset support
7. add test spec: Set false to rule
8. add point: load default ruleset when use empty ruleset
5. add rule: attr-unsafe-chars
6. add default rule: attr-no-duplication
7. add inline ruleset support
8. add test spec: Set false to rule
9. add point: load default ruleset when use empty ruleset

## ver 0.9.4 (2013-9-27)

Expand Down
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.

23 changes: 23 additions & 0 deletions src/rules/attr-unsafe-chars.js
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'attr-unsafe-chars',
description: 'Attribute value cant not use unsafe chars.',
init: function(parser, reporter){
var self = this;
parser.addListener('tagstart', function(event){
var attrs = event.attrs,
attr,
col = event.col + event.tagName.length + 1;
var regUnsafe = /[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
for(var i=0, l=attrs.length;i<l;i++){
attr = attrs[i];
if(regUnsafe.test(attr.value) === true){
reporter.warn('The value of attribute [ '+attr.name+' ] cant not use unsafe chars.', event.line, col + attr.index, self, attr.raw);
}
}
});
}
});
33 changes: 33 additions & 0 deletions test/rules/attr-unsafe-chars.js
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/

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

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

var ruldId = 'attr-unsafe-chars',
ruleOptions = {};

ruleOptions[ruldId] = true;

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

it('Attribute value have unsafe chars should result in an error', function(){
var code = '<a href="https://vimeo.com/album/1951235/video/56931059‎">Sud Web 2012</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(3);
expect(messages[0].type).to.be('warning');
});

it('Attribute value have no unsafe chars should not result in an error', function(){
var code = '<input disabled="disabled" />';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

});

0 comments on commit f55b21d

Please sign in to comment.