diff --git a/.jshintrc b/.jshintrc index 52a05cb90..ab0809f31 100644 --- a/.jshintrc +++ b/.jshintrc @@ -15,6 +15,7 @@ "browser": true, "wsh": true, "-W099": true, + "-W100": true, "predef": [ "HTMLHint", diff --git a/CHANGE.md b/CHANGE.md index f379ae8ae..0d8f0e2a6 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -9,10 +9,11 @@ add: 2. add rule: space-tab-mixed-disabled 3. add rule: id-class-ad-disabled 4. add rule: href-abs-or-rel -5. add default rule: attr-no-duplication -6. add inline ruleset support -7. add test spec: Set false to rule -8. add point: load default ruleset when use empty ruleset +5. add rule: attr-unsafe-chars +6. add default rule: attr-no-duplication +7. add inline ruleset support +8. add test spec: Set false to rule +9. add point: load default ruleset when use empty ruleset ## ver 0.9.4 (2013-9-27) diff --git a/coverage.html b/coverage.html index 352d8b0e1..2b0273d64 100644 --- a/coverage.html +++ b/coverage.html @@ -338,4 +338,4 @@ code .string { color: #5890AD } code .keyword { color: #8A6343 } code .number { color: #2F6FAD } -
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, ruleset){ |
31 | // parse inline ruleset | |
32 | 82 | 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 | 82 | if(ruleset === undefined || Object.keys(ruleset).length ===0){ |
49 | 3 | ruleset = HTMLHint.defaultRuleset; |
50 | } | |
51 | ||
52 | 82 | var parser = new HTMLParser(); |
53 | 82 | var reporter = new HTMLHint.Reporter(html.split(/\r?\n/), ruleset); |
54 | ||
55 | 82 | var rules = HTMLHint.rules, |
56 | rule; | |
57 | 82 | for (var id in ruleset){ |
58 | 106 | rule = rules[id]; |
59 | 106 | if (rule !== undefined && ruleset[id] !== false){ |
60 | 103 | rule.init(parser, reporter, ruleset[id]); |
61 | } | |
62 | } | |
63 | ||
64 | 82 | parser.parse(html); |
65 | ||
66 | 82 | 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 | 82 | var self = this; |
84 | 82 | self._init.apply(self,arguments); |
85 | }; | |
86 | ||
87 | 1 | Reporter.prototype = { |
88 | _init: function(lines, ruleset){ | |
89 | 82 | var self = this; |
90 | 82 | self.lines = lines; |
91 | 82 | self.ruleset = ruleset; |
92 | 82 | 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 | 48 | 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 | 100 | var self = this; |
109 | 100 | 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 | 107 | var self = this; |
136 | 107 | self._init.apply(self,arguments); |
137 | }; | |
138 | ||
139 | 1 | HTMLParser.prototype = { |
140 | _init: function(){ | |
141 | 107 | var self = this; |
142 | 107 | self._listeners = {}; |
143 | 107 | self._mapCdataTags = self.makeMap("script,style"); |
144 | 107 | self._arrBlocks = []; |
145 | }, | |
146 | ||
147 | makeMap: function(str){ | |
148 | 115 | var obj = {}, items = str.split(","); |
149 | 115 | for ( var i = 0; i < items.length; i++ ){ |
150 | 326 | obj[ items[i] ] = true; |
151 | } | |
152 | 115 | return obj; |
153 | }, | |
154 | ||
155 | // parse html code | |
156 | parse: function(html){ | |
157 | ||
158 | 107 | var self = this, |
159 | mapCdataTags = self._mapCdataTags; | |
160 | ||
161 | 107 | 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 | 107 | var match, matchIndex, lastIndex = 0, tagName, arrAttrs, tagCDATA, attrsCDATA, arrCDATA, lastCDATAIndex = 0, text; |
166 | 107 | var lastLineIndex = 0, line = 1; |
167 | 107 | var arrBlocks = self._arrBlocks; |
168 | ||
169 | 107 | self.fire('start', { |
170 | pos: 0, | |
171 | line: 1, | |
172 | col: 1 | |
173 | }); | |
174 | ||
175 | 107 | while((match = regTag.exec(html))){ |
176 | 257 | matchIndex = match.index; |
177 | 257 | if(matchIndex > lastIndex){//保存前面的文本或者CDATA |
178 | 77 | text = html.substring(lastIndex, matchIndex); |
179 | 77 | if(tagCDATA){ |
180 | 10 | arrCDATA.push(text); |
181 | } | |
182 | else{//文本 | |
183 | 67 | saveBlock('text', text, lastIndex); |
184 | } | |
185 | } | |
186 | 257 | lastIndex = regTag.lastIndex; |
187 | ||
188 | 257 | if((tagName = match[1])){ |
189 | 90 | 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 | 90 | if(!tagCDATA){ |
200 | //标签结束 | |
201 | 89 | saveBlock('tagend', match[0], matchIndex, { |
202 | 'tagName': tagName | |
203 | }); | |
204 | 89 | continue; |
205 | } | |
206 | } | |
207 | ||
208 | 168 | if(tagCDATA){ |
209 | 1 | arrCDATA.push(match[0]); |
210 | } | |
211 | else{ | |
212 | 167 | if((tagName = match[4])){//标签开始 |
213 | 154 | arrAttrs = []; |
214 | 154 | var attrs = match[5], |
215 | attrMatch, | |
216 | attrMatchCount = 0; | |
217 | 154 | while((attrMatch = regAttr.exec(attrs))){ |
218 | 150 | 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 | 150 | arrAttrs.push({'name': name, 'value': value, 'quote': quote, 'index': attrMatch.index, 'raw': attrMatch[0]}); |
225 | 150 | attrMatchCount += attrMatch[0].length; |
226 | } | |
227 | 154 | if(attrMatchCount === attrs.length){ |
228 | 154 | saveBlock('tagstart', match[0], matchIndex, { |
229 | 'tagName': tagName, | |
230 | 'attrs': arrAttrs, | |
231 | 'close': match[6] | |
232 | }); | |
233 | 154 | 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 | 107 | if(html.length > lastIndex){ |
254 | //结尾文本 | |
255 | 13 | text = html.substring(lastIndex, html.length); |
256 | 13 | saveBlock('text', text, lastIndex); |
257 | } | |
258 | ||
259 | 107 | self.fire('end', { |
260 | pos: lastIndex, | |
261 | line: line, | |
262 | col: lastIndex - lastLineIndex + 1 | |
263 | }); | |
264 | ||
265 | //存储区块 | |
266 | 107 | function saveBlock(type, raw, pos, data){ |
267 | 351 | var col = pos - lastLineIndex + 1; |
268 | 351 | if(data === undefined){ |
269 | 80 | data = {}; |
270 | } | |
271 | 351 | data.raw = raw; |
272 | 351 | data.pos = pos; |
273 | 351 | data.line = line; |
274 | 351 | data.col = col; |
275 | 351 | arrBlocks.push(data); |
276 | 351 | self.fire(type, data); |
277 | 351 | var lineMatch; |
278 | 351 | 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 | 145 | var _listeners = this._listeners; |
289 | 145 | var arrTypes = types.split(/[,\s]/), type; |
290 | 145 | for(var i=0, l = arrTypes.length;i<l;i++){ |
291 | 150 | type = arrTypes[i]; |
292 | 150 | if (_listeners[type] === undefined){ |
293 | 129 | _listeners[type] = []; |
294 | } | |
295 | 150 | _listeners[type].push(listener); |
296 | } | |
297 | }, | |
298 | ||
299 | // fire event | |
300 | fire: function(type, data){ | |
301 | 565 | if (data === undefined){ |
302 | 0 | data = {}; |
303 | } | |
304 | 565 | data.type = type; |
305 | 565 | var self = this, |
306 | listeners = [], | |
307 | listenersType = self._listeners[type], | |
308 | listenersAll = self._listeners['all']; | |
309 | 565 | if (listenersType !== undefined){ |
310 | 162 | listeners = listeners.concat(listenersType); |
311 | } | |
312 | 565 | if (listenersAll !== undefined){ |
313 | 137 | listeners = listeners.concat(listenersAll); |
314 | } | |
315 | 565 | for (var i = 0, l = listeners.length; i < l; i++){ |
316 | 326 | 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) 2013, Yanis Wang <yanis.wang@gmail.com> | |
423 | * MIT Licensed | |
424 | */ | |
425 | 1 | HTMLHint.addRule({ |
426 | id: 'attr-value-double-quotes', | |
427 | description: 'Attribute value must closed by double quotes.', | |
428 | init: function(parser, reporter){ | |
429 | 5 | var self = this; |
430 | 5 | parser.addListener('tagstart', function(event){ |
431 | 9 | var attrs = event.attrs, |
432 | attr, | |
433 | col = event.col + event.tagName.length + 1; | |
434 | 9 | for(var i=0, l=attrs.length;i<l;i++){ |
435 | 19 | attr = attrs[i]; |
436 | 19 | if((attr.value !== '' && attr.quote !== '"') || |
437 | (attr.value === '' && attr.quote === "'")){ | |
438 | 5 | reporter.error('The value of attribute [ '+attr.name+' ] must closed by double quotes.', event.line, col + attr.index, self, attr.raw); |
439 | } | |
440 | } | |
441 | }); | |
442 | } | |
443 | }); | |
444 | /** | |
445 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
446 | * MIT Licensed | |
447 | */ | |
448 | 1 | HTMLHint.addRule({ |
449 | id: 'attr-value-not-empty', | |
450 | description: 'Attribute must set value.', | |
451 | init: function(parser, reporter){ | |
452 | 3 | var self = this; |
453 | 3 | parser.addListener('tagstart', function(event){ |
454 | 3 | var attrs = event.attrs, |
455 | attr, | |
456 | col = event.col + event.tagName.length + 1; | |
457 | 3 | for(var i=0, l=attrs.length;i<l;i++){ |
458 | 3 | attr = attrs[i]; |
459 | 3 | if(attr.quote === '' && attr.value === ''){ |
460 | 1 | reporter.warn('The attribute [ '+attr.name+' ] must set value.', event.line, col + attr.index, self, attr.raw); |
461 | } | |
462 | } | |
463 | }); | |
464 | } | |
465 | }); | |
466 | /** | |
467 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
468 | * MIT Licensed | |
469 | */ | |
470 | 1 | HTMLHint.addRule({ |
471 | id: 'csslint', | |
472 | description: 'Scan css with csslint.', | |
473 | init: function(parser, reporter, options){ | |
474 | 1 | var self = this; |
475 | 1 | parser.addListener('cdata', function(event){ |
476 | 1 | if(event.tagName.toLowerCase() === 'style'){ |
477 | ||
478 | 1 | var cssVerify; |
479 | ||
480 | 1 | if(typeof exports === 'object' && require){ |
481 | 1 | cssVerify = require("csslint").CSSLint.verify; |
482 | } | |
483 | else{ | |
484 | 0 | cssVerify = CSSLint.verify; |
485 | } | |
486 | ||
487 | 1 | if(options !== undefined){ |
488 | 1 | var styleLine = event.line - 1, |
489 | styleCol = event.col - 1; | |
490 | 1 | try{ |
491 | 1 | var messages = cssVerify(event.raw, options).messages; |
492 | 1 | messages.forEach(function(error){ |
493 | 2 | var line = error.line; |
494 | 2 | reporter[error.type==='warning'?'warn':'error']('['+error.rule.id+'] '+error.message, styleLine + line, (line === 1 ? styleCol : 0) + error.col, self, error.evidence); |
495 | }); | |
496 | } | |
497 | catch(e){} | |
498 | } | |
499 | ||
500 | } | |
501 | }); | |
502 | } | |
503 | }); | |
504 | /** | |
505 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
506 | * MIT Licensed | |
507 | */ | |
508 | 1 | HTMLHint.addRule({ |
509 | id: 'doctype-first', | |
510 | description: 'Doctype must be first.', | |
511 | init: function(parser, reporter){ | |
512 | 5 | var self = this; |
513 | 5 | var allEvent = function(event){ |
514 | 10 | if(event.type === 'start' || (event.type === 'text' && /^\s*$/.test(event.raw))){ |
515 | 5 | return; |
516 | } | |
517 | 5 | if((event.type !== 'comment' && event.long === false) || /^DOCTYPE\s+/i.test(event.content) === false){ |
518 | 4 | reporter.error('Doctype must be first.', event.line, event.col, self, event.raw); |
519 | } | |
520 | 5 | parser.removeListener('all', allEvent); |
521 | }; | |
522 | 5 | parser.addListener('all', allEvent); |
523 | } | |
524 | }); | |
525 | /** | |
526 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
527 | * MIT Licensed | |
528 | */ | |
529 | 1 | HTMLHint.addRule({ |
530 | id: 'doctype-html5', | |
531 | description: 'Doctype must be html5.', | |
532 | init: function(parser, reporter){ | |
533 | 2 | var self = this; |
534 | 2 | function onComment(event){ |
535 | 9 | if(event.long === false && event.content.toLowerCase() !== 'doctype html'){ |
536 | 1 | reporter.warn('Doctype must be html5.', event.line, event.col, self, event.raw); |
537 | } | |
538 | } | |
539 | 2 | function onTagStart(){ |
540 | 2 | parser.removeListener('comment', onComment); |
541 | 2 | parser.removeListener('tagstart', onTagStart); |
542 | } | |
543 | 2 | parser.addListener('all', onComment); |
544 | 2 | parser.addListener('tagstart', onTagStart); |
545 | } | |
546 | }); | |
547 | /** | |
548 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
549 | * MIT Licensed | |
550 | */ | |
551 | 1 | HTMLHint.addRule({ |
552 | id: 'head-script-disabled', | |
553 | description: 'The script tag can not be used in head.', | |
554 | init: function(parser, reporter){ | |
555 | 3 | var self = this; |
556 | 3 | function onTagStart(event){ |
557 | 5 | if(event.tagName.toLowerCase() === 'script'){ |
558 | 2 | reporter.warn('The script tag can not be used in head.', event.line, event.col, self, event.raw); |
559 | } | |
560 | } | |
561 | 3 | function onTagEnd(event){ |
562 | 7 | if(event.tagName.toLowerCase() === 'head'){ |
563 | 3 | parser.removeListener('tagstart', onTagStart); |
564 | 3 | parser.removeListener('tagstart', onTagEnd); |
565 | } | |
566 | } | |
567 | 3 | parser.addListener('tagstart', onTagStart); |
568 | 3 | parser.addListener('tagend', onTagEnd); |
569 | } | |
570 | }); | |
571 | /** | |
572 | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | |
573 | * MIT Licensed | |
574 | */ | |
575 | 1 | HTMLHint.addRule({ |
576 | id: 'href-abs-or-rel', | |
577 | description: 'Href must be absolute or relative.', | |
578 | init: function(parser, reporter, options){ | |
579 | 4 | var self = this; |
580 | ||
581 | 4 | var hrefMode = options === 'abs' ? 'absolute' : 'relative'; |
582 | ||
583 | 4 | parser.addListener('tagstart', function(event){ |
584 | 16 | var attrs = event.attrs; |
585 | 16 | var attr; |
586 | 16 | var col = event.col + event.tagName.length + 1; |
587 | ||
588 | 16 | for(var i=0, l=attrs.length;i<l;i++){ |
589 | 16 | attr = attrs[i]; |
590 | 16 | if(attr.name === 'href'){ |
591 | 16 | if((hrefMode === 'absolute' && /^\w+?:/.test(attr.value) === false) || |
592 | (hrefMode === 'relative' && /^https?:\/\//.test(attr.value) === true)){ | |
593 | 4 | reporter.warn('The value of href [ '+attr.value+' ] must be '+hrefMode+'.', event.line, col + attr.index, self, attr.raw); |
594 | } | |
595 | 16 | break; |
596 | } | |
597 | } | |
598 | }); | |
599 | } | |
600 | }); | |
601 | /** | |
602 | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | |
603 | * MIT Licensed | |
604 | */ | |
605 | 1 | HTMLHint.addRule({ |
606 | id: 'id-class-ad-disabled', | |
607 | description: 'Id and class can not use ad keyword, it will blocked by adblock software.', | |
608 | init: function(parser, reporter){ | |
609 | 17 | var self = this; |
610 | 17 | parser.addListener('tagstart', function(event){ |
611 | 17 | var attrs = event.attrs; |
612 | 17 | var attr; |
613 | 17 | var attrName; |
614 | 17 | var col = event.col + event.tagName.length + 1; |
615 | ||
616 | 17 | for(var i=0, l=attrs.length;i<l;i++){ |
617 | 20 | attr = attrs[i]; |
618 | 20 | attrName = attr.name; |
619 | 20 | if(/^(id|class)$/i.test(attrName)){ |
620 | 20 | if(/(^|[-\_])ad([-\_]|$)/i.test(attr.value)){ |
621 | 14 | reporter.warn('The value of '+attrName+' can not use ad keyword.', event.line, col + attr.index, self, attr.raw); |
622 | } | |
623 | } | |
624 | } | |
625 | }); | |
626 | } | |
627 | }); | |
628 | /** | |
629 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
630 | * MIT Licensed | |
631 | */ | |
632 | 1 | HTMLHint.addRule({ |
633 | id: 'id-class-value', | |
634 | description: 'Id and class value must meet some rules.', | |
635 | init: function(parser, reporter, options){ | |
636 | 8 | var self = this; |
637 | 8 | var arrRules = { |
638 | 'underline': { | |
639 | 'regId': /^[a-z\d]+(_[a-z\d]+)*$/, | |
640 | 'message': 'Id and class value must lower case and split by underline.' | |
641 | }, | |
642 | 'dash': { | |
643 | 'regId': /^[a-z\d]+(-[a-z\d]+)*$/, | |
644 | 'message': 'Id and class value must lower case and split by dash.' | |
645 | }, | |
646 | 'hump': { | |
647 | 'regId': /^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/, | |
648 | 'message': 'Id and class value must meet hump style.' | |
649 | } | |
650 | }, rule; | |
651 | 8 | if(typeof options === 'string'){ |
652 | 6 | rule = arrRules[options]; |
653 | } | |
654 | else{ | |
655 | 2 | rule = options; |
656 | } | |
657 | 8 | if(rule && rule.regId){ |
658 | 8 | var regId = rule.regId, |
659 | message = rule.message; | |
660 | 8 | parser.addListener('tagstart', function(event){ |
661 | 8 | var attrs = event.attrs, |
662 | attr, | |
663 | col = event.col + event.tagName.length + 1; | |
664 | 8 | for(var i=0, l1=attrs.length;i<l1;i++){ |
665 | 16 | attr = attrs[i]; |
666 | 16 | if(attr.name.toLowerCase() === 'id'){ |
667 | 8 | if(regId.test(attr.value) === false){ |
668 | 4 | reporter.warn(message, event.line, col + attr.index, self, attr.raw); |
669 | } | |
670 | } | |
671 | 16 | if(attr.name.toLowerCase() === 'class'){ |
672 | 8 | var arrClass = attr.value.split(/\s+/g), classValue; |
673 | 8 | for(var j=0, l2=arrClass.length;j<l2;j++){ |
674 | 8 | classValue = arrClass[j]; |
675 | 8 | if(classValue && regId.test(classValue) === false){ |
676 | 4 | reporter.warn(message, event.line, col + attr.index, self, classValue); |
677 | } | |
678 | } | |
679 | } | |
680 | } | |
681 | }); | |
682 | } | |
683 | } | |
684 | }); | |
685 | /** | |
686 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
687 | * MIT Licensed | |
688 | */ | |
689 | 1 | HTMLHint.addRule({ |
690 | id: 'id-unique', | |
691 | description: 'Id must be unique.', | |
692 | init: function(parser, reporter){ | |
693 | 5 | var self = this; |
694 | 5 | var mapIdCount = {}; |
695 | 5 | parser.addListener('tagstart', function(event){ |
696 | 11 | var attrs = event.attrs, |
697 | attr, | |
698 | id, | |
699 | col = event.col + event.tagName.length + 1; | |
700 | 11 | for(var i=0, l=attrs.length;i<l;i++){ |
701 | 17 | attr = attrs[i]; |
702 | 17 | if(attr.name.toLowerCase() === 'id'){ |
703 | 8 | id = attr.value; |
704 | 8 | if(id){ |
705 | 8 | if(mapIdCount[id] === undefined){ |
706 | 5 | mapIdCount[id] = 1; |
707 | } | |
708 | else{ | |
709 | 3 | mapIdCount[id] ++; |
710 | } | |
711 | 8 | if(mapIdCount[id] > 1){ |
712 | 3 | reporter.error('Id redefinition of [ '+id+' ].', event.line, col + attr.index, self, attr.raw); |
713 | } | |
714 | } | |
715 | 8 | break; |
716 | } | |
717 | } | |
718 | }); | |
719 | } | |
720 | }); | |
721 | /** | |
722 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
723 | * MIT Licensed | |
724 | */ | |
725 | 1 | HTMLHint.addRule({ |
726 | id: 'img-alt-require', | |
727 | description: 'Alt of img tag must be set value.', | |
728 | init: function(parser, reporter){ | |
729 | 4 | var self = this; |
730 | 4 | parser.addListener('tagstart', function(event){ |
731 | 4 | if(event.tagName.toLowerCase() === 'img'){ |
732 | 4 | var attrs = event.attrs; |
733 | 4 | var haveAlt = false; |
734 | 4 | for(var i=0, l=attrs.length;i<l;i++){ |
735 | 9 | if(attrs[i].name.toLowerCase() === 'alt'){ |
736 | 2 | haveAlt = true; |
737 | 2 | break; |
738 | } | |
739 | } | |
740 | 4 | if(haveAlt === false){ |
741 | 2 | reporter.warn('Alt of img tag must be set value.', event.line, event.col, self, event.raw); |
742 | } | |
743 | } | |
744 | }); | |
745 | } | |
746 | }); | |
747 | /** | |
748 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
749 | * MIT Licensed | |
750 | */ | |
751 | 1 | HTMLHint.addRule({ |
752 | id: 'jshint', | |
753 | description: 'Scan script with jshint.', | |
754 | init: function(parser, reporter, options){ | |
755 | 4 | var self = this; |
756 | 4 | parser.addListener('cdata', function(event){ |
757 | 4 | if(event.tagName.toLowerCase() === 'script'){ |
758 | ||
759 | 4 | var mapAttrs = parser.getMapAttrs(event.attrs), |
760 | type = mapAttrs.type; | |
761 | ||
762 | // Only scan internal javascript | |
763 | 4 | if(mapAttrs.src !== undefined || (type && /^(text\/javascript)$/i.test(type) === false)){ |
764 | 2 | return; |
765 | } | |
766 | ||
767 | 2 | var jsVerify; |
768 | ||
769 | 2 | if(typeof exports === 'object' && require){ |
770 | 2 | jsVerify = require('jshint').JSHINT; |
771 | } | |
772 | else{ | |
773 | 0 | jsVerify = JSHINT; |
774 | } | |
775 | ||
776 | 2 | if(options !== undefined){ |
777 | 2 | var styleLine = event.line - 1, |
778 | styleCol = event.col - 1; | |
779 | 2 | var code = event.raw.replace(/\t/g,' '); |
780 | 2 | try{ |
781 | 2 | var status = jsVerify(code, options); |
782 | 2 | if(status === false){ |
783 | 2 | jsVerify.errors.forEach(function(error){ |
784 | 8 | var line = error.line; |
785 | 8 | reporter.warn(error.reason, styleLine + line, (line === 1 ? styleCol : 0) + error.character, self, error.evidence); |
786 | }); | |
787 | } | |
788 | } | |
789 | catch(e){} | |
790 | } | |
791 | ||
792 | } | |
793 | }); | |
794 | } | |
795 | }); | |
796 | /** | |
797 | * Copyright (c) 2014, Yanis Wang <yanis.wang@gmail.com> | |
798 | * MIT Licensed | |
799 | */ | |
800 | 1 | HTMLHint.addRule({ |
801 | id: 'space-tab-mixed-disabled', | |
802 | description: 'Spaces and tabs can not mixed in front of line.', | |
803 | init: function(parser, reporter){ | |
804 | 6 | var self = this; |
805 | 6 | parser.addListener('text', function(event){ |
806 | 12 | var raw = event.raw; |
807 | 12 | var reMixed = /(^|\r?\n)( +\t|\t+ )/g; |
808 | 12 | var match; |
809 | 12 | while((match = reMixed.exec(raw))){ |
810 | 3 | var fixedPos = parser.fixPos(event, match.index + match[1].length); |
811 | 3 | reporter.warn('Mixed spaces and tabs in front of line.', fixedPos.line, 1, self, event.raw); |
812 | } | |
813 | }); | |
814 | } | |
815 | }); | |
816 | /** | |
817 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
818 | * MIT Licensed | |
819 | */ | |
820 | 1 | HTMLHint.addRule({ |
821 | id: 'spec-char-escape', | |
822 | description: 'Special characters must be escaped.', | |
823 | init: function(parser, reporter){ | |
824 | 5 | var self = this; |
825 | 5 | parser.addListener('text', function(event){ |
826 | 5 | var raw = event.raw, |
827 | reSpecChar = /[<>]/g, | |
828 | match; | |
829 | 5 | while((match = reSpecChar.exec(raw))){ |
830 | 5 | var fixedPos = parser.fixPos(event, match.index); |
831 | 5 | reporter.error('Special characters must be escaped : [ '+match[0]+' ].', fixedPos.line, fixedPos.col, self, event.raw); |
832 | } | |
833 | }); | |
834 | } | |
835 | }); | |
836 | /** | |
837 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
838 | * MIT Licensed | |
839 | */ | |
840 | 1 | HTMLHint.addRule({ |
841 | id: 'src-not-empty', | |
842 | description: 'Src of img(script,link) must set value.', | |
843 | init: function(parser, reporter){ | |
844 | 6 | var self = this; |
845 | 6 | parser.addListener('tagstart', function(event){ |
846 | 35 | var tagName = event.tagName, |
847 | attrs = event.attrs, | |
848 | attr, | |
849 | col = event.col + tagName.length + 1; | |
850 | 35 | for(var i=0, l=attrs.length;i<l;i++){ |
851 | 42 | attr = attrs[i]; |
852 | 42 | if(((/^(img|script|embed|bgsound|iframe)$/.test(tagName) === true && attr.name === 'src') || |
853 | (tagName === 'link' && attr.name === 'href') || | |
854 | (tagName === 'object' && attr.name === 'data')) && | |
855 | attr.value === ''){ | |
856 | 16 | reporter.error('[ '+attr.name + '] of [ '+tagName+' ] must set value.', event.line, col + attr.index, self, attr.raw); |
857 | } | |
858 | } | |
859 | }); | |
860 | } | |
861 | }); | |
862 | /** | |
863 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
864 | * MIT Licensed | |
865 | */ | |
866 | 1 | HTMLHint.addRule({ |
867 | id: 'style-disabled', | |
868 | description: 'Style tag can not be use.', | |
869 | init: function(parser, reporter){ | |
870 | 2 | var self = this; |
871 | 2 | parser.addListener('tagstart', function(event){ |
872 | 4 | if(event.tagName.toLowerCase() === 'style'){ |
873 | 1 | reporter.warn('Style tag can not be use.', event.line, event.col, self, event.raw); |
874 | } | |
875 | }); | |
876 | } | |
877 | }); | |
878 | /** | |
879 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
880 | * MIT Licensed | |
881 | */ | |
882 | 1 | HTMLHint.addRule({ |
883 | id: 'tag-pair', | |
884 | description: 'Tag must be paired.', | |
885 | init: function(parser, reporter){ | |
886 | 6 | var self = this; |
887 | 6 | var stack=[], |
888 | mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01 | |
889 | 6 | parser.addListener('tagstart', function(event){ |
890 | 11 | var tagName = event.tagName.toLowerCase(); |
891 | 11 | if (mapEmptyTags[tagName] === undefined && !event.close){ |
892 | 9 | stack.push(tagName); |
893 | } | |
894 | }); | |
895 | 6 | parser.addListener('tagend', function(event){ |
896 | 5 | var tagName = event.tagName.toLowerCase(); |
897 | //向上寻找匹配的开始标签 | |
898 | 5 | for(var pos = stack.length-1;pos >= 0; pos--){ |
899 | 5 | if(stack[pos] === tagName){ |
900 | 4 | break; |
901 | } | |
902 | } | |
903 | 5 | if(pos >= 0){ |
904 | 4 | var arrTags = []; |
905 | 4 | for(var i=stack.length-1;i>pos;i--){ |
906 | 1 | arrTags.push('</'+stack[i]+'>'); |
907 | } | |
908 | 4 | if(arrTags.length > 0){ |
909 | 1 | reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, event.raw); |
910 | } | |
911 | 4 | stack.length=pos; |
912 | } | |
913 | else{ | |
914 | 1 | reporter.error('Tag must be paired, No start tag: [ ' + event.raw + ' ]', event.line, event.col, self, event.raw); |
915 | } | |
916 | }); | |
917 | 6 | parser.addListener('end', function(event){ |
918 | 6 | var arrTags = []; |
919 | 6 | for(var i=stack.length-1;i>=0;i--){ |
920 | 4 | arrTags.push('</'+stack[i]+'>'); |
921 | } | |
922 | 6 | if(arrTags.length > 0){ |
923 | 4 | reporter.error('Tag must be paired, Missing: [ '+ arrTags.join('') + ' ]', event.line, event.col, self, ''); |
924 | } | |
925 | }); | |
926 | } | |
927 | }); | |
928 | /** | |
929 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
930 | * MIT Licensed | |
931 | */ | |
932 | 1 | HTMLHint.addRule({ |
933 | id: 'tag-self-close', | |
934 | description: 'The empty tag must closed by self.', | |
935 | init: function(parser, reporter){ | |
936 | 2 | var self = this; |
937 | 2 | var mapEmptyTags = parser.makeMap("area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed");//HTML 4.01 |
938 | 2 | parser.addListener('tagstart', function(event){ |
939 | 4 | var tagName = event.tagName.toLowerCase(); |
940 | 4 | if(mapEmptyTags[tagName] !== undefined){ |
941 | 4 | if(!event.close){ |
942 | 2 | reporter.warn('The empty tag : [ '+tagName+' ] must closed by self.', event.line, event.col, self, event.raw); |
943 | } | |
944 | } | |
945 | }); | |
946 | } | |
947 | }); | |
948 | /** | |
949 | * Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com> | |
950 | * MIT Licensed | |
951 | */ | |
952 | 1 | HTMLHint.addRule({ |
953 | id: 'tagname-lowercase', | |
954 | description: 'Tagname must be lowercase.', | |
955 | init: function(parser, reporter){ | |
956 | 5 | var self = this; |
957 | 5 | parser.addListener('tagstart,tagend', function(event){ |
958 | 17 | var tagName = event.tagName; |
959 | 17 | if(tagName !== tagName.toLowerCase()){ |
960 | 6 | reporter.error('Tagname [ '+tagName+' ] must be lower case.', event.line, event.col, self, event.raw); |
961 | } | |
962 | }); | |
963 | } | |
964 | }); |