-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.js
57 lines (48 loc) · 1.3 KB
/
rules.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
const utils = require('./utils');
class BaseRule {
constructor(msgBuilder) {
this.msgBuilder = msgBuilder;
}
check(dom) {
return this.msgBuilder();
}
}
class TagWithoutAttributeRule extends BaseRule {
constructor(msgBuilder, tag, missingAttrs) {
super(msgBuilder);
this.tag = tag;
this.missingAttrs = missingAttrs;
}
check(dom) {
var missingAttr = Array.isArray(this.missingAttrs) ? this.missingAttrs.join(', ') : this.missingAttrs;
var count = dom(`${this.tag}:not([${missingAttr}])`).length;
return count > 0 ? utils.error(this.msgBuilder(count)) : utils.success;
}
}
class TagCountExceedThresholdRule extends BaseRule {
constructor(msgBuilder, tag, threshold) {
super(msgBuilder);
this.tag = tag;
this.threshold = threshold;
}
check(dom) {
var count = dom(this.tag).length;
return count > this.threshold ? utils.error(this.msgBuilder(count)) : utils.success;
}
}
class TagCountRule extends BaseRule {
constructor(msgBuilder, findTagExp, evaluator) {
super(msgBuilder);
this.findTagExp = findTagExp;
this.evaluator = evaluator;
}
check(dom) {
var count = dom(this.findTagExp).length;
return this.evaluator(count) ? utils.error(this.msgBuilder(count)) : utils.success;
}
}
module.exports = {
TagCountRule,
TagWithoutAttributeRule,
TagCountExceedThresholdRule
};