-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
id-class-value.js
57 lines (57 loc) · 2.15 KB
/
id-class-value.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Copyright (c) 2013, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
HTMLHint.addRule({
id: 'id-class-value',
description: 'Id and class value must meet some rules.',
init: function(parser, reporter, options){
var self = this;
var arrRules = {
'underline': {
'regId': /^[a-z\d]+(_[a-z\d]+)*$/,
'message': 'Id and class value must lower case and split by underline.'
},
'dash': {
'regId': /^[a-z\d]+(-[a-z\d]+)*$/,
'message': 'Id and class value must lower case and split by dash.'
},
'hump': {
'regId': /^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,
'message': 'Id and class value must meet hump style.'
}
}, rule;
if(typeof options === 'string'){
rule = arrRules[options];
}
else{
rule = options;
}
if(rule && rule.regId){
var regId = rule.regId,
message = rule.message;
parser.addListener('tagstart', function(event){
var attrs = event.attrs,
attr,
col = event.col + event.tagName.length + 1;
for(var i=0, l1=attrs.length;i<l1;i++){
attr = attrs[i];
if(attr.name.toLowerCase() === 'id'){
if(regId.test(attr.value) === false){
reporter.error(message, event.line, col + attr.index, self, attr.raw);
}
}
if(attr.name.toLowerCase() === 'class'){
var arrClass = attr.value.split(/\s+/g), classValue;
for(var j=0, l2=arrClass.length;j<l2;j++){
classValue = arrClass[j];
if(classValue && regId.test(classValue) === false){
reporter.error(message, event.line, col + attr.index, self, classValue);
}
}
}
}
});
}
}
});