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});