1 | | /** |
2 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
3 | | * MIT Licensed |
4 | | */ |
5 | 1 | var HTMLHint = (function (undefined) { |
6 | | |
7 | 1 | var HTMLHint = {}; |
8 | | |
9 | 1 | HTMLHint.version = '@VERSION'; |
10 | | |
11 | 1 | HTMLHint.rules = {}; |
12 | | |
13 | | //默认配置 |
14 | 1 | 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 | | |
26 | 1 | HTMLHint.addRule = function(rule){ |
27 | 22 | HTMLHint.rules[rule.id] = rule; |
28 | | }; |
29 | | |
30 | 1 | HTMLHint.verify = function(html, ruleset){ |
31 | | // parse inline ruleset |
32 | 84 | html = html.replace(/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i, function(all, strRuleset){ |
33 | 2 | if(ruleset === undefined){ |
34 | 0 | ruleset = {}; |
35 | | } |
36 | 2 | strRuleset.replace(/(?:^|,)\s*([^:]+)\s*:\s*([^,\s]+)/g, function(all, key, value){ |
37 | 2 | if(value === 'false'){ |
38 | 1 | value = false; |
39 | | } |
40 | 1 | else if(value === 'true'){ |
41 | 1 | value = true; |
42 | | } |
43 | 2 | ruleset[key] = value; |
44 | | }); |
45 | 2 | return ''; |
46 | | }); |
47 | | |
48 | 84 | if(ruleset === undefined || Object.keys(ruleset).length ===0){ |
49 | 3 | ruleset = HTMLHint.defaultRuleset; |
50 | | } |
51 | | |
52 | 84 | var parser = new HTMLParser(); |
53 | 84 | var reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset); |
54 | | |
55 | 84 | var rules = HTMLHint.rules, |
56 | | rule; |
57 | 84 | for (var id in ruleset){ |
58 | 108 | rule = rules[id]; |
59 | 108 | if (rule !== undefined && ruleset[id] !== false){ |
60 | 105 | rule.init(parser, reporter, ruleset[id]); |
61 | | } |
62 | | } |
63 | | |
64 | 84 | parser.parse(html); |
65 | | |
66 | 84 | return reporter.messages; |
67 | | }; |
68 | | |
69 | 1 | return HTMLHint; |
70 | | |
71 | | })(); |
72 | | |
73 | 1 | if (typeof exports === 'object' && exports){ |
74 | 1 | exports.HTMLHint = HTMLHint; |
75 | | } |
76 | | /** |
77 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
78 | | * MIT Licensed |
79 | | */ |
80 | 1 | (function(HTMLHint, undefined){ |
81 | | |
82 | 1 | var Reporter = function(){ |
83 | 84 | var self = this; |
84 | 84 | self._init.apply(self,arguments); |
85 | | }; |
86 | | |
87 | 1 | Reporter.prototype = { |
88 | | _init: function(lines, ruleset){ |
89 | 84 | var self = this; |
90 | 84 | self.lines = lines; |
91 | 84 | self.ruleset = ruleset; |
92 | 84 | self.messages = []; |
93 | | }, |
94 | | //错误 |
95 | | error: function(message, line, col, rule, raw){ |
96 | 52 | this.report('error', message, line, col, rule, raw); |
97 | | }, |
98 | | //警告 |
99 | | warn: function(message, line, col, rule, raw){ |
100 | 49 | this.report('warning', message, line, col, rule, raw); |
101 | | }, |
102 | | //信息 |
103 | | info: function(message, line, col, rule, raw){ |
104 | 0 | this.report('info', message, line, col, rule, raw); |
105 | | }, |
106 | | //报告 |
107 | | report: function(type, message, line, col, rule, raw){ |
108 | 101 | var self = this; |
109 | 101 | 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 | | |
125 | 1 | HTMLHint.Reporter = Reporter; |
126 | | |
127 | | })(HTMLHint); |
128 | | /** |
129 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
130 | | * MIT Licensed |
131 | | */ |
132 | 1 | var HTMLParser = (function(undefined){ |
133 | | |
134 | 1 | var HTMLParser = function(){ |
135 | 109 | var self = this; |
136 | 109 | self._init.apply(self,arguments); |
137 | | }; |
138 | | |
139 | 1 | HTMLParser.prototype = { |
140 | | _init: function(){ |
141 | 109 | var self = this; |
142 | 109 | self._listeners = {}; |
143 | 109 | self._mapCdataTags = self.makeMap("script,style"); |
144 | 109 | self._arrBlocks = []; |
145 | | }, |
146 | | |
147 | | makeMap: function(str){ |
148 | 117 | var obj = {}, items = str.split(","); |
149 | 117 | for ( var i = 0; i < items.length; i++ ){ |
150 | 330 | obj[ items[i] ] = true; |
151 | | } |
152 | 117 | return obj; |
153 | | }, |
154 | | |
155 | | // parse html code |
156 | | parse: function(html){ |
157 | | |
158 | 109 | var self = this, |
159 | | mapCdataTags = self._mapCdataTags; |
160 | | |
161 | 109 | 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 | | |
165 | 109 | var match, matchIndex, lastIndex = 0, tagName, arrAttrs, tagCDATA, attrsCDATA, arrCDATA, lastCDATAIndex = 0, text; |
166 | 109 | var lastLineIndex = 0, line = 1; |
167 | 109 | var arrBlocks = self._arrBlocks; |
168 | | |
169 | 109 | self.fire('start', { |
170 | | pos: 0, |
171 | | line: 1, |
172 | | col: 1 |
173 | | }); |
174 | | |
175 | 109 | while((match = regTag.exec(html))){ |
176 | 260 | matchIndex = match.index; |
177 | 260 | if(matchIndex > lastIndex){//保存前面的文本或者CDATA |
178 | 78 | text = html.substring(lastIndex, matchIndex); |
179 | 78 | if(tagCDATA){ |
180 | 10 | arrCDATA.push(text); |
181 | | } |
182 | | else{//文本 |
183 | 68 | saveBlock('text', text, lastIndex); |
184 | | } |
185 | | } |
186 | 260 | lastIndex = regTag.lastIndex; |
187 | | |
188 | 260 | if((tagName = match[1])){ |
189 | 91 | if(tagCDATA && tagName === tagCDATA){//结束标签前输出CDATA |
190 | 15 | text = arrCDATA.join(''); |
191 | 15 | saveBlock('cdata', text, lastCDATAIndex, { |
192 | | 'tagName': tagCDATA, |
193 | | 'attrs': attrsCDATA |
194 | | }); |
195 | 15 | tagCDATA = null; |
196 | 15 | attrsCDATA = null; |
197 | 15 | arrCDATA = null; |
198 | | } |
199 | 91 | if(!tagCDATA){ |
200 | | //标签结束 |
201 | 90 | saveBlock('tagend', match[0], matchIndex, { |
202 | | 'tagName': tagName |
203 | | }); |
204 | 90 | continue; |
205 | | } |
206 | | } |
207 | | |
208 | 170 | if(tagCDATA){ |
209 | 1 | arrCDATA.push(match[0]); |
210 | | } |
211 | | else{ |
212 | 169 | if((tagName = match[4])){//标签开始 |
213 | 156 | arrAttrs = []; |
214 | 156 | var attrs = match[5], |
215 | | attrMatch, |
216 | | attrMatchCount = 0; |
217 | 156 | while((attrMatch = regAttr.exec(attrs))){ |
218 | 152 | 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] : ''; |
224 | 152 | arrAttrs.push({'name': name, 'value': value, 'quote': quote, 'index': attrMatch.index, 'raw': attrMatch[0]}); |
225 | 152 | attrMatchCount += attrMatch[0].length; |
226 | | } |
227 | 156 | if(attrMatchCount === attrs.length){ |
228 | 156 | saveBlock('tagstart', match[0], matchIndex, { |
229 | | 'tagName': tagName, |
230 | | 'attrs': arrAttrs, |
231 | | 'close': match[6] |
232 | | }); |
233 | 156 | if(mapCdataTags[tagName]){ |
234 | 15 | tagCDATA = tagName; |
235 | 15 | attrsCDATA = arrAttrs.concat(); |
236 | 15 | arrCDATA = []; |
237 | 15 | lastCDATAIndex = lastIndex; |
238 | | } |
239 | | } |
240 | | else{//如果出现漏匹配,则把当前内容匹配为text |
241 | 0 | saveBlock('text', match[0], matchIndex); |
242 | | } |
243 | | } |
244 | 13 | else if(match[2] || match[3]){//注释标签 |
245 | 13 | saveBlock('comment', match[0], matchIndex, { |
246 | | 'content': match[2] || match[3], |
247 | | 'long': match[2]?true:false |
248 | | }); |
249 | | } |
250 | | } |
251 | | } |
252 | | |
253 | 109 | if(html.length > lastIndex){ |
254 | | //结尾文本 |
255 | 13 | text = html.substring(lastIndex, html.length); |
256 | 13 | saveBlock('text', text, lastIndex); |
257 | | } |
258 | | |
259 | 109 | self.fire('end', { |
260 | | pos: lastIndex, |
261 | | line: line, |
262 | | col: lastIndex - lastLineIndex + 1 |
263 | | }); |
264 | | |
265 | | //存储区块 |
266 | 109 | function saveBlock(type, raw, pos, data){ |
267 | 355 | var col = pos - lastLineIndex + 1; |
268 | 355 | if(data === undefined){ |
269 | 81 | data = {}; |
270 | | } |
271 | 355 | data.raw = raw; |
272 | 355 | data.pos = pos; |
273 | 355 | data.line = line; |
274 | 355 | data.col = col; |
275 | 355 | arrBlocks.push(data); |
276 | 355 | self.fire(type, data); |
277 | 355 | var lineMatch; |
278 | 355 | while((lineMatch = regLine.exec(raw))){ |
279 | 22 | line ++; |
280 | 22 | lastLineIndex = pos + regLine.lastIndex; |
281 | | } |
282 | | } |
283 | | |
284 | | }, |
285 | | |
286 | | // add event |
287 | | addListener: function(types, listener){ |
288 | 147 | var _listeners = this._listeners; |
289 | 147 | var arrTypes = types.split(/[,\s]/), type; |
290 | 147 | for(var i=0, l = arrTypes.length;i<l;i++){ |
291 | 152 | type = arrTypes[i]; |
292 | 152 | if (_listeners[type] === undefined){ |
293 | 131 | _listeners[type] = []; |
294 | | } |
295 | 152 | _listeners[type].push(listener); |
296 | | } |
297 | | }, |
298 | | |
299 | | // fire event |
300 | | fire: function(type, data){ |
301 | 573 | if (data === undefined){ |
302 | 0 | data = {}; |
303 | | } |
304 | 573 | data.type = type; |
305 | 573 | var self = this, |
306 | | listeners = [], |
307 | | listenersType = self._listeners[type], |
308 | | listenersAll = self._listeners['all']; |
309 | 573 | if (listenersType !== undefined){ |
310 | 164 | listeners = listeners.concat(listenersType); |
311 | | } |
312 | 573 | if (listenersAll !== undefined){ |
313 | 137 | listeners = listeners.concat(listenersAll); |
314 | | } |
315 | 573 | for (var i = 0, l = listeners.length; i < l; i++){ |
316 | 328 | listeners[i].call(self, data); |
317 | | } |
318 | | }, |
319 | | |
320 | | // remove event |
321 | | removeListener: function(type, listener){ |
322 | 15 | var listenersType = this._listeners[type]; |
323 | 15 | if(listenersType !== undefined){ |
324 | 13 | for (var i = 0, l = listenersType.length; i < l; i++){ |
325 | 10 | if (listenersType[i] === listener){ |
326 | 10 | listenersType.splice(i, 1); |
327 | 10 | break; |
328 | | } |
329 | | } |
330 | | } |
331 | | }, |
332 | | |
333 | | //fix pos if event.raw have \n |
334 | | fixPos: function(event, index){ |
335 | 8 | var text = event.raw.substr(0, index); |
336 | 8 | var arrLines = text.split(/\r?\n/), |
337 | | lineCount = arrLines.length - 1, |
338 | | line = event.line, col; |
339 | 8 | if(lineCount > 0){ |
340 | 2 | line += lineCount; |
341 | 2 | col = arrLines[lineCount].length + 1; |
342 | | } |
343 | | else{ |
344 | 6 | col = event.col + index; |
345 | | } |
346 | 8 | return { |
347 | | line: line, |
348 | | col: col |
349 | | }; |
350 | | }, |
351 | | |
352 | | // covert array type of attrs to map |
353 | | getMapAttrs: function(arrAttrs){ |
354 | 6 | var mapAttrs = {}, |
355 | | attr; |
356 | 6 | for(var i=0,l=arrAttrs.length;i<l;i++){ |
357 | 6 | attr = arrAttrs[i]; |
358 | 6 | mapAttrs[attr.name] = attr.value; |
359 | | } |
360 | 6 | return mapAttrs; |
361 | | } |
362 | | }; |
363 | | |
364 | 1 | return HTMLParser; |
365 | | |
366 | | })(); |
367 | | |
368 | 1 | if (typeof exports === 'object' && exports){ |
369 | 1 | exports.HTMLParser = HTMLParser; |
370 | | } |
371 | | /** |
372 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
373 | | * MIT Licensed |
374 | | */ |
375 | 1 | HTMLHint.addRule({ |
376 | | id: 'attr-lowercase', |
377 | | description: 'Attribute name must be lowercase.', |
378 | | init: function(parser, reporter){ |
379 | 5 | var self = this; |
380 | 5 | parser.addListener('tagstart', function(event){ |
381 | 9 | var attrs = event.attrs, |
382 | | attr, |
383 | | col = event.col + event.tagName.length + 1; |
384 | 9 | for(var i=0, l=attrs.length;i<l;i++){ |
385 | 15 | attr = attrs[i]; |
386 | 15 | var attrName = attr.name; |
387 | 15 | if(attrName !== attrName.toLowerCase()){ |
388 | 4 | 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 | | */ |
398 | 1 | HTMLHint.addRule({ |
399 | | id: 'attr-no-duplication', |
400 | | description: 'Attribute name can not been duplication.', |
401 | | init: function(parser, reporter){ |
402 | 5 | var self = this; |
403 | 5 | parser.addListener('tagstart', function(event){ |
404 | 9 | var attrs = event.attrs; |
405 | 9 | var attr; |
406 | 9 | var attrName; |
407 | 9 | var col = event.col + event.tagName.length + 1; |
408 | | |
409 | 9 | var mapAttrName = {}; |
410 | 9 | for(var i=0, l=attrs.length;i<l;i++){ |
411 | 16 | attr = attrs[i]; |
412 | 16 | attrName = attr.name; |
413 | 16 | if(mapAttrName[attrName] === true){ |
414 | 3 | reporter.error('The name of attribute [ '+attr.name+' ] been duplication.', event.line, col + attr.index, self, attr.raw); |
415 | | } |
416 | 16 | mapAttrName[attrName] = true; |
417 | | } |
418 | | }); |
419 | | } |
420 | | }); |
421 | | /** |
422 | | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> |
423 | | * MIT Licensed |
424 | | */ |
425 | 1 | HTMLHint.addRule({ |
426 | | id: 'attr-unsafe-chars', |
427 | | description: 'Attribute value cant not use unsafe chars.', |
428 | | init: function(parser, reporter){ |
429 | 2 | var self = this; |
430 | 2 | parser.addListener('tagstart', function(event){ |
431 | 2 | var attrs = event.attrs, |
432 | | attr, |
433 | | col = event.col + event.tagName.length + 1; |
434 | 2 | var regUnsafe = /[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/; |
435 | 2 | for(var i=0, l=attrs.length;i<l;i++){ |
436 | 2 | attr = attrs[i]; |
437 | 2 | if(regUnsafe.test(attr.value) === true){ |
438 | 1 | 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 | | */ |
448 | 1 | HTMLHint.addRule({ |
449 | | id: 'attr-value-double-quotes', |
450 | | description: 'Attribute value must closed by double quotes.', |
451 | | init: function(parser, reporter){ |
452 | 5 | var self = this; |
453 | 5 | parser.addListener('tagstart', function(event){ |
454 | 9 | var attrs = event.attrs, |
455 | | attr, |
456 | | col = event.col + event.tagName.length + 1; |
457 | 9 | for(var i=0, l=attrs.length;i<l;i++){ |
458 | 19 | attr = attrs[i]; |
459 | 19 | if((attr.value !== '' && attr.quote !== '"') || |
460 | | (attr.value === '' && attr.quote === "'")){ |
461 | 5 | 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 | | */ |
471 | 1 | HTMLHint.addRule({ |
472 | | id: 'attr-value-not-empty', |
473 | | description: 'Attribute must set value.', |
474 | | init: function(parser, reporter){ |
475 | 3 | var self = this; |
476 | 3 | parser.addListener('tagstart', function(event){ |
477 | 3 | var attrs = event.attrs, |
478 | | attr, |
479 | | col = event.col + event.tagName.length + 1; |
480 | 3 | for(var i=0, l=attrs.length;i<l;i++){ |
481 | 3 | attr = attrs[i]; |
482 | 3 | if(attr.quote === '' && attr.value === ''){ |
483 | 1 | 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 | | */ |
493 | 1 | HTMLHint.addRule({ |
494 | | id: 'csslint', |
495 | | description: 'Scan css with csslint.', |
496 | | init: function(parser, reporter, options){ |
497 | 1 | var self = this; |
498 | 1 | parser.addListener('cdata', function(event){ |
499 | 1 | if(event.tagName.toLowerCase() === 'style'){ |
500 | | |
501 | 1 | var cssVerify; |
502 | | |
503 | 1 | if(typeof exports === 'object' && require){ |
504 | 1 | cssVerify = require("csslint").CSSLint.verify; |
505 | | } |
506 | | else{ |
507 | 0 | cssVerify = CSSLint.verify; |
508 | | } |
509 | | |
510 | 1 | if(options !== undefined){ |
511 | 1 | var styleLine = event.line - 1, |
512 | | styleCol = event.col - 1; |
513 | 1 | try{ |
514 | 1 | var messages = cssVerify(event.raw, options).messages; |
515 | 1 | messages.forEach(function(error){ |
516 | 2 | var line = error.line; |
517 | 2 | 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 | | */ |
531 | 1 | HTMLHint.addRule({ |
532 | | id: 'doctype-first', |
533 | | description: 'Doctype must be first.', |
534 | | init: function(parser, reporter){ |
535 | 5 | var self = this; |
536 | 5 | var allEvent = function(event){ |
537 | 10 | if(event.type === 'start' || (event.type === 'text' && /^\s*$/.test(event.raw))){ |
538 | 5 | return; |
539 | | } |
540 | 5 | if((event.type !== 'comment' && event.long === false) || /^DOCTYPE\s+/i.test(event.content) === false){ |
541 | 4 | reporter.error('Doctype must be first.', event.line, event.col, self, event.raw); |
542 | | } |
543 | 5 | parser.removeListener('all', allEvent); |
544 | | }; |
545 | 5 | parser.addListener('all', allEvent); |
546 | | } |
547 | | }); |
548 | | /** |
549 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
550 | | * MIT Licensed |
551 | | */ |
552 | 1 | HTMLHint.addRule({ |
553 | | id: 'doctype-html5', |
554 | | description: 'Doctype must be html5.', |
555 | | init: function(parser, reporter){ |
556 | 2 | var self = this; |
557 | 2 | function onComment(event){ |
558 | 9 | if(event.long === false && event.content.toLowerCase() !== 'doctype html'){ |
559 | 1 | reporter.warn('Doctype must be html5.', event.line, event.col, self, event.raw); |
560 | | } |
561 | | } |
562 | 2 | function onTagStart(){ |
563 | 2 | parser.removeListener('comment', onComment); |
564 | 2 | parser.removeListener('tagstart', onTagStart); |
565 | | } |
566 | 2 | parser.addListener('all', onComment); |
567 | 2 | parser.addListener('tagstart', onTagStart); |
568 | | } |
569 | | }); |
570 | | /** |
571 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
572 | | * MIT Licensed |
573 | | */ |
574 | 1 | HTMLHint.addRule({ |
575 | | id: 'head-script-disabled', |
576 | | description: 'The script tag can not be used in head.', |
577 | | init: function(parser, reporter){ |
578 | 3 | var self = this; |
579 | 3 | function onTagStart(event){ |
580 | 5 | if(event.tagName.toLowerCase() === 'script'){ |
581 | 2 | reporter.warn('The script tag can not be used in head.', event.line, event.col, self, event.raw); |
582 | | } |
583 | | } |
584 | 3 | function onTagEnd(event){ |
585 | 7 | if(event.tagName.toLowerCase() === 'head'){ |
586 | 3 | parser.removeListener('tagstart', onTagStart); |
587 | 3 | parser.removeListener('tagstart', onTagEnd); |
588 | | } |
589 | | } |
590 | 3 | parser.addListener('tagstart', onTagStart); |
591 | 3 | parser.addListener('tagend', onTagEnd); |
592 | | } |
593 | | }); |
594 | | /** |
595 | | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> |
596 | | * MIT Licensed |
597 | | */ |
598 | 1 | HTMLHint.addRule({ |
599 | | id: 'href-abs-or-rel', |
600 | | description: 'Href must be absolute or relative.', |
601 | | init: function(parser, reporter, options){ |
602 | 4 | var self = this; |
603 | | |
604 | 4 | var hrefMode = options === 'abs' ? 'absolute' : 'relative'; |
605 | | |
606 | 4 | parser.addListener('tagstart', function(event){ |
607 | 16 | var attrs = event.attrs; |
608 | 16 | var attr; |
609 | 16 | var col = event.col + event.tagName.length + 1; |
610 | | |
611 | 16 | for(var i=0, l=attrs.length;i<l;i++){ |
612 | 16 | attr = attrs[i]; |
613 | 16 | if(attr.name === 'href'){ |
614 | 16 | if((hrefMode === 'absolute' && /^\w+?:/.test(attr.value) === false) || |
615 | | (hrefMode === 'relative' && /^https?:\/\//.test(attr.value) === true)){ |
616 | 4 | reporter.warn('The value of href [ '+attr.value+' ] must be '+hrefMode+'.', event.line, col + attr.index, self, attr.raw); |
617 | | } |
618 | 16 | break; |
619 | | } |
620 | | } |
621 | | }); |
622 | | } |
623 | | }); |
624 | | /** |
625 | | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> |
626 | | * MIT Licensed |
627 | | */ |
628 | 1 | HTMLHint.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){ |
632 | 17 | var self = this; |
633 | 17 | parser.addListener('tagstart', function(event){ |
634 | 17 | var attrs = event.attrs; |
635 | 17 | var attr; |
636 | 17 | var attrName; |
637 | 17 | var col = event.col + event.tagName.length + 1; |
638 | | |
639 | 17 | for(var i=0, l=attrs.length;i<l;i++){ |
640 | 20 | attr = attrs[i]; |
641 | 20 | attrName = attr.name; |
642 | 20 | if(/^(id|class)$/i.test(attrName)){ |
643 | 20 | if(/(^|[-\_])ad([-\_]|$)/i.test(attr.value)){ |
644 | 14 | 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 | | */ |
655 | 1 | HTMLHint.addRule({ |
656 | | id: 'id-class-value', |
657 | | description: 'Id and class value must meet some rules.', |
658 | | init: function(parser, reporter, options){ |
659 | 8 | var self = this; |
660 | 8 | 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; |
674 | 8 | if(typeof options === 'string'){ |
675 | 6 | rule = arrRules[options]; |
676 | | } |
677 | | else{ |
678 | 2 | rule = options; |
679 | | } |
680 | 8 | if(rule && rule.regId){ |
681 | 8 | var regId = rule.regId, |
682 | | message = rule.message; |
683 | 8 | parser.addListener('tagstart', function(event){ |
684 | 8 | var attrs = event.attrs, |
685 | | attr, |
686 | | col = event.col + event.tagName.length + 1; |
687 | 8 | for(var i=0, l1=attrs.length;i<l1;i++){ |
688 | 16 | attr = attrs[i]; |
689 | 16 | if(attr.name.toLowerCase() === 'id'){ |
690 | 8 | if(regId.test(attr.value) === false){ |
691 | 4 | reporter.warn(message, event.line, col + attr.index, self, attr.raw); |
692 | | } |
693 | | } |
694 | 16 | if(attr.name.toLowerCase() === 'class'){ |
695 | 8 | var arrClass = attr.value.split(/\s+/g), classValue; |
696 | 8 | for(var j=0, l2=arrClass.length;j<l2;j++){ |
697 | 8 | classValue = arrClass[j]; |
698 | 8 | if(classValue && regId.test(classValue) === false){ |
699 | 4 | 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 | | */ |
712 | 1 | HTMLHint.addRule({ |
713 | | id: 'id-unique', |
714 | | description: 'Id must be unique.', |
715 | | init: function(parser, reporter){ |
716 | 5 | var self = this; |
717 | 5 | var mapIdCount = {}; |
718 | 5 | parser.addListener('tagstart', function(event){ |
719 | 11 | var attrs = event.attrs, |
720 | | attr, |
721 | | id, |
722 | | col = event.col + event.tagName.length + 1; |
723 | 11 | for(var i=0, l=attrs.length;i<l;i++){ |
724 | 17 | attr = attrs[i]; |
725 | 17 | if(attr.name.toLowerCase() === 'id'){ |
726 | 8 | id = attr.value; |
727 | 8 | if(id){ |
728 | 8 | if(mapIdCount[id] === undefined){ |
729 | 5 | mapIdCount[id] = 1; |
730 | | } |
731 | | else{ |
732 | 3 | mapIdCount[id] ++; |
733 | | } |
734 | 8 | if(mapIdCount[id] > 1){ |
735 | 3 | reporter.error('Id redefinition of [ '+id+' ].', event.line, col + attr.index, self, attr.raw); |
736 | | } |
737 | | } |
738 | 8 | break; |
739 | | } |
740 | | } |
741 | | }); |
742 | | } |
743 | | }); |
744 | | /** |
745 | | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> |
746 | | * MIT Licensed |
747 | | */ |
748 | 1 | HTMLHint.addRule({ |
749 | | id: 'img-alt-require', |
750 | | description: 'Alt of img tag must be set value.', |
751 | | init: function(parser, reporter){ |
752 | 4 | var self = this; |
753 | 4 | parser.addListener('tagstart', function(event){ |
754 | 4 | if(event.tagName.toLowerCase() === 'img'){ |
755 | 4 | var attrs = event.attrs; |
756 | 4 | var haveAlt = false; |
757 | 4 | for(var i=0, l=attrs.length;i<l;i++){ |
758 | 9 | if(attrs[i].name.toLowerCase() === 'alt'){ |
759 | 2 | haveAlt = true; |
760 | 2 | break; |
761 | | } |
762 | | } |
763 | 4 | if(haveAlt === false){ |
764 | 2 | 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 | | */ |
774 | 1 | HTMLHint.addRule({ |
775 | | id: 'jshint', |
776 | | description: 'Scan script with jshint.', |
777 | | init: function(parser, reporter, options){ |
778 | 4 | var self = this; |
779 | 4 | parser.addListener('cdata', function(event){ |
780 | 4 | if(event.tagName.toLowerCase() === 'script'){ |
781 | | |
782 | 4 | var mapAttrs = parser.getMapAttrs(event.attrs), |
783 | | type = mapAttrs.type; |
784 | | |
785 | | // Only scan internal javascript |
786 | 4 | if(mapAttrs.src !== undefined || (type && /^(text\/javascript)$/i.test(type) === false)){ |
787 | 2 | return; |
788 | | } |
789 | | |
790 | 2 | var jsVerify; |
791 | | |
792 | 2 | if(typeof exports === 'object' && require){ |
793 | 2 | jsVerify = require('jshint').JSHINT; |
794 | | } |
795 | | else{ |
796 | 0 | jsVerify = JSHINT; |
797 | | } |
798 | | |
799 | 2 | if(options !== undefined){ |
800 | 2 | var styleLine = event.line - 1, |
801 | | styleCol = event.col - 1; |
802 | 2 | var code = event.raw.replace(/\t/g,' '); |
803 | 2 | try{ |
804 | 2 | var status = jsVerify(code, options); |
805 | 2 | if(status === false){ |
806 | 2 | jsVerify.errors.forEach(function(error){ |
807 | 8 | var line = error.line; |
808 | 8 | 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 | | */ |
823 | 1 | HTMLHint.addRule({ |
824 | | id: 'space-tab-mixed-disabled', |
825 | | description: 'Spaces and tabs can not mixed in front of line.', |
826 | | init: function(parser, reporter){ |
827 | 6 | var self = this; |
828 | 6 | parser.addListener('text', function(event){ |
829 | 12 | var raw = event.raw; |
830 | 12 | var reMixed = /(^|\r?\n)( +\t|\t+ )/g; |
831 | 12 | var match; |
832 | 12 | while((match = reMixed.exec(raw))){ |
833 | 3 | var fixedPos = parser.fixPos(event, match.index + match[1].length); |
834 | 3 | 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 | | */ |
843 | 1 | HTMLHint.addRule({ |
844 | | id: 'spec-char-escape', |
845 | | description: 'Special characters must be escaped.', |
846 | | init: function(parser, reporter){ |
847 | 5 | var self = this; |
848 | 5 | parser.addListener('text', function(event){ |
849 | 5 | var raw = event.raw, |
850 | | reSpecChar = /[<>]/g, |
851 | | match; |
852 | 5 | while((match = reSpecChar.exec(raw))){ |
853 | 5 | var fixedPos = parser.fixPos(event, match.index); |
854 | 5 | 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 | | */ |
863 | 1 | HTMLHint.addRule({ |
864 | | id: 'src-not-empty', |
865 | | description: 'Src of img(script,link) must set value.', |
866 | | init: function(parser, reporter){ |
867 | 6 | var self = this; |
868 | 6 | parser.addListener('tagstart', function(event){ |
869 | 35 | var tagName = event.tagName, |
870 | | attrs = event.attrs, |
871 | | attr, |
872 | | col = event.col + tagName.length + 1; |
873 | 35 | for(var i=0, l=attrs.length;i<l;i++){ |
874 | 42 | attr = attrs[i]; |
875 | 42 | 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 === ''){ |
879 | 16 | 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 | | */ |
889 | 1 | HTMLHint.addRule({ |
890 | | id: 'style-disabled', |
891 | | description: 'Style tag can not be use.', |
892 | | init: function(parser, reporter){ |
893 | 2 | var self = this; |
894 | 2 | parser.addListener('tagstart', function(event){ |
895 | 4 | if(event.tagName.toLowerCase() === 'style'){ |
896 | 1 | 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 | | */ |
905 | 1 | HTMLHint.addRule({ |
906 | | id: 'tag-pair', |
907 | | description: 'Tag must be paired.', |
908 | | init: function(parser, reporter){ |
909 | 6 | var self = this; |
910 | 6 | var stack=[], |
911 | | mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01 |
912 | 6 | parser.addListener('tagstart', function(event){ |
913 | 11 | var tagName = event.tagName.toLowerCase(); |
914 | 11 | if (mapEmptyTags[tagName] === undefined && !event.close){ |
915 | 9 | stack.push(tagName); |
916 | | } |
917 | | }); |
918 | 6 | parser.addListener('tagend', function(event){ |
919 | 5 | var tagName = event.tagName.toLowerCase(); |
920 | | //向上寻找匹配的开始标签 |
921 | 5 | for(var pos = stack.length-1;pos >= 0; pos--){ |
922 | 5 | if(stack[pos] === tagName){ |
923 | 4 | break; |
924 | | } |
925 | | } |
926 | 5 | if(pos >= 0){ |
927 | 4 | var arrTags = []; |
928 | 4 | for(var i=stack.length-1;i>pos;i--){ |
929 | 1 | arrTags.push('</'+stack[i]+'>'); |
930 | | } |
931 | 4 | if(arrTags.length > 0){ |
932 | 1 | reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, event.raw); |
933 | | } |
934 | 4 | stack.length=pos; |
935 | | } |
936 | | else{ |
937 | 1 | reporter.error('Tag must be paired, No start tag: [ ' + event.raw + ' ]', event.line, event.col, self, event.raw); |
938 | | } |
939 | | }); |
940 | 6 | parser.addListener('end', function(event){ |
941 | 6 | var arrTags = []; |
942 | 6 | for(var i=stack.length-1;i>=0;i--){ |
943 | 4 | arrTags.push('</'+stack[i]+'>'); |
944 | | } |
945 | 6 | if(arrTags.length > 0){ |
946 | 4 | 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 | | */ |
955 | 1 | HTMLHint.addRule({ |
956 | | id: 'tag-self-close', |
957 | | description: 'The empty tag must closed by self.', |
958 | | init: function(parser, reporter){ |
959 | 2 | var self = this; |
960 | 2 | var mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01 |
961 | 2 | parser.addListener('tagstart', function(event){ |
962 | 4 | var tagName = event.tagName.toLowerCase(); |
963 | 4 | if(mapEmptyTags[tagName] !== undefined){ |
964 | 4 | if(!event.close){ |
965 | 2 | 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 | | */ |
975 | 1 | HTMLHint.addRule({ |
976 | | id: 'tagname-lowercase', |
977 | | description: 'Tagname must be lowercase.', |
978 | | init: function(parser, reporter){ |
979 | 5 | var self = this; |
980 | 5 | parser.addListener('tagstart,tagend', function(event){ |
981 | 17 | var tagName = event.tagName; |
982 | 17 | if(tagName !== tagName.toLowerCase()){ |
983 | 6 | reporter.error('Tagname [ '+tagName+' ] must be lower case.', event.line, event.col, self, event.raw); |
984 | | } |
985 | | }); |
986 | | } |
987 | | }); |