Repository navigation
Expand file tree
/
Copy pathid-class-value.js
More file actions
73 lines (73 loc) · 2.15 KB
/
Copy pathid-class-value.js
File metadata and controls
73 lines (73 loc) · 2.15 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
export default {
id: 'id-class-value',
description:
'The id and class attribute values must meet the specified rules.',
init: function(parser, reporter, options) {
var self = this;
var arrRules = {
underline: {
regId: /^[a-z\d]+(_[a-z\d]+)*$/,
message:
'The id and class attribute values must be in lowercase and split by an underscore.'
},
dash: {
regId: /^[a-z\d]+(-[a-z\d]+)*$/,
message:
'The id and class attribute values must be in lowercase and split by a dash.'
},
hump: {
regId: /^[a-z][a-zA-Z\d]*([A-Z][a-zA-Z\d]*)*$/,
message:
'The id and class attribute values must meet the camelCase style.'
}
},
rule;
if (typeof options === 'string') {
rule = arrRules[options];
} else {
rule = options;
}
if (rule && rule.regId) {
var regId = rule.regId,
message = rule.message;
if (!(regId instanceof RegExp)) {
regId = new RegExp(regId);
}
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.warn(
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.warn(
message,
event.line,
col + attr.index,
self,
classValue
);
}
}
}
}
});
}
}
};