diff --git a/.jshintrc b/.jshintrc index 52a05cb90..ab0809f31 100644 --- a/.jshintrc +++ b/.jshintrc @@ -15,6 +15,7 @@ "browser": true, "wsh": true, "-W099": true, + "-W100": true, "predef": [ "HTMLHint", diff --git a/CHANGE.md b/CHANGE.md index f379ae8ae..0d8f0e2a6 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -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) diff --git a/coverage.html b/coverage.html index 352d8b0e1..2b0273d64 100644 --- a/coverage.html +++ b/coverage.html @@ -338,4 +338,4 @@ code .string { color: #5890AD } code .keyword { color: #8A6343 } code .number { color: #2F6FAD } -

Coverage

98%
407
401
6

htmlhint.js

98%
407
401
6
LineHitsSource
1/**
2 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
3 * MIT Licensed
4 */
51var HTMLHint = (function (undefined) {
6
71 var HTMLHint = {};
8
91 HTMLHint.version = '@VERSION';
10
111 HTMLHint.rules = {};
12
13 //默认配置
141 HTMLHint.defaultRuleset = {
15 'tagname-lowercase': true,
16 'attr-lowercase': true,
17 'attr-value-double-quotes': true,
18 'doctype-first': true,
19 'tag-pair': true,
20 'spec-char-escape': true,
21 'id-unique': true,
22 'src-not-empty': true,
23 'attr-no-duplication': true
24 };
25
261 HTMLHint.addRule = function(rule){
2721 HTMLHint.rules[rule.id] = rule;
28 };
29
301 HTMLHint.verify = function(html, ruleset){
31 // parse inline ruleset
3282 html = html.replace(/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i, function(all, strRuleset){
332 if(ruleset === undefined){
340 ruleset = {};
35 }
362 strRuleset.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g, function(all, key, value){
372 if(value === 'false'){
381 value = false;
39 }
401 else if(value === 'true'){
411 value = true;
42 }
432 ruleset[key] = value;
44 });
452 return '';
46 });
47
4882 if(ruleset === undefined || Object.keys(ruleset).length ===0){
493 ruleset = HTMLHint.defaultRuleset;
50 }
51
5282 var parser = new HTMLParser();
5382 var reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset);
54
5582 var rules = HTMLHint.rules,
56 rule;
5782 for (var id in ruleset){
58106 rule = rules[id];
59106 if (rule !== undefined && ruleset[id] !== false){
60103 rule.init(parser, reporter, ruleset[id]);
61 }
62 }
63
6482 parser.parse(html);
65
6682 return reporter.messages;
67 };
68
691 return HTMLHint;
70
71})();
72
731if (typeof exports === 'object' && exports){
741 exports.HTMLHint = HTMLHint;
75}
76/**
77 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
78 * MIT Licensed
79 */
801(function(HTMLHint, undefined){
81
821 var Reporter = function(){
8382 var self = this;
8482 self._init.apply(self,arguments);
85 };
86
871 Reporter.prototype = {
88 _init: function(lines, ruleset){
8982 var self = this;
9082 self.lines = lines;
9182 self.ruleset = ruleset;
9282 self.messages = [];
93 },
94 //错误
95 error: function(message, line, col, rule, raw){
9652 this.report('error', message, line, col, rule, raw);
97 },
98 //警告
99 warn: function(message, line, col, rule, raw){
10048 this.report('warning', message, line, col, rule, raw);
101 },
102 //信息
103 info: function(message, line, col, rule, raw){
1040 this.report('info', message, line, col, rule, raw);
105 },
106 //报告
107 report: function(type, message, line, col, rule, raw){
108100 var self = this;
109100 self.messages.push({
110 type: type,
111 message: message,
112 raw: raw,
113 evidence: self.lines[line-1],
114 line: line,
115 col: col,
116 rule: {
117 id: rule.id,
118 description: rule.description,
119 link: 'https://github.com/yaniswang/HTMLHint/wiki/' + rule.id
120 }
121 });
122 }
123 };
124
1251 HTMLHint.Reporter = Reporter;
126
127})(HTMLHint);
128/**
129 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
130 * MIT Licensed
131 */
1321var HTMLParser = (function(undefined){
133
1341 var HTMLParser = function(){
135107 var self = this;
136107 self._init.apply(self,arguments);
137 };
138
1391 HTMLParser.prototype = {
140 _init: function(){
141107 var self = this;
142107 self._listeners = {};
143107 self._mapCdataTags = self.makeMap("script,style");
144107 self._arrBlocks = [];
145 },
146
147 makeMap: function(str){
148115 var obj = {}, items = str.split(",");
149115 for ( var i = 0; i < items.length; i++ ){
150326 obj[ items[i] ] = true;
151 }
152115 return obj;
153 },
154
155 // parse html code
156 parse: function(html){
157
158107 var self = this,
159 mapCdataTags = self._mapCdataTags;
160
161107 var regTag=/<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"']+))?)*?)\s*(\/?))>/g,
162 regAttr = /\s*([\w\-:]+)(?:\s*=\s*(?:(")([^"]*)"|(')([^']*)'|([^\s"']+)))?/g,
163 regLine = /\r?\n/g;
164
165107 var match, matchIndex, lastIndex = 0, tagName, arrAttrs, tagCDATA, attrsCDATA, arrCDATA, lastCDATAIndex = 0, text;
166107 var lastLineIndex = 0, line = 1;
167107 var arrBlocks = self._arrBlocks;
168
169107 self.fire('start', {
170 pos: 0,
171 line: 1,
172 col: 1
173 });
174
175107 while((match = regTag.exec(html))){
176257 matchIndex = match.index;
177257 if(matchIndex > lastIndex){//保存前面的文本或者CDATA
17877 text = html.substring(lastIndex, matchIndex);
17977 if(tagCDATA){
18010 arrCDATA.push(text);
181 }
182 else{//文本
18367 saveBlock('text', text, lastIndex);
184 }
185 }
186257 lastIndex = regTag.lastIndex;
187
188257 if((tagName = match[1])){
18990 if(tagCDATA && tagName === tagCDATA){//结束标签前输出CDATA
19015 text = arrCDATA.join('');
19115 saveBlock('cdata', text, lastCDATAIndex, {
192 'tagName': tagCDATA,
193 'attrs': attrsCDATA
194 });
19515 tagCDATA = null;
19615 attrsCDATA = null;
19715 arrCDATA = null;
198 }
19990 if(!tagCDATA){
200 //标签结束
20189 saveBlock('tagend', match[0], matchIndex, {
202 'tagName': tagName
203 });
20489 continue;
205 }
206 }
207
208168 if(tagCDATA){
2091 arrCDATA.push(match[0]);
210 }
211 else{
212167 if((tagName = match[4])){//标签开始
213154 arrAttrs = [];
214154 var attrs = match[5],
215 attrMatch,
216 attrMatchCount = 0;
217154 while((attrMatch = regAttr.exec(attrs))){
218150 var name = attrMatch[1],
219 quote = attrMatch[2] ? attrMatch[2] :
220 attrMatch[4] ? attrMatch[4] : '',
221 value = attrMatch[3] ? attrMatch[3] :
222 attrMatch[5] ? attrMatch[5] :
223 attrMatch[6] ? attrMatch[6] : '';
224150 arrAttrs.push({'name': name, 'value': value, 'quote': quote, 'index': attrMatch.index, 'raw': attrMatch[0]});
225150 attrMatchCount += attrMatch[0].length;
226 }
227154 if(attrMatchCount === attrs.length){
228154 saveBlock('tagstart', match[0], matchIndex, {
229 'tagName': tagName,
230 'attrs': arrAttrs,
231 'close': match[6]
232 });
233154 if(mapCdataTags[tagName]){
23415 tagCDATA = tagName;
23515 attrsCDATA = arrAttrs.concat();
23615 arrCDATA = [];
23715 lastCDATAIndex = lastIndex;
238 }
239 }
240 else{//如果出现漏匹配,则把当前内容匹配为text
2410 saveBlock('text', match[0], matchIndex);
242 }
243 }
24413 else if(match[2] || match[3]){//注释标签
24513 saveBlock('comment', match[0], matchIndex, {
246 'content': match[2] || match[3],
247 'long': match[2]?true:false
248 });
249 }
250 }
251 }
252
253107 if(html.length > lastIndex){
254 //结尾文本
25513 text = html.substring(lastIndex, html.length);
25613 saveBlock('text', text, lastIndex);
257 }
258
259107 self.fire('end', {
260 pos: lastIndex,
261 line: line,
262 col: lastIndex - lastLineIndex + 1
263 });
264
265 //存储区块
266107 function saveBlock(type, raw, pos, data){
267351 var col = pos - lastLineIndex + 1;
268351 if(data === undefined){
26980 data = {};
270 }
271351 data.raw = raw;
272351 data.pos = pos;
273351 data.line = line;
274351 data.col = col;
275351 arrBlocks.push(data);
276351 self.fire(type, data);
277351 var lineMatch;
278351 while((lineMatch = regLine.exec(raw))){
27922 line ++;
28022 lastLineIndex = pos + regLine.lastIndex;
281 }
282 }
283
284 },
285
286 // add event
287 addListener: function(types, listener){
288145 var _listeners = this._listeners;
289145 var arrTypes = types.split(/[,\s]/), type;
290145 for(var i=0, l = arrTypes.length;i<l;i++){
291150 type = arrTypes[i];
292150 if (_listeners[type] === undefined){
293129 _listeners[type] = [];
294 }
295150 _listeners[type].push(listener);
296 }
297 },
298
299 // fire event
300 fire: function(type, data){
301565 if (data === undefined){
3020 data = {};
303 }
304565 data.type = type;
305565 var self = this,
306 listeners = [],
307 listenersType = self._listeners[type],
308 listenersAll = self._listeners['all'];
309565 if (listenersType !== undefined){
310162 listeners = listeners.concat(listenersType);
311 }
312565 if (listenersAll !== undefined){
313137 listeners = listeners.concat(listenersAll);
314 }
315565 for (var i = 0, l = listeners.length; i < l; i++){
316326 listeners[i].call(self, data);
317 }
318 },
319
320 // remove event
321 removeListener: function(type, listener){
32215 var listenersType = this._listeners[type];
32315 if(listenersType !== undefined){
32413 for (var i = 0, l = listenersType.length; i < l; i++){
32510 if (listenersType[i] === listener){
32610 listenersType.splice(i, 1);
32710 break;
328 }
329 }
330 }
331 },
332
333 //fix pos if event.raw have \n
334 fixPos: function(event, index){
3358 var text = event.raw.substr(0, index);
3368 var arrLines = text.split(/\r?\n/),
337 lineCount = arrLines.length - 1,
338 line = event.line, col;
3398 if(lineCount > 0){
3402 line += lineCount;
3412 col = arrLines[lineCount].length + 1;
342 }
343 else{
3446 col = event.col + index;
345 }
3468 return {
347 line: line,
348 col: col
349 };
350 },
351
352 // covert array type of attrs to map
353 getMapAttrs: function(arrAttrs){
3546 var mapAttrs = {},
355 attr;
3566 for(var i=0,l=arrAttrs.length;i<l;i++){
3576 attr = arrAttrs[i];
3586 mapAttrs[attr.name] = attr.value;
359 }
3606 return mapAttrs;
361 }
362 };
363
3641 return HTMLParser;
365
366})();
367
3681if (typeof exports === 'object' && exports){
3691 exports.HTMLParser = HTMLParser;
370}
371/**
372 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
373 * MIT Licensed
374 */
3751HTMLHint.addRule({
376 id: 'attr-lowercase',
377 description: 'Attribute name must be lowercase.',
378 init: function(parser, reporter){
3795 var self = this;
3805 parser.addListener('tagstart', function(event){
3819 var attrs = event.attrs,
382 attr,
383 col = event.col + event.tagName.length + 1;
3849 for(var i=0, l=attrs.length;i<l;i++){
38515 attr = attrs[i];
38615 var attrName = attr.name;
38715 if(attrName !== attrName.toLowerCase()){
3884 reporter.error('Attribute name [ '+attrName+' ] must be lower case.', event.line, col + attr.index, self, attr.raw);
389 }
390 }
391 });
392 }
393});
394/**
395 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
396 * MIT Licensed
397 */
3981HTMLHint.addRule({
399 id: 'attr-no-duplication',
400 description: 'Attribute name can not been duplication.',
401 init: function(parser, reporter){
4025 var self = this;
4035 parser.addListener('tagstart', function(event){
4049 var attrs = event.attrs;
4059 var attr;
4069 var attrName;
4079 var col = event.col + event.tagName.length + 1;
408
4099 var mapAttrName = {};
4109 for(var i=0, l=attrs.length;i<l;i++){
41116 attr = attrs[i];
41216 attrName = attr.name;
41316 if(mapAttrName[attrName] === true){
4143 reporter.error('The name of attribute [ '+attr.name+' ] been duplication.', event.line, col + attr.index, self, attr.raw);
415 }
41616 mapAttrName[attrName] = true;
417 }
418 });
419 }
420});
421/**
422 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
423 * MIT Licensed
424 */
4251HTMLHint.addRule({
426 id: 'attr-value-double-quotes',
427 description: 'Attribute value must closed by double quotes.',
428 init: function(parser, reporter){
4295 var self = this;
4305 parser.addListener('tagstart', function(event){
4319 var attrs = event.attrs,
432 attr,
433 col = event.col + event.tagName.length + 1;
4349 for(var i=0, l=attrs.length;i<l;i++){
43519 attr = attrs[i];
43619 if((attr.value !== '' && attr.quote !== '"') ||
437 (attr.value === '' && attr.quote === "'")){
4385 reporter.error('The value of attribute [ '+attr.name+' ] must closed by double quotes.', event.line, col + attr.index, self, attr.raw);
439 }
440 }
441 });
442 }
443});
444/**
445 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
446 * MIT Licensed
447 */
4481HTMLHint.addRule({
449 id: 'attr-value-not-empty',
450 description: 'Attribute must set value.',
451 init: function(parser, reporter){
4523 var self = this;
4533 parser.addListener('tagstart', function(event){
4543 var attrs = event.attrs,
455 attr,
456 col = event.col + event.tagName.length + 1;
4573 for(var i=0, l=attrs.length;i<l;i++){
4583 attr = attrs[i];
4593 if(attr.quote === '' && attr.value === ''){
4601 reporter.warn('The attribute [ '+attr.name+' ] must set value.', event.line, col + attr.index, self, attr.raw);
461 }
462 }
463 });
464 }
465});
466/**
467 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
468 * MIT Licensed
469 */
4701HTMLHint.addRule({
471 id: 'csslint',
472 description: 'Scan css with csslint.',
473 init: function(parser, reporter, options){
4741 var self = this;
4751 parser.addListener('cdata', function(event){
4761 if(event.tagName.toLowerCase() === 'style'){
477
4781 var cssVerify;
479
4801 if(typeof exports === 'object' && require){
4811 cssVerify = require("csslint").CSSLint.verify;
482 }
483 else{
4840 cssVerify = CSSLint.verify;
485 }
486
4871 if(options !== undefined){
4881 var styleLine = event.line - 1,
489 styleCol = event.col - 1;
4901 try{
4911 var messages = cssVerify(event.raw, options).messages;
4921 messages.forEach(function(error){
4932 var line = error.line;
4942 reporter[error.type==='warning'?'warn':'error']('['+error.rule.id+'] '+error.message, styleLine + line, (line === 1 ? styleCol : 0) + error.col, self, error.evidence);
495 });
496 }
497 catch(e){}
498 }
499
500 }
501 });
502 }
503});
504/**
505 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
506 * MIT Licensed
507 */
5081HTMLHint.addRule({
509 id: 'doctype-first',
510 description: 'Doctype must be first.',
511 init: function(parser, reporter){
5125 var self = this;
5135 var allEvent = function(event){
51410 if(event.type === 'start' || (event.type === 'text' && /^\s*$/.test(event.raw))){
5155 return;
516 }
5175 if((event.type !== 'comment' && event.long === false) || /^DOCTYPE\s+/i.test(event.content) === false){
5184 reporter.error('Doctype must be first.', event.line, event.col, self, event.raw);
519 }
5205 parser.removeListener('all', allEvent);
521 };
5225 parser.addListener('all', allEvent);
523 }
524});
525/**
526 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
527 * MIT Licensed
528 */
5291HTMLHint.addRule({
530 id: 'doctype-html5',
531 description: 'Doctype must be html5.',
532 init: function(parser, reporter){
5332 var self = this;
5342 function onComment(event){
5359 if(event.long === false && event.content.toLowerCase() !== 'doctype html'){
5361 reporter.warn('Doctype must be html5.', event.line, event.col, self, event.raw);
537 }
538 }
5392 function onTagStart(){
5402 parser.removeListener('comment', onComment);
5412 parser.removeListener('tagstart', onTagStart);
542 }
5432 parser.addListener('all', onComment);
5442 parser.addListener('tagstart', onTagStart);
545 }
546});
547/**
548 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
549 * MIT Licensed
550 */
5511HTMLHint.addRule({
552 id: 'head-script-disabled',
553 description: 'The script tag can not be used in head.',
554 init: function(parser, reporter){
5553 var self = this;
5563 function onTagStart(event){
5575 if(event.tagName.toLowerCase() === 'script'){
5582 reporter.warn('The script tag can not be used in head.', event.line, event.col, self, event.raw);
559 }
560 }
5613 function onTagEnd(event){
5627 if(event.tagName.toLowerCase() === 'head'){
5633 parser.removeListener('tagstart', onTagStart);
5643 parser.removeListener('tagstart', onTagEnd);
565 }
566 }
5673 parser.addListener('tagstart', onTagStart);
5683 parser.addListener('tagend', onTagEnd);
569 }
570});
571/**
572 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
573 * MIT Licensed
574 */
5751HTMLHint.addRule({
576 id: 'href-abs-or-rel',
577 description: 'Href must be absolute or relative.',
578 init: function(parser, reporter, options){
5794 var self = this;
580
5814 var hrefMode = options === 'abs' ? 'absolute' : 'relative';
582
5834 parser.addListener('tagstart', function(event){
58416 var attrs = event.attrs;
58516 var attr;
58616 var col = event.col + event.tagName.length + 1;
587
58816 for(var i=0, l=attrs.length;i<l;i++){
58916 attr = attrs[i];
59016 if(attr.name === 'href'){
59116 if((hrefMode === 'absolute' && /^\w+?:/.test(attr.value) === false) ||
592 (hrefMode === 'relative' && /^https?:\/\//.test(attr.value) === true)){
5934 reporter.warn('The value of href [ '+attr.value+' ] must be '+hrefMode+'.', event.line, col + attr.index, self, attr.raw);
594 }
59516 break;
596 }
597 }
598 });
599 }
600});
601/**
602 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
603 * MIT Licensed
604 */
6051HTMLHint.addRule({
606 id: 'id-class-ad-disabled',
607 description: 'Id and class can not use ad keyword, it will blocked by adblock software.',
608 init: function(parser, reporter){
60917 var self = this;
61017 parser.addListener('tagstart', function(event){
61117 var attrs = event.attrs;
61217 var attr;
61317 var attrName;
61417 var col = event.col + event.tagName.length + 1;
615
61617 for(var i=0, l=attrs.length;i<l;i++){
61720 attr = attrs[i];
61820 attrName = attr.name;
61920 if(/^(id|class)$/i.test(attrName)){
62020 if(/(^|[-\_])ad([-\_]|$)/i.test(attr.value)){
62114 reporter.warn('The value of '+attrName+' can not use ad keyword.', event.line, col + attr.index, self, attr.raw);
622 }
623 }
624 }
625 });
626 }
627});
628/**
629 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
630 * MIT Licensed
631 */
6321HTMLHint.addRule({
633 id: 'id-class-value',
634 description: 'Id and class value must meet some rules.',
635 init: function(parser, reporter, options){
6368 var self = this;
6378 var arrRules = {
638 'underline': {
639 'regId': /^[a-z\d]+(_[a-z\d]+)*$/,
640 'message': 'Id and class value must lower case and split by underline.'
641 },
642 'dash': {
643 'regId': /^[a-z\d]+(-[a-z\d]+)*$/,
644 'message': 'Id and class value must lower case and split by dash.'
645 },
646 'hump': {
647 'regId': /^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,
648 'message': 'Id and class value must meet hump style.'
649 }
650 }, rule;
6518 if(typeof options === 'string'){
6526 rule = arrRules[options];
653 }
654 else{
6552 rule = options;
656 }
6578 if(rule && rule.regId){
6588 var regId = rule.regId,
659 message = rule.message;
6608 parser.addListener('tagstart', function(event){
6618 var attrs = event.attrs,
662 attr,
663 col = event.col + event.tagName.length + 1;
6648 for(var i=0, l1=attrs.length;i<l1;i++){
66516 attr = attrs[i];
66616 if(attr.name.toLowerCase() === 'id'){
6678 if(regId.test(attr.value) === false){
6684 reporter.warn(message, event.line, col + attr.index, self, attr.raw);
669 }
670 }
67116 if(attr.name.toLowerCase() === 'class'){
6728 var arrClass = attr.value.split(/\s+/g), classValue;
6738 for(var j=0, l2=arrClass.length;j<l2;j++){
6748 classValue = arrClass[j];
6758 if(classValue && regId.test(classValue) === false){
6764 reporter.warn(message, event.line, col + attr.index, self, classValue);
677 }
678 }
679 }
680 }
681 });
682 }
683 }
684});
685/**
686 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
687 * MIT Licensed
688 */
6891HTMLHint.addRule({
690 id: 'id-unique',
691 description: 'Id must be unique.',
692 init: function(parser, reporter){
6935 var self = this;
6945 var mapIdCount = {};
6955 parser.addListener('tagstart', function(event){
69611 var attrs = event.attrs,
697 attr,
698 id,
699 col = event.col + event.tagName.length + 1;
70011 for(var i=0, l=attrs.length;i<l;i++){
70117 attr = attrs[i];
70217 if(attr.name.toLowerCase() === 'id'){
7038 id = attr.value;
7048 if(id){
7058 if(mapIdCount[id] === undefined){
7065 mapIdCount[id] = 1;
707 }
708 else{
7093 mapIdCount[id] ++;
710 }
7118 if(mapIdCount[id] > 1){
7123 reporter.error('Id redefinition of [ '+id+' ].', event.line, col + attr.index, self, attr.raw);
713 }
714 }
7158 break;
716 }
717 }
718 });
719 }
720});
721/**
722 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
723 * MIT Licensed
724 */
7251HTMLHint.addRule({
726 id: 'img-alt-require',
727 description: 'Alt of img tag must be set value.',
728 init: function(parser, reporter){
7294 var self = this;
7304 parser.addListener('tagstart', function(event){
7314 if(event.tagName.toLowerCase() === 'img'){
7324 var attrs = event.attrs;
7334 var haveAlt = false;
7344 for(var i=0, l=attrs.length;i<l;i++){
7359 if(attrs[i].name.toLowerCase() === 'alt'){
7362 haveAlt = true;
7372 break;
738 }
739 }
7404 if(haveAlt === false){
7412 reporter.warn('Alt of img tag must be set value.', event.line, event.col, self, event.raw);
742 }
743 }
744 });
745 }
746});
747/**
748 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
749 * MIT Licensed
750 */
7511HTMLHint.addRule({
752 id: 'jshint',
753 description: 'Scan script with jshint.',
754 init: function(parser, reporter, options){
7554 var self = this;
7564 parser.addListener('cdata', function(event){
7574 if(event.tagName.toLowerCase() === 'script'){
758
7594 var mapAttrs = parser.getMapAttrs(event.attrs),
760 type = mapAttrs.type;
761
762 // Only scan internal javascript
7634 if(mapAttrs.src !== undefined || (type && /^(text\/javascript)$/i.test(type) === false)){
7642 return;
765 }
766
7672 var jsVerify;
768
7692 if(typeof exports === 'object' && require){
7702 jsVerify = require('jshint').JSHINT;
771 }
772 else{
7730 jsVerify = JSHINT;
774 }
775
7762 if(options !== undefined){
7772 var styleLine = event.line - 1,
778 styleCol = event.col - 1;
7792 var code = event.raw.replace(/\t/g,' ');
7802 try{
7812 var status = jsVerify(code, options);
7822 if(status === false){
7832 jsVerify.errors.forEach(function(error){
7848 var line = error.line;
7858 reporter.warn(error.reason, styleLine + line, (line === 1 ? styleCol : 0) + error.character, self, error.evidence);
786 });
787 }
788 }
789 catch(e){}
790 }
791
792 }
793 });
794 }
795});
796/**
797 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
798 * MIT Licensed
799 */
8001HTMLHint.addRule({
801 id: 'space-tab-mixed-disabled',
802 description: 'Spaces and tabs can not mixed in front of line.',
803 init: function(parser, reporter){
8046 var self = this;
8056 parser.addListener('text', function(event){
80612 var raw = event.raw;
80712 var reMixed = /(^|\r?\n)( +\t|\t+ )/g;
80812 var match;
80912 while((match = reMixed.exec(raw))){
8103 var fixedPos = parser.fixPos(event, match.index + match[1].length);
8113 reporter.warn('Mixed spaces and tabs in front of line.', fixedPos.line, 1, self, event.raw);
812 }
813 });
814 }
815});
816/**
817 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
818 * MIT Licensed
819 */
8201HTMLHint.addRule({
821 id: 'spec-char-escape',
822 description: 'Special characters must be escaped.',
823 init: function(parser, reporter){
8245 var self = this;
8255 parser.addListener('text', function(event){
8265 var raw = event.raw,
827 reSpecChar = /[<>]/g,
828 match;
8295 while((match = reSpecChar.exec(raw))){
8305 var fixedPos = parser.fixPos(event, match.index);
8315 reporter.error('Special characters must be escaped : [ '+match[0]+' ].', fixedPos.line, fixedPos.col, self, event.raw);
832 }
833 });
834 }
835});
836/**
837 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
838 * MIT Licensed
839 */
8401HTMLHint.addRule({
841 id: 'src-not-empty',
842 description: 'Src of img(script,link) must set value.',
843 init: function(parser, reporter){
8446 var self = this;
8456 parser.addListener('tagstart', function(event){
84635 var tagName = event.tagName,
847 attrs = event.attrs,
848 attr,
849 col = event.col + tagName.length + 1;
85035 for(var i=0, l=attrs.length;i<l;i++){
85142 attr = attrs[i];
85242 if(((/^(img|script|embed|bgsound|iframe)$/.test(tagName) === true && attr.name === 'src') ||
853 (tagName === 'link' && attr.name === 'href') ||
854 (tagName === 'object' && attr.name === 'data')) &&
855 attr.value === ''){
85616 reporter.error('[ '+attr.name + '] of [ '+tagName+' ] must set value.', event.line, col + attr.index, self, attr.raw);
857 }
858 }
859 });
860 }
861});
862/**
863 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
864 * MIT Licensed
865 */
8661HTMLHint.addRule({
867 id: 'style-disabled',
868 description: 'Style tag can not be use.',
869 init: function(parser, reporter){
8702 var self = this;
8712 parser.addListener('tagstart', function(event){
8724 if(event.tagName.toLowerCase() === 'style'){
8731 reporter.warn('Style tag can not be use.', event.line, event.col, self, event.raw);
874 }
875 });
876 }
877});
878/**
879 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
880 * MIT Licensed
881 */
8821HTMLHint.addRule({
883 id: 'tag-pair',
884 description: 'Tag must be paired.',
885 init: function(parser, reporter){
8866 var self = this;
8876 var stack=[],
888 mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01
8896 parser.addListener('tagstart', function(event){
89011 var tagName = event.tagName.toLowerCase();
89111 if (mapEmptyTags[tagName] === undefined && !event.close){
8929 stack.push(tagName);
893 }
894 });
8956 parser.addListener('tagend', function(event){
8965 var tagName = event.tagName.toLowerCase();
897 //向上寻找匹配的开始标签
8985 for(var pos = stack.length-1;pos >= 0; pos--){
8995 if(stack[pos] === tagName){
9004 break;
901 }
902 }
9035 if(pos >= 0){
9044 var arrTags = [];
9054 for(var i=stack.length-1;i>pos;i--){
9061 arrTags.push('</'+stack[i]+'>');
907 }
9084 if(arrTags.length > 0){
9091 reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, event.raw);
910 }
9114 stack.length=pos;
912 }
913 else{
9141 reporter.error('Tag must be paired, No start tag: [ ' + event.raw + ' ]', event.line, event.col, self, event.raw);
915 }
916 });
9176 parser.addListener('end', function(event){
9186 var arrTags = [];
9196 for(var i=stack.length-1;i>=0;i--){
9204 arrTags.push('</'+stack[i]+'>');
921 }
9226 if(arrTags.length > 0){
9234 reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, '');
924 }
925 });
926 }
927});
928/**
929 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
930 * MIT Licensed
931 */
9321HTMLHint.addRule({
933 id: 'tag-self-close',
934 description: 'The empty tag must closed by self.',
935 init: function(parser, reporter){
9362 var self = this;
9372 var mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01
9382 parser.addListener('tagstart', function(event){
9394 var tagName = event.tagName.toLowerCase();
9404 if(mapEmptyTags[tagName] !== undefined){
9414 if(!event.close){
9422 reporter.warn('The empty tag : [ '+tagName+' ] must closed by self.', event.line, event.col, self, event.raw);
943 }
944 }
945 });
946 }
947});
948/**
949 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
950 * MIT Licensed
951 */
9521HTMLHint.addRule({
953 id: 'tagname-lowercase',
954 description: 'Tagname must be lowercase.',
955 init: function(parser, reporter){
9565 var self = this;
9575 parser.addListener('tagstart,tagend', function(event){
95817 var tagName = event.tagName;
95917 if(tagName !== tagName.toLowerCase()){
9606 reporter.error('Tagname [ '+tagName+' ] must be lower case.', event.line, event.col, self, event.raw);
961 }
962 });
963 }
964});
\ No newline at end of file +

