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