Skip to content

Commit

Permalink
remove encoding detect
Browse files Browse the repository at this point in the history
attr-unsafe-chars(rule): show unsafe code in message
  • Loading branch information
yaniswang committed Oct 9, 2015
1 parent 54a5193 commit 6aa6011
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .jshintrc-browser
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"exports",
"console",
"JSHINT",
"CSSLint"
"CSSLint",
"escape"
]
}
2 changes: 1 addition & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ HTMLHint change log

add:

1. support detect file encoding in cli
1. attr-unsafe-chars(rule): show unsafe code in message

fix:

Expand Down
7 changes: 1 addition & 6 deletions bin/htmlhint
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var fs = require('fs');
var path = require('path');
var stripJsonComments = require('strip-json-comments');
var async = require('async');
var jschardet = require("jschardet");
var iconv = require('iconv-lite');

var HTMLHint = require("../index").HTMLHint;
var pkg = require('../package.json');
Expand Down Expand Up @@ -274,10 +272,7 @@ function walkPath(dir, callback, onFinish) {

// hint file
function hintFile(filepath, ruleset){
var content = fs.readFileSync(filepath);
// detect encoding
var encoding = jschardet.detect(content).encoding || 'utf-8';
content = iconv.decode(content, encoding);
var content = fs.readFileSync(filepath, 'utf-8');
return HTMLHint.verify(content, ruleset);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"colors": "1.0.3",
"commander": "2.6.0",
"csslint": "0.10.0",
"iconv-lite": "^0.4.13",
"jschardet": "1.3.0",
"jshint": "2.8.0",
"strip-json-comments": "1.0.4"
},
Expand Down
11 changes: 7 additions & 4 deletions src/rules/attr-unsafe-chars.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ HTMLHint.addRule({
var attrs = event.attrs,
attr,
col = event.col + event.tagName.length + 1;
// exclude \x0a(\r), \x0d(\n)
var regUnsafe = /[\u0000-\u0009\u000b\u000c\u000e-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
// exclude \x09(\t), \x0a(\r), \x0d(\n)
var regUnsafe = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
var match;
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+' ] cannot contain an unsafe char.', event.line, col + attr.index, self, attr.raw);
match = attr.value.match(regUnsafe);
if(match !== null){
var unsafeCode = escape(match[0]).replace(/%u/, '\\u').replace(/%/, '\\x');
reporter.warn('The value of attribute [ '+attr.name+' ] cannot contain an unsafe char [ ' + unsafeCode + ' ].', event.line, col + attr.index, self, attr.raw);
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions test/rules/attr-unsafe-chars.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe('Rules: '+ruldId, function(){
expect(messages.length).to.be(0);
});

it('Attribute value have \\r\\n should not result in an error', function(){
var code = '<link rel="icon" type="image/x-icon" href="data:image/x-icon;base64,R0lGODlhEAAQAKEAAAAAAICAgP///wAAACH/\nC05FVFNDQVBFMi4wAwEAAAAh/hFDcmVhdGVkIHdpdGggR0lNUAAh+QQBZAADACwAAAAAEAAQAAACJIyPacLtvp5kEUwYmL00i81VXK\neNgjiioQdeqsuakXl6tIIjBQAh+QQBZAADACwAAAAAEAAQAAACIIyPacLtvp5kcb5qG85iZ2+BkyiRV8BBaEqtrKkqslEAADs="/>';
it('Attribute value have \\r\\n\\t should not result in an error', function(){
var code = '<link rel="icon" type="image/x-icon" href="data:image/x-icon;base64,R0lGODlhEAAQAKEAAAAAAICAgP///wAAACH/\nC05FVFNDQVBFMi4wAwEAAAAh/hFDcmVhdGVkIHdpdGggR0lNUAAh+QQBZAADACwAAAAAEAAQAAACJIyPacLtvp5kEUwYmL00i81VXK\neNgjiioQdeqsuakXl6tIIjBQAh+QQBZAADACwAAAAAEAAQAAACIIyPacLtvp5kcb5qG85iZ2+BkyiRV8BBaEqtrKkqslEAADs=\t"/>';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
Expand Down

0 comments on commit 6aa6011

Please sign in to comment.