Coverage

98%
416
410
6

htmlhint.js

98%
416
410
6
LineHitsSource
1/**
2 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
3 * MIT Licensed
4 */
51var HTMLHint = (function (undefined) {
6
71 var HTMLHint = {};
8
91 HTMLHint.version = '@VERSION';
10
111 HTMLHint.rules = {};
12
13 //默认配置
141 HTMLHint.defaultRuleset = {
15 'tagname-lowercase': true,
16 'attr-lowercase': true,
17 'attr-value-double-quotes': true,
18 'doctype-first': true,
19 'tag-pair': true,
20 'spec-char-escape': true,
21 'id-unique': true,
22 'src-not-empty': true,
23 'attr-no-duplication': true
24 };
25
261 HTMLHint.addRule = function(rule){
2722 HTMLHint.rules[rule.id] = rule;
28 };
29
301 HTMLHint.verify = function(html, ruleset){
31 // parse inline ruleset
3284 html = html.replace(/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i, function(all, strRuleset){
332 if(ruleset === undefined){
340 ruleset = {};
35 }
362 strRuleset.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g, function(all, key, value){
372 if(value === 'false'){
381 value = false;
39 }
401 else if(value === 'true'){
411 value = true;
42 }
432 ruleset[key] = value;
44 });
452 return '';
46 });
47
4884 if(ruleset === undefined || Object.keys(ruleset).length ===0){
493 ruleset = HTMLHint.defaultRuleset;
50 }
51
5284 var parser = new HTMLParser();
5384 var reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset);
54
5584 var rules = HTMLHint.rules,
56 rule;
5784 for (var id in ruleset){
58108 rule = rules[id];
59108 if (rule !== undefined && ruleset[id] !== false){
60105 rule.init(parser, reporter, ruleset[id]);
61 }
62 }
63
6484 parser.parse(html);
65
6684 return reporter.messages;
67 };
68
691 return HTMLHint;
70
71})();
72
731if (typeof exports === 'object' && exports){
741 exports.HTMLHint = HTMLHint;
75}
76/**
77 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
78 * MIT Licensed
79 */
801(function(HTMLHint, undefined){
81
821 var Reporter = function(){
8384 var self = this;
8484 self._init.apply(self,arguments);
85 };
86
871 Reporter.prototype = {
88 _init: function(lines, ruleset){
8984 var self = this;
9084 self.lines = lines;
9184 self.ruleset = ruleset;
9284 self.messages = [];
93 },
94 //错误
95 error: function(message, line, col, rule, raw){
9652 this.report('error', message, line, col, rule, raw);
97 },
98 //警告
99 warn: function(message, line, col, rule, raw){
10049 this.report('warning', message, line, col, rule, raw);
101 },
102 //信息
103 info: function(message, line, col, rule, raw){
1040 this.report('info', message, line, col, rule, raw);
105 },
106 //报告
107 report: function(type, message, line, col, rule, raw){
108101 var self = this;
109101 self.messages.push({
110 type: type,
111 message: message,
112 raw: raw,
113 evidence: self.lines[line-1],
114 line: line,
115 col: col,
116 rule: {
117 id: rule.id,
118 description: rule.description,
119 link: 'https://github.com/yaniswang/HTMLHint/wiki/' + rule.id
120 }
121 });
122 }
123 };
124
1251 HTMLHint.Reporter = Reporter;
126
127})(HTMLHint);
128/**
129 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
130 * MIT Licensed
131 */
1321var HTMLParser = (function(undefined){
133
1341 var HTMLParser = function(){
135109 var self = this;
136109 self._init.apply(self,arguments);
137 };
138
1391 HTMLParser.prototype = {
140 _init: function(){
141109 var self = this;
142109 self._listeners = {};
143109 self._mapCdataTags = self.makeMap("script,style");
144109 self._arrBlocks = [];
145 },
146
147 makeMap: function(str){
148117 var obj = {}, items = str.split(",");
149117 for ( var i = 0; i < items.length; i++ ){
150330 obj[ items[i] ] = true;
151 }
152117 return obj;
153 },
154
155 // parse html code
156 parse: function(html){
157
158109 var self = this,
159 mapCdataTags = self._mapCdataTags;
160
161109 var regTag=/<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"']+))?)*?)\s*(\/?))>/g,
162 regAttr = /\s*([\w\-:]+)(?:\s*=\s*(?:(")([^"]*)"|(')([^']*)'|([^\s"']+)))?/g,
163 regLine = /\r?\n/g;
164
165109 var match, matchIndex, lastIndex = 0, tagName, arrAttrs, tagCDATA, attrsCDATA, arrCDATA, lastCDATAIndex = 0, text;
166109 var lastLineIndex = 0, line = 1;
167109 var arrBlocks = self._arrBlocks;
168
169109 self.fire('start', {
170 pos: 0,
171 line: 1,
172 col: 1
173 });
174
175109 while((match = regTag.exec(html))){
176260 matchIndex = match.index;
177260 if(matchIndex > lastIndex){//保存前面的文本或者CDATA
17878 text = html.substring(lastIndex, matchIndex);
17978 if(tagCDATA){
18010 arrCDATA.push(text);
181 }
182 else{//文本
18368 saveBlock('text', text, lastIndex);
184 }
185 }
186260 lastIndex = regTag.lastIndex;
187
188260 if((tagName = match[1])){
18991 if(tagCDATA && tagName === tagCDATA){//结束标签前输出CDATA
19015 text = arrCDATA.join('');
19115 saveBlock('cdata', text, lastCDATAIndex, {
192 'tagName': tagCDATA,
193 'attrs': attrsCDATA
194 });
19515 tagCDATA = null;
19615 attrsCDATA = null;
19715 arrCDATA = null;
198 }
19991 if(!tagCDATA){
200 //标签结束
20190 saveBlock('tagend', match[0], matchIndex, {
202 'tagName': tagName
203 });
20490 continue;
205 }
206 }
207
208170 if(tagCDATA){
2091 arrCDATA.push(match[0]);
210 }
211 else{
212169 if((tagName = match[4])){//标签开始
213156 arrAttrs = [];
214156 var attrs = match[5],
215 attrMatch,
216 attrMatchCount = 0;
217156 while((attrMatch = regAttr.exec(attrs))){
218152 var name = attrMatch[1],
219 quote = attrMatch[2] ? attrMatch[2] :
220 attrMatch[4] ? attrMatch[4] : '',
221 value = attrMatch[3] ? attrMatch[3] :
222 attrMatch[5] ? attrMatch[5] :
223 attrMatch[6] ? attrMatch[6] : '';
224152 arrAttrs.push({'name': name, 'value': value, 'quote': quote, 'index': attrMatch.index, 'raw': attrMatch[0]});
225152 attrMatchCount += attrMatch[0].length;
226 }
227156 if(attrMatchCount === attrs.length){
228156 saveBlock('tagstart', match[0], matchIndex, {
229 'tagName': tagName,
230 'attrs': arrAttrs,
231 'close': match[6]
232 });
233156 if(mapCdataTags[tagName]){
23415 tagCDATA = tagName;
23515 attrsCDATA = arrAttrs.concat();
23615 arrCDATA = [];
23715 lastCDATAIndex = lastIndex;
238 }
239 }
240 else{//如果出现漏匹配,则把当前内容匹配为text
2410 saveBlock('text', match[0], matchIndex);
242 }
243 }
24413 else if(match[2] || match[3]){//注释标签
24513 saveBlock('comment', match[0], matchIndex, {
246 'content': match[2] || match[3],
247 'long': match[2]?true:false
248 });
249 }
250 }
251 }
252
253109 if(html.length > lastIndex){
254 //结尾文本
25513 text = html.substring(lastIndex, html.length);
25613 saveBlock('text', text, lastIndex);
257 }
258
259109 self.fire('end', {
260 pos: lastIndex,
261 line: line,
262 col: lastIndex - lastLineIndex + 1
263 });
264
265 //存储区块
266109 function saveBlock(type, raw, pos, data){
267355 var col = pos - lastLineIndex + 1;
268355 if(data === undefined){
26981 data = {};
270 }
271355 data.raw = raw;
272355 data.pos = pos;
273355 data.line = line;
274355 data.col = col;
275355 arrBlocks.push(data);
276355 self.fire(type, data);
277355 var lineMatch;
278355 while((lineMatch = regLine.exec(raw))){
27922 line ++;
28022 lastLineIndex = pos + regLine.lastIndex;
281 }
282 }
283
284 },
285
286 // add event
287 addListener: function(types, listener){
288147 var _listeners = this._listeners;
289147 var arrTypes = types.split(/[,\s]/), type;
290147 for(var i=0, l = arrTypes.length;i<l;i++){
291152 type = arrTypes[i];
292152 if (_listeners[type] === undefined){
293131 _listeners[type] = [];
294 }
295152 _listeners[type].push(listener);
296 }
297 },
298
299 // fire event
300 fire: function(type, data){
301573 if (data === undefined){
3020 data = {};
303 }
304573 data.type = type;
305573 var self = this,
306 listeners = [],
307 listenersType = self._listeners[type],
308 listenersAll = self._listeners['all'];
309573 if (listenersType !== undefined){
310164 listeners = listeners.concat(listenersType);
311 }
312573 if (listenersAll !== undefined){
313137 listeners = listeners.concat(listenersAll);
314 }
315573 for (var i = 0, l = listeners.length; i < l; i++){
316328 listeners[i].call(self, data);
317 }
318 },
319
320 // remove event
321 removeListener: function(type, listener){
32215 var listenersType = this._listeners[type];
32315 if(listenersType !== undefined){
32413 for (var i = 0, l = listenersType.length; i < l; i++){
32510 if (listenersType[i] === listener){
32610 listenersType.splice(i, 1);
32710 break;
328 }
329 }
330 }
331 },
332
333 //fix pos if event.raw have \n
334 fixPos: function(event, index){
3358 var text = event.raw.substr(0, index);
3368 var arrLines = text.split(/\r?\n/),
337 lineCount = arrLines.length - 1,
338 line = event.line, col;
3398 if(lineCount > 0){
3402 line += lineCount;
3412 col = arrLines[lineCount].length + 1;
342 }
343 else{
3446 col = event.col + index;
345 }
3468 return {
347 line: line,
348 col: col
349 };
350 },
351
352 // covert array type of attrs to map
353 getMapAttrs: function(arrAttrs){
3546 var mapAttrs = {},
355 attr;
3566 for(var i=0,l=arrAttrs.length;i<l;i++){
3576 attr = arrAttrs[i];
3586 mapAttrs[attr.name] = attr.value;
359 }
3606 return mapAttrs;
361 }
362 };
363
3641 return HTMLParser;
365
366})();
367
3681if (typeof exports === 'object' && exports){
3691 exports.HTMLParser = HTMLParser;
370}
371/**
372 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
373 * MIT Licensed
374 */
3751HTMLHint.addRule({
376 id: 'attr-lowercase',
377 description: 'Attribute name must be lowercase.',
378 init: function(parser, reporter){
3795 var self = this;
3805 parser.addListener('tagstart', function(event){
3819 var attrs = event.attrs,
382 attr,
383 col = event.col + event.tagName.length + 1;
3849 for(var i=0, l=attrs.length;i<l;i++){
38515 attr = attrs[i];
38615 var attrName = attr.name;
38715 if(attrName !== attrName.toLowerCase()){
3884 reporter.error('Attribute name [ '+attrName+' ] must be lower case.', event.line, col + attr.index, self, attr.raw);
389 }
390 }
391 });
392 }
393});
394/**
395 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
396 * MIT Licensed
397 */
3981HTMLHint.addRule({
399 id: 'attr-no-duplication',
400 description: 'Attribute name can not been duplication.',
401 init: function(parser, reporter){
4025 var self = this;
4035 parser.addListener('tagstart', function(event){
4049 var attrs = event.attrs;
4059 var attr;
4069 var attrName;
4079 var col = event.col + event.tagName.length + 1;
408
4099 var mapAttrName = {};
4109 for(var i=0, l=attrs.length;i<l;i++){
41116 attr = attrs[i];
41216 attrName = attr.name;
41316 if(mapAttrName[attrName] === true){
4143 reporter.error('The name of attribute [ '+attr.name+' ] been duplication.', event.line, col + attr.index, self, attr.raw);
415 }
41616 mapAttrName[attrName] = true;
417 }
418 });
419 }
420});
421/**
422 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
423 * MIT Licensed
424 */
4251HTMLHint.addRule({
426 id: 'attr-unsafe-chars',
427 description: 'Attribute value cant not use unsafe chars.',
428 init: function(parser, reporter){
4292 var self = this;
4302 parser.addListener('tagstart', function(event){
4312 var attrs = event.attrs,
432 attr,
433 col = event.col + event.tagName.length + 1;
4342 var regUnsafe = /[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
4352 for(var i=0, l=attrs.length;i<l;i++){
4362 attr = attrs[i];
4372 if(regUnsafe.test(attr.value) === true){
4381 reporter.warn('The value of attribute [ '+attr.name+' ] cant not use unsafe chars.', event.line, col + attr.index, self, attr.raw);
439 }
440 }
441 });
442 }
443});
444/**
445 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
446 * MIT Licensed
447 */
4481HTMLHint.addRule({
449 id: 'attr-value-double-quotes',
450 description: 'Attribute value must closed by double quotes.',
451 init: function(parser, reporter){
4525 var self = this;
4535 parser.addListener('tagstart', function(event){
4549 var attrs = event.attrs,
455 attr,
456 col = event.col + event.tagName.length + 1;
4579 for(var i=0, l=attrs.length;i<l;i++){
45819 attr = attrs[i];
45919 if((attr.value !== '' && attr.quote !== '"') ||
460 (attr.value === '' && attr.quote === "'")){
4615 reporter.error('The value of attribute [ '+attr.name+' ] must closed by double quotes.', event.line, col + attr.index, self, attr.raw);
462 }
463 }
464 });
465 }
466});
467/**
468 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
469 * MIT Licensed
470 */
4711HTMLHint.addRule({
472 id: 'attr-value-not-empty',
473 description: 'Attribute must set value.',
474 init: function(parser, reporter){
4753 var self = this;
4763 parser.addListener('tagstart', function(event){
4773 var attrs = event.attrs,
478 attr,
479 col = event.col + event.tagName.length + 1;
4803 for(var i=0, l=attrs.length;i<l;i++){
4813 attr = attrs[i];
4823 if(attr.quote === '' && attr.value === ''){
4831 reporter.warn('The attribute [ '+attr.name+' ] must set value.', event.line, col + attr.index, self, attr.raw);
484 }
485 }
486 });
487 }
488});
489/**
490 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
491 * MIT Licensed
492 */
4931HTMLHint.addRule({
494 id: 'csslint',
495 description: 'Scan css with csslint.',
496 init: function(parser, reporter, options){
4971 var self = this;
4981 parser.addListener('cdata', function(event){
4991 if(event.tagName.toLowerCase() === 'style'){
500
5011 var cssVerify;
502
5031 if(typeof exports === 'object' && require){
5041 cssVerify = require("csslint").CSSLint.verify;
505 }
506 else{
5070 cssVerify = CSSLint.verify;
508 }
509
5101 if(options !== undefined){
5111 var styleLine = event.line - 1,
512 styleCol = event.col - 1;
5131 try{
5141 var messages = cssVerify(event.raw, options).messages;
5151 messages.forEach(function(error){
5162 var line = error.line;
5172 reporter[error.type==='warning'?'warn':'error']('['+error.rule.id+'] '+error.message, styleLine + line, (line === 1 ? styleCol : 0) + error.col, self, error.evidence);
518 });
519 }
520 catch(e){}
521 }
522
523 }
524 });
525 }
526});
527/**
528 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
529 * MIT Licensed
530 */
5311HTMLHint.addRule({
532 id: 'doctype-first',
533 description: 'Doctype must be first.',
534 init: function(parser, reporter){
5355 var self = this;
5365 var allEvent = function(event){
53710 if(event.type === 'start' || (event.type === 'text' && /^\s*$/.test(event.raw))){
5385 return;
539 }
5405 if((event.type !== 'comment' && event.long === false) || /^DOCTYPE\s+/i.test(event.content) === false){
5414 reporter.error('Doctype must be first.', event.line, event.col, self, event.raw);
542 }
5435 parser.removeListener('all', allEvent);
544 };
5455 parser.addListener('all', allEvent);
546 }
547});
548/**
549 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
550 * MIT Licensed
551 */
5521HTMLHint.addRule({
553 id: 'doctype-html5',
554 description: 'Doctype must be html5.',
555 init: function(parser, reporter){
5562 var self = this;
5572 function onComment(event){
5589 if(event.long === false && event.content.toLowerCase() !== 'doctype html'){
5591 reporter.warn('Doctype must be html5.', event.line, event.col, self, event.raw);
560 }
561 }
5622 function onTagStart(){
5632 parser.removeListener('comment', onComment);
5642 parser.removeListener('tagstart', onTagStart);
565 }
5662 parser.addListener('all', onComment);
5672 parser.addListener('tagstart', onTagStart);
568 }
569});
570/**
571 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
572 * MIT Licensed
573 */
5741HTMLHint.addRule({
575 id: 'head-script-disabled',
576 description: 'The script tag can not be used in head.',
577 init: function(parser, reporter){
5783 var self = this;
5793 function onTagStart(event){
5805 if(event.tagName.toLowerCase() === 'script'){
5812 reporter.warn('The script tag can not be used in head.', event.line, event.col, self, event.raw);
582 }
583 }
5843 function onTagEnd(event){
5857 if(event.tagName.toLowerCase() === 'head'){
5863 parser.removeListener('tagstart', onTagStart);
5873 parser.removeListener('tagstart', onTagEnd);
588 }
589 }
5903 parser.addListener('tagstart', onTagStart);
5913 parser.addListener('tagend', onTagEnd);
592 }
593});
594/**
595 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
596 * MIT Licensed
597 */
5981HTMLHint.addRule({
599 id: 'href-abs-or-rel',
600 description: 'Href must be absolute or relative.',
601 init: function(parser, reporter, options){
6024 var self = this;
603
6044 var hrefMode = options === 'abs' ? 'absolute' : 'relative';
605
6064 parser.addListener('tagstart', function(event){
60716 var attrs = event.attrs;
60816 var attr;
60916 var col = event.col + event.tagName.length + 1;
610
61116 for(var i=0, l=attrs.length;i<l;i++){
61216 attr = attrs[i];
61316 if(attr.name === 'href'){
61416 if((hrefMode === 'absolute' && /^\w+?:/.test(attr.value) === false) ||
615 (hrefMode === 'relative' && /^https?:\/\//.test(attr.value) === true)){
6164 reporter.warn('The value of href [ '+attr.value+' ] must be '+hrefMode+'.', event.line, col + attr.index, self, attr.raw);
617 }
61816 break;
619 }
620 }
621 });
622 }
623});
624/**
625 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
626 * MIT Licensed
627 */
6281HTMLHint.addRule({
629 id: 'id-class-ad-disabled',
630 description: 'Id and class can not use ad keyword, it will blocked by adblock software.',
631 init: function(parser, reporter){
63217 var self = this;
63317 parser.addListener('tagstart', function(event){
63417 var attrs = event.attrs;
63517 var attr;
63617 var attrName;
63717 var col = event.col + event.tagName.length + 1;
638
63917 for(var i=0, l=attrs.length;i<l;i++){
64020 attr = attrs[i];
64120 attrName = attr.name;
64220 if(/^(id|class)$/i.test(attrName)){
64320 if(/(^|[-\_])ad([-\_]|$)/i.test(attr.value)){
64414 reporter.warn('The value of '+attrName+' can not use ad keyword.', event.line, col + attr.index, self, attr.raw);
645 }
646 }
647 }
648 });
649 }
650});
651/**
652 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
653 * MIT Licensed
654 */
6551HTMLHint.addRule({
656 id: 'id-class-value',
657 description: 'Id and class value must meet some rules.',
658 init: function(parser, reporter, options){
6598 var self = this;
6608 var arrRules = {
661 'underline': {
662 'regId': /^[a-z\d]+(_[a-z\d]+)*$/,
663 'message': 'Id and class value must lower case and split by underline.'
664 },
665 'dash': {
666 'regId': /^[a-z\d]+(-[a-z\d]+)*$/,
667 'message': 'Id and class value must lower case and split by dash.'
668 },
669 'hump': {
670 'regId': /^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,
671 'message': 'Id and class value must meet hump style.'
672 }
673 }, rule;
6748 if(typeof options === 'string'){
6756 rule = arrRules[options];
676 }
677 else{
6782 rule = options;
679 }
6808 if(rule && rule.regId){
6818 var regId = rule.regId,
682 message = rule.message;
6838 parser.addListener('tagstart', function(event){
6848 var attrs = event.attrs,
685 attr,
686 col = event.col + event.tagName.length + 1;
6878 for(var i=0, l1=attrs.length;i<l1;i++){
68816 attr = attrs[i];
68916 if(attr.name.toLowerCase() === 'id'){
6908 if(regId.test(attr.value) === false){
6914 reporter.warn(message, event.line, col + attr.index, self, attr.raw);
692 }
693 }
69416 if(attr.name.toLowerCase() === 'class'){
6958 var arrClass = attr.value.split(/\s+/g), classValue;
6968 for(var j=0, l2=arrClass.length;j<l2;j++){
6978 classValue = arrClass[j];
6988 if(classValue && regId.test(classValue) === false){
6994 reporter.warn(message, event.line, col + attr.index, self, classValue);
700 }
701 }
702 }
703 }
704 });
705 }
706 }
707});
708/**
709 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
710 * MIT Licensed
711 */
7121HTMLHint.addRule({
713 id: 'id-unique',
714 description: 'Id must be unique.',
715 init: function(parser, reporter){
7165 var self = this;
7175 var mapIdCount = {};
7185 parser.addListener('tagstart', function(event){
71911 var attrs = event.attrs,
720 attr,
721 id,
722 col = event.col + event.tagName.length + 1;
72311 for(var i=0, l=attrs.length;i<l;i++){
72417 attr = attrs[i];
72517 if(attr.name.toLowerCase() === 'id'){
7268 id = attr.value;
7278 if(id){
7288 if(mapIdCount[id] === undefined){
7295 mapIdCount[id] = 1;
730 }
731 else{
7323 mapIdCount[id] ++;
733 }
7348 if(mapIdCount[id] > 1){
7353 reporter.error('Id redefinition of [ '+id+' ].', event.line, col + attr.index, self, attr.raw);
736 }
737 }
7388 break;
739 }
740 }
741 });
742 }
743});
744/**
745 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
746 * MIT Licensed
747 */
7481HTMLHint.addRule({
749 id: 'img-alt-require',
750 description: 'Alt of img tag must be set value.',
751 init: function(parser, reporter){
7524 var self = this;
7534 parser.addListener('tagstart', function(event){
7544 if(event.tagName.toLowerCase() === 'img'){
7554 var attrs = event.attrs;
7564 var haveAlt = false;
7574 for(var i=0, l=attrs.length;i<l;i++){
7589 if(attrs[i].name.toLowerCase() === 'alt'){
7592 haveAlt = true;
7602 break;
761 }
762 }
7634 if(haveAlt === false){
7642 reporter.warn('Alt of img tag must be set value.', event.line, event.col, self, event.raw);
765 }
766 }
767 });
768 }
769});
770/**
771 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
772 * MIT Licensed
773 */
7741HTMLHint.addRule({
775 id: 'jshint',
776 description: 'Scan script with jshint.',
777 init: function(parser, reporter, options){
7784 var self = this;
7794 parser.addListener('cdata', function(event){
7804 if(event.tagName.toLowerCase() === 'script'){
781
7824 var mapAttrs = parser.getMapAttrs(event.attrs),
783 type = mapAttrs.type;
784
785 // Only scan internal javascript
7864 if(mapAttrs.src !== undefined || (type && /^(text\/javascript)$/i.test(type) === false)){
7872 return;
788 }
789
7902 var jsVerify;
791
7922 if(typeof exports === 'object' && require){
7932 jsVerify = require('jshint').JSHINT;
794 }
795 else{
7960 jsVerify = JSHINT;
797 }
798
7992 if(options !== undefined){
8002 var styleLine = event.line - 1,
801 styleCol = event.col - 1;
8022 var code = event.raw.replace(/\t/g,' ');
8032 try{
8042 var status = jsVerify(code, options);
8052 if(status === false){
8062 jsVerify.errors.forEach(function(error){
8078 var line = error.line;
8088 reporter.warn(error.reason, styleLine + line, (line === 1 ? styleCol : 0) + error.character, self, error.evidence);
809 });
810 }
811 }
812 catch(e){}
813 }
814
815 }
816 });
817 }
818});
819/**
820 * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com>
821 * MIT Licensed
822 */
8231HTMLHint.addRule({
824 id: 'space-tab-mixed-disabled',
825 description: 'Spaces and tabs can not mixed in front of line.',
826 init: function(parser, reporter){
8276 var self = this;
8286 parser.addListener('text', function(event){
82912 var raw = event.raw;
83012 var reMixed = /(^|\r?\n)( +\t|\t+ )/g;
83112 var match;
83212 while((match = reMixed.exec(raw))){
8333 var fixedPos = parser.fixPos(event, match.index + match[1].length);
8343 reporter.warn('Mixed spaces and tabs in front of line.', fixedPos.line, 1, self, event.raw);
835 }
836 });
837 }
838});
839/**
840 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
841 * MIT Licensed
842 */
8431HTMLHint.addRule({
844 id: 'spec-char-escape',
845 description: 'Special characters must be escaped.',
846 init: function(parser, reporter){
8475 var self = this;
8485 parser.addListener('text', function(event){
8495 var raw = event.raw,
850 reSpecChar = /[<>]/g,
851 match;
8525 while((match = reSpecChar.exec(raw))){
8535 var fixedPos = parser.fixPos(event, match.index);
8545 reporter.error('Special characters must be escaped : [ '+match[0]+' ].', fixedPos.line, fixedPos.col, self, event.raw);
855 }
856 });
857 }
858});
859/**
860 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
861 * MIT Licensed
862 */
8631HTMLHint.addRule({
864 id: 'src-not-empty',
865 description: 'Src of img(script,link) must set value.',
866 init: function(parser, reporter){
8676 var self = this;
8686 parser.addListener('tagstart', function(event){
86935 var tagName = event.tagName,
870 attrs = event.attrs,
871 attr,
872 col = event.col + tagName.length + 1;
87335 for(var i=0, l=attrs.length;i<l;i++){
87442 attr = attrs[i];
87542 if(((/^(img|script|embed|bgsound|iframe)$/.test(tagName) === true && attr.name === 'src') ||
876 (tagName === 'link' && attr.name === 'href') ||
877 (tagName === 'object' && attr.name === 'data')) &&
878 attr.value === ''){
87916 reporter.error('[ '+attr.name + '] of [ '+tagName+' ] must set value.', event.line, col + attr.index, self, attr.raw);
880 }
881 }
882 });
883 }
884});
885/**
886 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
887 * MIT Licensed
888 */
8891HTMLHint.addRule({
890 id: 'style-disabled',
891 description: 'Style tag can not be use.',
892 init: function(parser, reporter){
8932 var self = this;
8942 parser.addListener('tagstart', function(event){
8954 if(event.tagName.toLowerCase() === 'style'){
8961 reporter.warn('Style tag can not be use.', event.line, event.col, self, event.raw);
897 }
898 });
899 }
900});
901/**
902 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
903 * MIT Licensed
904 */
9051HTMLHint.addRule({
906 id: 'tag-pair',
907 description: 'Tag must be paired.',
908 init: function(parser, reporter){
9096 var self = this;
9106 var stack=[],
911 mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01
9126 parser.addListener('tagstart', function(event){
91311 var tagName = event.tagName.toLowerCase();
91411 if (mapEmptyTags[tagName] === undefined && !event.close){
9159 stack.push(tagName);
916 }
917 });
9186 parser.addListener('tagend', function(event){
9195 var tagName = event.tagName.toLowerCase();
920 //向上寻找匹配的开始标签
9215 for(var pos = stack.length-1;pos >= 0; pos--){
9225 if(stack[pos] === tagName){
9234 break;
924 }
925 }
9265 if(pos >= 0){
9274 var arrTags = [];
9284 for(var i=stack.length-1;i>pos;i--){
9291 arrTags.push('</'+stack[i]+'>');
930 }
9314 if(arrTags.length > 0){
9321 reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, event.raw);
933 }
9344 stack.length=pos;
935 }
936 else{
9371 reporter.error('Tag must be paired, No start tag: [ ' + event.raw + ' ]', event.line, event.col, self, event.raw);
938 }
939 });
9406 parser.addListener('end', function(event){
9416 var arrTags = [];
9426 for(var i=stack.length-1;i>=0;i--){
9434 arrTags.push('</'+stack[i]+'>');
944 }
9456 if(arrTags.length > 0){
9464 reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, '');
947 }
948 });
949 }
950});
951/**
952 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
953 * MIT Licensed
954 */
9551HTMLHint.addRule({
956 id: 'tag-self-close',
957 description: 'The empty tag must closed by self.',
958 init: function(parser, reporter){
9592 var self = this;
9602 var mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01
9612 parser.addListener('tagstart', function(event){
9624 var tagName = event.tagName.toLowerCase();
9634 if(mapEmptyTags[tagName] !== undefined){
9644 if(!event.close){
9652 reporter.warn('The empty tag : [ '+tagName+' ] must closed by self.', event.line, event.col, self, event.raw);
966 }
967 }
968 });
969 }
970});
971/**
972 * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
973 * MIT Licensed
974 */
9751HTMLHint.addRule({
976 id: 'tagname-lowercase',
977 description: 'Tagname must be lowercase.',
978 init: function(parser, reporter){
9795 var self = this;
9805 parser.addListener('tagstart,tagend', function(event){
98117 var tagName = event.tagName;
98217 if(tagName !== tagName.toLowerCase()){
9836 reporter.error('Tagname [ '+tagName+' ] must be lower case.', event.line, event.col, self, event.raw);
984 }
985 });
986 }
987});
\ No newline at end of file diff --git a/lib/htmlhint.js b/lib/htmlhint.js index a19574277..366cf7d55 100644 --- a/lib/htmlhint.js +++ b/lib/htmlhint.js @@ -5,4 +5,4 @@ * (c) 2013 Yanis Wang . * MIT Licensed */ -var HTMLHint=function(e){var t={};return t.version="0.9.6",t.rules={},t.defaultRuleset={"tagname-lowercase":!0,"attr-lowercase":!0,"attr-value-double-quotes":!0,"doctype-first":!0,"tag-pair":!0,"spec-char-escape":!0,"id-unique":!0,"src-not-empty":!0,"attr-no-duplication":!0},t.addRule=function(e){t.rules[e.id]=e},t.verify=function(a,n){a=a.replace(/^\s*/i,function(t,a){return n===e&&(n={}),a.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g,function(e,t,a){"false"===a?a=!1:"true"===a&&(a=!0),n[t]=a}),""}),(n===e||0===Object.keys(n).length)&&(n=t.defaultRuleset);var i,r=new HTMLParser,s=new t.Reporter(a.split(/\r?\n/),n),o=t.rules;for(var l in n)i=o[l],i!==e&&n[l]!==!1&&i.init(r,s,n[l]);return r.parse(a),s.messages},t}();"object"==typeof exports&&exports&&(exports.HTMLHint=HTMLHint),function(e){var t=function(){var e=this;e._init.apply(e,arguments)};t.prototype={_init:function(e,t){var a=this;a.lines=e,a.ruleset=t,a.messages=[]},error:function(e,t,a,n,i){this.report("error",e,t,a,n,i)},warn:function(e,t,a,n,i){this.report("warning",e,t,a,n,i)},info:function(e,t,a,n,i){this.report("info",e,t,a,n,i)},report:function(e,t,a,n,i,r){var s=this;s.messages.push({type:e,message:t,raw:r,evidence:s.lines[a-1],line:a,col:n,rule:{id:i.id,description:i.description,link:"https://github.com/yaniswang/HTMLHint/wiki/"+i.id}})}},e.Reporter=t}(HTMLHint);var HTMLParser=function(e){var t=function(){var e=this;e._init.apply(e,arguments)};return t.prototype={_init:function(){var e=this;e._listeners={},e._mapCdataTags=e.makeMap("script,style"),e._arrBlocks=[]},makeMap:function(e){for(var t={},a=e.split(","),n=0;a.length>n;n++)t[a[n]]=!0;return t},parse:function(t){function a(t,a,n,i){var r=n-w+1;i===e&&(i={}),i.raw=a,i.pos=n,i.line=b,i.col=r,L.push(i),u.fire(t,i);for(var s;s=p.exec(a);)b++,w=n+p.lastIndex}var n,i,r,s,o,l,d,c,u=this,f=u._mapCdataTags,g=/<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"']+))?)*?)\s*(\/?))>/g,m=/\s*([\w\-:]+)(?:\s*=\s*(?:(")([^"]*)"|(')([^']*)'|([^\s"']+)))?/g,p=/\r?\n/g,v=0,h=0,w=0,b=1,L=u._arrBlocks;for(u.fire("start",{pos:0,line:1,col:1});n=g.exec(t);)if(i=n.index,i>v&&(c=t.substring(v,i),o?d.push(c):a("text",c,v)),v=g.lastIndex,!(r=n[1])||(o&&r===o&&(c=d.join(""),a("cdata",c,h,{tagName:o,attrs:l}),o=null,l=null,d=null),o))if(o)d.push(n[0]);else if(r=n[4]){s=[];for(var H,y=n[5],T=0;H=m.exec(y);){var x=H[1],M=H[2]?H[2]:H[4]?H[4]:"",R=H[3]?H[3]:H[5]?H[5]:H[6]?H[6]:"";s.push({name:x,value:R,quote:M,index:H.index,raw:H[0]}),T+=H[0].length}T===y.length?(a("tagstart",n[0],i,{tagName:r,attrs:s,close:n[6]}),f[r]&&(o=r,l=s.concat(),d=[],h=v)):a("text",n[0],i)}else(n[2]||n[3])&&a("comment",n[0],i,{content:n[2]||n[3],"long":n[2]?!0:!1});else a("tagend",n[0],i,{tagName:r});t.length>v&&(c=t.substring(v,t.length),a("text",c,v)),u.fire("end",{pos:v,line:b,col:v-w+1})},addListener:function(t,a){for(var n,i=this._listeners,r=t.split(/[,\s]/),s=0,o=r.length;o>s;s++)n=r[s],i[n]===e&&(i[n]=[]),i[n].push(a)},fire:function(t,a){a===e&&(a={}),a.type=t;var n=this,i=[],r=n._listeners[t],s=n._listeners.all;r!==e&&(i=i.concat(r)),s!==e&&(i=i.concat(s));for(var o=0,l=i.length;l>o;o++)i[o].call(n,a)},removeListener:function(t,a){var n=this._listeners[t];if(n!==e)for(var i=0,r=n.length;r>i;i++)if(n[i]===a){n.splice(i,1);break}},fixPos:function(e,t){var a,n=e.raw.substr(0,t),i=n.split(/\r?\n/),r=i.length-1,s=e.line;return r>0?(s+=r,a=i[r].length+1):a=e.col+t,{line:s,col:a}},getMapAttrs:function(e){for(var t,a={},n=0,i=e.length;i>n;n++)t=e[n],a[t.name]=t.value;return a}},t}();"object"==typeof exports&&exports&&(exports.HTMLParser=HTMLParser),HTMLHint.addRule({id:"attr-lowercase",description:"Attribute name must be lowercase.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++){n=i[s];var l=n.name;l!==l.toLowerCase()&&t.error("Attribute name [ "+l+" ] must be lower case.",e.line,r+n.index,a,n.raw)}})}}),HTMLHint.addRule({id:"attr-no-duplication",description:"Attribute name can not been duplication.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i,r=e.attrs,s=e.col+e.tagName.length+1,o={},l=0,d=r.length;d>l;l++)n=r[l],i=n.name,o[i]===!0&&t.error("The name of attribute [ "+n.name+" ] been duplication.",e.line,s+n.index,a,n.raw),o[i]=!0})}}),HTMLHint.addRule({id:"attr-value-double-quotes",description:"Attribute value must closed by double quotes.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++)n=i[s],(""!==n.value&&'"'!==n.quote||""===n.value&&"'"===n.quote)&&t.error("The value of attribute [ "+n.name+" ] must closed by double quotes.",e.line,r+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"attr-value-not-empty",description:"Attribute must set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++)n=i[s],""===n.quote&&""===n.value&&t.warn("The attribute [ "+n.name+" ] must set value.",e.line,r+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"csslint",description:"Scan css with csslint.",init:function(e,t,a){var n=this;e.addListener("cdata",function(e){if("style"===e.tagName.toLowerCase()){var i;if(i="object"==typeof exports&&require?require("csslint").CSSLint.verify:CSSLint.verify,void 0!==a){var r=e.line-1,s=e.col-1;try{var o=i(e.raw,a).messages;o.forEach(function(e){var a=e.line;t["warning"===e.type?"warn":"error"]("["+e.rule.id+"] "+e.message,r+a,(1===a?s:0)+e.col,n,e.evidence)})}catch(l){}}}})}}),HTMLHint.addRule({id:"doctype-first",description:"Doctype must be first.",init:function(e,t){var a=this,n=function(i){"start"===i.type||"text"===i.type&&/^\s*$/.test(i.raw)||(("comment"!==i.type&&i.long===!1||/^DOCTYPE\s+/i.test(i.content)===!1)&&t.error("Doctype must be first.",i.line,i.col,a,i.raw),e.removeListener("all",n))};e.addListener("all",n)}}),HTMLHint.addRule({id:"doctype-html5",description:"Doctype must be html5.",init:function(e,t){function a(e){e.long===!1&&"doctype html"!==e.content.toLowerCase()&&t.warn("Doctype must be html5.",e.line,e.col,i,e.raw)}function n(){e.removeListener("comment",a),e.removeListener("tagstart",n)}var i=this;e.addListener("all",a),e.addListener("tagstart",n)}}),HTMLHint.addRule({id:"head-script-disabled",description:"The script tag can not be used in head.",init:function(e,t){function a(e){"script"===e.tagName.toLowerCase()&&t.warn("The script tag can not be used in head.",e.line,e.col,i,e.raw)}function n(t){"head"===t.tagName.toLowerCase()&&(e.removeListener("tagstart",a),e.removeListener("tagstart",n))}var i=this;e.addListener("tagstart",a),e.addListener("tagend",n)}}),HTMLHint.addRule({id:"href-abs-or-rel",description:"Href must be absolute or relative.",init:function(e,t,a){var n=this,i="abs"===a?"absolute":"relative";e.addListener("tagstart",function(e){for(var a,r=e.attrs,s=e.col+e.tagName.length+1,o=0,l=r.length;l>o;o++)if(a=r[o],"href"===a.name){("absolute"===i&&/^\w+?:/.test(a.value)===!1||"relative"===i&&/^https?:\/\//.test(a.value)===!0)&&t.warn("The value of href [ "+a.value+" ] must be "+i+".",e.line,s+a.index,n,a.raw);break}})}}),HTMLHint.addRule({id:"id-class-ad-disabled",description:"Id and class can not use ad keyword, it will blocked by adblock software.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i,r=e.attrs,s=e.col+e.tagName.length+1,o=0,l=r.length;l>o;o++)n=r[o],i=n.name,/^(id|class)$/i.test(i)&&/(^|[-\_])ad([-\_]|$)/i.test(n.value)&&t.warn("The value of "+i+" can not use ad keyword.",e.line,s+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"id-class-value",description:"Id and class value must meet some rules.",init:function(e,t,a){var n,i=this,r={underline:{regId:/^[a-z\d]+(_[a-z\d]+)*$/,message:"Id and class value must lower case and split by underline."},dash:{regId:/^[a-z\d]+(-[a-z\d]+)*$/,message:"Id and class value must lower case and split by dash."},hump:{regId:/^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,message:"Id and class value must meet hump style."}};if(n="string"==typeof a?r[a]:a,n&&n.regId){var s=n.regId,o=n.message;e.addListener("tagstart",function(e){for(var a,n=e.attrs,r=e.col+e.tagName.length+1,l=0,d=n.length;d>l;l++)if(a=n[l],"id"===a.name.toLowerCase()&&s.test(a.value)===!1&&t.warn(o,e.line,r+a.index,i,a.raw),"class"===a.name.toLowerCase())for(var c,u=a.value.split(/\s+/g),f=0,g=u.length;g>f;f++)c=u[f],c&&s.test(c)===!1&&t.warn(o,e.line,r+a.index,i,c)})}}}),HTMLHint.addRule({id:"id-unique",description:"Id must be unique.",init:function(e,t){var a=this,n={};e.addListener("tagstart",function(e){for(var i,r,s=e.attrs,o=e.col+e.tagName.length+1,l=0,d=s.length;d>l;l++)if(i=s[l],"id"===i.name.toLowerCase()){r=i.value,r&&(void 0===n[r]?n[r]=1:n[r]++,n[r]>1&&t.error("Id redefinition of [ "+r+" ].",e.line,o+i.index,a,i.raw));break}})}}),HTMLHint.addRule({id:"img-alt-require",description:"Alt of img tag must be set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){if("img"===e.tagName.toLowerCase()){for(var n=e.attrs,i=!1,r=0,s=n.length;s>r;r++)if("alt"===n[r].name.toLowerCase()){i=!0;break}i===!1&&t.warn("Alt of img tag must be set value.",e.line,e.col,a,e.raw)}})}}),HTMLHint.addRule({id:"jshint",description:"Scan script with jshint.",init:function(e,t,a){var n=this;e.addListener("cdata",function(i){if("script"===i.tagName.toLowerCase()){var r=e.getMapAttrs(i.attrs),s=r.type;if(void 0!==r.src||s&&/^(text\/javascript)$/i.test(s)===!1)return;var o;if(o="object"==typeof exports&&require?require("jshint").JSHINT:JSHINT,void 0!==a){var l=i.line-1,d=i.col-1,c=i.raw.replace(/\t/g," ");try{var u=o(c,a);u===!1&&o.errors.forEach(function(e){var a=e.line;t.warn(e.reason,l+a,(1===a?d:0)+e.character,n,e.evidence)})}catch(f){}}}})}}),HTMLHint.addRule({id:"space-tab-mixed-disabled",description:"Spaces and tabs can not mixed in front of line.",init:function(e,t){var a=this;e.addListener("text",function(n){for(var i,r=n.raw,s=/(^|\r?\n)( +\t|\t+ )/g;i=s.exec(r);){var o=e.fixPos(n,i.index+i[1].length);t.warn("Mixed spaces and tabs in front of line.",o.line,1,a,n.raw)}})}}),HTMLHint.addRule({id:"spec-char-escape",description:"Special characters must be escaped.",init:function(e,t){var a=this;e.addListener("text",function(n){for(var i,r=n.raw,s=/[<>]/g;i=s.exec(r);){var o=e.fixPos(n,i.index);t.error("Special characters must be escaped : [ "+i[0]+" ].",o.line,o.col,a,n.raw)}})}}),HTMLHint.addRule({id:"src-not-empty",description:"Src of img(script,link) must set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.tagName,r=e.attrs,s=e.col+i.length+1,o=0,l=r.length;l>o;o++)n=r[o],(/^(img|script|embed|bgsound|iframe)$/.test(i)===!0&&"src"===n.name||"link"===i&&"href"===n.name||"object"===i&&"data"===n.name)&&""===n.value&&t.error("[ "+n.name+"] of [ "+i+" ] must set value.",e.line,s+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"style-disabled",description:"Style tag can not be use.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){"style"===e.tagName.toLowerCase()&&t.warn("Style tag can not be use.",e.line,e.col,a,e.raw)})}}),HTMLHint.addRule({id:"tag-pair",description:"Tag must be paired.",init:function(e,t){var a=this,n=[],i=e.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");e.addListener("tagstart",function(e){var t=e.tagName.toLowerCase();void 0!==i[t]||e.close||n.push(t)}),e.addListener("tagend",function(e){for(var i=e.tagName.toLowerCase(),r=n.length-1;r>=0&&n[r]!==i;r--);if(r>=0){for(var s=[],o=n.length-1;o>r;o--)s.push("");s.length>0&&t.error("Tag must be paired, Missing: [ "+s.join("")+" ]",e.line,e.col,a,e.raw),n.length=r}else t.error("Tag must be paired, No start tag: [ "+e.raw+" ]",e.line,e.col,a,e.raw)}),e.addListener("end",function(e){for(var i=[],r=n.length-1;r>=0;r--)i.push("");i.length>0&&t.error("Tag must be paired, Missing: [ "+i.join("")+" ]",e.line,e.col,a,"")})}}),HTMLHint.addRule({id:"tag-self-close",description:"The empty tag must closed by self.",init:function(e,t){var a=this,n=e.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");e.addListener("tagstart",function(e){var i=e.tagName.toLowerCase();void 0!==n[i]&&(e.close||t.warn("The empty tag : [ "+i+" ] must closed by self.",e.line,e.col,a,e.raw))})}}),HTMLHint.addRule({id:"tagname-lowercase",description:"Tagname must be lowercase.",init:function(e,t){var a=this;e.addListener("tagstart,tagend",function(e){var n=e.tagName;n!==n.toLowerCase()&&t.error("Tagname [ "+n+" ] must be lower case.",e.line,e.col,a,e.raw)})}}); \ No newline at end of file +var HTMLHint=function(e){var t={};return t.version="0.9.6",t.rules={},t.defaultRuleset={"tagname-lowercase":!0,"attr-lowercase":!0,"attr-value-double-quotes":!0,"doctype-first":!0,"tag-pair":!0,"spec-char-escape":!0,"id-unique":!0,"src-not-empty":!0,"attr-no-duplication":!0},t.addRule=function(e){t.rules[e.id]=e},t.verify=function(a,n){a=a.replace(/^\s*/i,function(t,a){return n===e&&(n={}),a.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g,function(e,t,a){"false"===a?a=!1:"true"===a&&(a=!0),n[t]=a}),""}),(n===e||0===Object.keys(n).length)&&(n=t.defaultRuleset);var i,r=new HTMLParser,s=new t.Reporter(a.split(/\r?\n/),n),o=t.rules;for(var l in n)i=o[l],i!==e&&n[l]!==!1&&i.init(r,s,n[l]);return r.parse(a),s.messages},t}();"object"==typeof exports&&exports&&(exports.HTMLHint=HTMLHint),function(e){var t=function(){var e=this;e._init.apply(e,arguments)};t.prototype={_init:function(e,t){var a=this;a.lines=e,a.ruleset=t,a.messages=[]},error:function(e,t,a,n,i){this.report("error",e,t,a,n,i)},warn:function(e,t,a,n,i){this.report("warning",e,t,a,n,i)},info:function(e,t,a,n,i){this.report("info",e,t,a,n,i)},report:function(e,t,a,n,i,r){var s=this;s.messages.push({type:e,message:t,raw:r,evidence:s.lines[a-1],line:a,col:n,rule:{id:i.id,description:i.description,link:"https://github.com/yaniswang/HTMLHint/wiki/"+i.id}})}},e.Reporter=t}(HTMLHint);var HTMLParser=function(e){var t=function(){var e=this;e._init.apply(e,arguments)};return t.prototype={_init:function(){var e=this;e._listeners={},e._mapCdataTags=e.makeMap("script,style"),e._arrBlocks=[]},makeMap:function(e){for(var t={},a=e.split(","),n=0;a.length>n;n++)t[a[n]]=!0;return t},parse:function(t){function a(t,a,n,i){var r=n-w+1;i===e&&(i={}),i.raw=a,i.pos=n,i.line=b,i.col=r,L.push(i),c.fire(t,i);for(var s;s=p.exec(a);)b++,w=n+p.lastIndex}var n,i,r,s,o,l,d,u,c=this,f=c._mapCdataTags,g=/<(?:\/([^\s>]+)\s*|!--([\s\S]*?)--|!([^>]*?)|([\w\-:]+)((?:\s+[\w\-:]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"']+))?)*?)\s*(\/?))>/g,m=/\s*([\w\-:]+)(?:\s*=\s*(?:(")([^"]*)"|(')([^']*)'|([^\s"']+)))?/g,p=/\r?\n/g,v=0,h=0,w=0,b=1,L=c._arrBlocks;for(c.fire("start",{pos:0,line:1,col:1});n=g.exec(t);)if(i=n.index,i>v&&(u=t.substring(v,i),o?d.push(u):a("text",u,v)),v=g.lastIndex,!(r=n[1])||(o&&r===o&&(u=d.join(""),a("cdata",u,h,{tagName:o,attrs:l}),o=null,l=null,d=null),o))if(o)d.push(n[0]);else if(r=n[4]){s=[];for(var H,y=n[5],T=0;H=m.exec(y);){var x=H[1],M=H[2]?H[2]:H[4]?H[4]:"",R=H[3]?H[3]:H[5]?H[5]:H[6]?H[6]:"";s.push({name:x,value:R,quote:M,index:H.index,raw:H[0]}),T+=H[0].length}T===y.length?(a("tagstart",n[0],i,{tagName:r,attrs:s,close:n[6]}),f[r]&&(o=r,l=s.concat(),d=[],h=v)):a("text",n[0],i)}else(n[2]||n[3])&&a("comment",n[0],i,{content:n[2]||n[3],"long":n[2]?!0:!1});else a("tagend",n[0],i,{tagName:r});t.length>v&&(u=t.substring(v,t.length),a("text",u,v)),c.fire("end",{pos:v,line:b,col:v-w+1})},addListener:function(t,a){for(var n,i=this._listeners,r=t.split(/[,\s]/),s=0,o=r.length;o>s;s++)n=r[s],i[n]===e&&(i[n]=[]),i[n].push(a)},fire:function(t,a){a===e&&(a={}),a.type=t;var n=this,i=[],r=n._listeners[t],s=n._listeners.all;r!==e&&(i=i.concat(r)),s!==e&&(i=i.concat(s));for(var o=0,l=i.length;l>o;o++)i[o].call(n,a)},removeListener:function(t,a){var n=this._listeners[t];if(n!==e)for(var i=0,r=n.length;r>i;i++)if(n[i]===a){n.splice(i,1);break}},fixPos:function(e,t){var a,n=e.raw.substr(0,t),i=n.split(/\r?\n/),r=i.length-1,s=e.line;return r>0?(s+=r,a=i[r].length+1):a=e.col+t,{line:s,col:a}},getMapAttrs:function(e){for(var t,a={},n=0,i=e.length;i>n;n++)t=e[n],a[t.name]=t.value;return a}},t}();"object"==typeof exports&&exports&&(exports.HTMLParser=HTMLParser),HTMLHint.addRule({id:"attr-lowercase",description:"Attribute name must be lowercase.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++){n=i[s];var l=n.name;l!==l.toLowerCase()&&t.error("Attribute name [ "+l+" ] must be lower case.",e.line,r+n.index,a,n.raw)}})}}),HTMLHint.addRule({id:"attr-no-duplication",description:"Attribute name can not been duplication.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i,r=e.attrs,s=e.col+e.tagName.length+1,o={},l=0,d=r.length;d>l;l++)n=r[l],i=n.name,o[i]===!0&&t.error("The name of attribute [ "+n.name+" ] been duplication.",e.line,s+n.index,a,n.raw),o[i]=!0})}}),HTMLHint.addRule({id:"attr-unsafe-chars",description:"Attribute value cant not use unsafe chars.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=/[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/,o=0,l=i.length;l>o;o++)n=i[o],s.test(n.value)===!0&&t.warn("The value of attribute [ "+n.name+" ] cant not use unsafe chars.",e.line,r+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"attr-value-double-quotes",description:"Attribute value must closed by double quotes.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++)n=i[s],(""!==n.value&&'"'!==n.quote||""===n.value&&"'"===n.quote)&&t.error("The value of attribute [ "+n.name+" ] must closed by double quotes.",e.line,r+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"attr-value-not-empty",description:"Attribute must set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.attrs,r=e.col+e.tagName.length+1,s=0,o=i.length;o>s;s++)n=i[s],""===n.quote&&""===n.value&&t.warn("The attribute [ "+n.name+" ] must set value.",e.line,r+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"csslint",description:"Scan css with csslint.",init:function(e,t,a){var n=this;e.addListener("cdata",function(e){if("style"===e.tagName.toLowerCase()){var i;if(i="object"==typeof exports&&require?require("csslint").CSSLint.verify:CSSLint.verify,void 0!==a){var r=e.line-1,s=e.col-1;try{var o=i(e.raw,a).messages;o.forEach(function(e){var a=e.line;t["warning"===e.type?"warn":"error"]("["+e.rule.id+"] "+e.message,r+a,(1===a?s:0)+e.col,n,e.evidence)})}catch(l){}}}})}}),HTMLHint.addRule({id:"doctype-first",description:"Doctype must be first.",init:function(e,t){var a=this,n=function(i){"start"===i.type||"text"===i.type&&/^\s*$/.test(i.raw)||(("comment"!==i.type&&i.long===!1||/^DOCTYPE\s+/i.test(i.content)===!1)&&t.error("Doctype must be first.",i.line,i.col,a,i.raw),e.removeListener("all",n))};e.addListener("all",n)}}),HTMLHint.addRule({id:"doctype-html5",description:"Doctype must be html5.",init:function(e,t){function a(e){e.long===!1&&"doctype html"!==e.content.toLowerCase()&&t.warn("Doctype must be html5.",e.line,e.col,i,e.raw)}function n(){e.removeListener("comment",a),e.removeListener("tagstart",n)}var i=this;e.addListener("all",a),e.addListener("tagstart",n)}}),HTMLHint.addRule({id:"head-script-disabled",description:"The script tag can not be used in head.",init:function(e,t){function a(e){"script"===e.tagName.toLowerCase()&&t.warn("The script tag can not be used in head.",e.line,e.col,i,e.raw)}function n(t){"head"===t.tagName.toLowerCase()&&(e.removeListener("tagstart",a),e.removeListener("tagstart",n))}var i=this;e.addListener("tagstart",a),e.addListener("tagend",n)}}),HTMLHint.addRule({id:"href-abs-or-rel",description:"Href must be absolute or relative.",init:function(e,t,a){var n=this,i="abs"===a?"absolute":"relative";e.addListener("tagstart",function(e){for(var a,r=e.attrs,s=e.col+e.tagName.length+1,o=0,l=r.length;l>o;o++)if(a=r[o],"href"===a.name){("absolute"===i&&/^\w+?:/.test(a.value)===!1||"relative"===i&&/^https?:\/\//.test(a.value)===!0)&&t.warn("The value of href [ "+a.value+" ] must be "+i+".",e.line,s+a.index,n,a.raw);break}})}}),HTMLHint.addRule({id:"id-class-ad-disabled",description:"Id and class can not use ad keyword, it will blocked by adblock software.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i,r=e.attrs,s=e.col+e.tagName.length+1,o=0,l=r.length;l>o;o++)n=r[o],i=n.name,/^(id|class)$/i.test(i)&&/(^|[-\_])ad([-\_]|$)/i.test(n.value)&&t.warn("The value of "+i+" can not use ad keyword.",e.line,s+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"id-class-value",description:"Id and class value must meet some rules.",init:function(e,t,a){var n,i=this,r={underline:{regId:/^[a-z\d]+(_[a-z\d]+)*$/,message:"Id and class value must lower case and split by underline."},dash:{regId:/^[a-z\d]+(-[a-z\d]+)*$/,message:"Id and class value must lower case and split by dash."},hump:{regId:/^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,message:"Id and class value must meet hump style."}};if(n="string"==typeof a?r[a]:a,n&&n.regId){var s=n.regId,o=n.message;e.addListener("tagstart",function(e){for(var a,n=e.attrs,r=e.col+e.tagName.length+1,l=0,d=n.length;d>l;l++)if(a=n[l],"id"===a.name.toLowerCase()&&s.test(a.value)===!1&&t.warn(o,e.line,r+a.index,i,a.raw),"class"===a.name.toLowerCase())for(var u,c=a.value.split(/\s+/g),f=0,g=c.length;g>f;f++)u=c[f],u&&s.test(u)===!1&&t.warn(o,e.line,r+a.index,i,u)})}}}),HTMLHint.addRule({id:"id-unique",description:"Id must be unique.",init:function(e,t){var a=this,n={};e.addListener("tagstart",function(e){for(var i,r,s=e.attrs,o=e.col+e.tagName.length+1,l=0,d=s.length;d>l;l++)if(i=s[l],"id"===i.name.toLowerCase()){r=i.value,r&&(void 0===n[r]?n[r]=1:n[r]++,n[r]>1&&t.error("Id redefinition of [ "+r+" ].",e.line,o+i.index,a,i.raw));break}})}}),HTMLHint.addRule({id:"img-alt-require",description:"Alt of img tag must be set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){if("img"===e.tagName.toLowerCase()){for(var n=e.attrs,i=!1,r=0,s=n.length;s>r;r++)if("alt"===n[r].name.toLowerCase()){i=!0;break}i===!1&&t.warn("Alt of img tag must be set value.",e.line,e.col,a,e.raw)}})}}),HTMLHint.addRule({id:"jshint",description:"Scan script with jshint.",init:function(e,t,a){var n=this;e.addListener("cdata",function(i){if("script"===i.tagName.toLowerCase()){var r=e.getMapAttrs(i.attrs),s=r.type;if(void 0!==r.src||s&&/^(text\/javascript)$/i.test(s)===!1)return;var o;if(o="object"==typeof exports&&require?require("jshint").JSHINT:JSHINT,void 0!==a){var l=i.line-1,d=i.col-1,u=i.raw.replace(/\t/g," ");try{var c=o(u,a);c===!1&&o.errors.forEach(function(e){var a=e.line;t.warn(e.reason,l+a,(1===a?d:0)+e.character,n,e.evidence)})}catch(f){}}}})}}),HTMLHint.addRule({id:"space-tab-mixed-disabled",description:"Spaces and tabs can not mixed in front of line.",init:function(e,t){var a=this;e.addListener("text",function(n){for(var i,r=n.raw,s=/(^|\r?\n)( +\t|\t+ )/g;i=s.exec(r);){var o=e.fixPos(n,i.index+i[1].length);t.warn("Mixed spaces and tabs in front of line.",o.line,1,a,n.raw)}})}}),HTMLHint.addRule({id:"spec-char-escape",description:"Special characters must be escaped.",init:function(e,t){var a=this;e.addListener("text",function(n){for(var i,r=n.raw,s=/[<>]/g;i=s.exec(r);){var o=e.fixPos(n,i.index);t.error("Special characters must be escaped : [ "+i[0]+" ].",o.line,o.col,a,n.raw)}})}}),HTMLHint.addRule({id:"src-not-empty",description:"Src of img(script,link) must set value.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){for(var n,i=e.tagName,r=e.attrs,s=e.col+i.length+1,o=0,l=r.length;l>o;o++)n=r[o],(/^(img|script|embed|bgsound|iframe)$/.test(i)===!0&&"src"===n.name||"link"===i&&"href"===n.name||"object"===i&&"data"===n.name)&&""===n.value&&t.error("[ "+n.name+"] of [ "+i+" ] must set value.",e.line,s+n.index,a,n.raw)})}}),HTMLHint.addRule({id:"style-disabled",description:"Style tag can not be use.",init:function(e,t){var a=this;e.addListener("tagstart",function(e){"style"===e.tagName.toLowerCase()&&t.warn("Style tag can not be use.",e.line,e.col,a,e.raw)})}}),HTMLHint.addRule({id:"tag-pair",description:"Tag must be paired.",init:function(e,t){var a=this,n=[],i=e.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");e.addListener("tagstart",function(e){var t=e.tagName.toLowerCase();void 0!==i[t]||e.close||n.push(t)}),e.addListener("tagend",function(e){for(var i=e.tagName.toLowerCase(),r=n.length-1;r>=0&&n[r]!==i;r--);if(r>=0){for(var s=[],o=n.length-1;o>r;o--)s.push("");s.length>0&&t.error("Tag must be paired, Missing: [ "+s.join("")+" ]",e.line,e.col,a,e.raw),n.length=r}else t.error("Tag must be paired, No start tag: [ "+e.raw+" ]",e.line,e.col,a,e.raw)}),e.addListener("end",function(e){for(var i=[],r=n.length-1;r>=0;r--)i.push("");i.length>0&&t.error("Tag must be paired, Missing: [ "+i.join("")+" ]",e.line,e.col,a,"")})}}),HTMLHint.addRule({id:"tag-self-close",description:"The empty tag must closed by self.",init:function(e,t){var a=this,n=e.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");e.addListener("tagstart",function(e){var i=e.tagName.toLowerCase();void 0!==n[i]&&(e.close||t.warn("The empty tag : [ "+i+" ] must closed by self.",e.line,e.col,a,e.raw))})}}),HTMLHint.addRule({id:"tagname-lowercase",description:"Tagname must be lowercase.",init:function(e,t){var a=this;e.addListener("tagstart,tagend",function(e){var n=e.tagName;n!==n.toLowerCase()&&t.error("Tagname [ "+n+" ] must be lower case.",e.line,e.col,a,e.raw)})}}); \ No newline at end of file diff --git a/src/rules/attr-unsafe-chars.js b/src/rules/attr-unsafe-chars.js new file mode 100644 index 000000000..cb7ede68f --- /dev/null +++ b/src/rules/attr-unsafe-chars.js @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2014, Yanis Wang + * 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 + * 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 = 'Sud Web 2012'; + 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 = ''; + var messages = HTMLHint.verify(code, ruleOptions); + expect(messages.length).to.be(0); + }); + +}); \ No newline at end of file