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