forked from jabbany/CommentCoreLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentFilter.js
More file actions
117 lines (116 loc) · 3.87 KB
/
CommentFilter.js
File metadata and controls
117 lines (116 loc) · 3.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
Comment Filters/Filter Lang
Licensed Under MIT License
**/
function CommentFilter(){
this.rulebook = {"all":[]};
this.modifiers = [];
this.runtime = null;
this.allowTypes = {
"1":true,
"4":true,
"5":true,
"6":true,
"7":true,
"8":true,
"17":true
};
this.doModify = function(cmt){
for(var k=0;k<this.modifiers.length;k++){
cmt = this.modifiers[k](cmt);
}
return cmt;
};
this.isMatchRule = function(cmtData,rule){
switch(rule['operator']){
case '==':if(cmtData[rule['subject']] == rule['value']){return false;};break;
case '>':if(cmtData[rule['subject']] > rule['value']){return false;};break;
case '<':if(cmtData[rule['subject']] < rule['value']){return false;};break;
case 'range':if(cmtData[rule['subject']] > rule.value.min && cmtData[rule['subject']] < rule.value.max){return false;};break;
case '!=':if(cmtData[rule['subject']] != rule.value){return false;}break;
case '~':if(new RegExp(rule.value).test(cmtData[rule[subject]])){return false;}break;
case '!~':if(!(new RegExp(rule.value).test(cmtData[rule[subject]]))){return false;}break;
}
return true;
};
this.beforeSend = function(cmt){
//Check with the rules upon size
var cmtMode = cmt.data.mode;
if(this.rulebook[cmtMode]!=null){
for(var i=0;i<this.rulebook[cmtMode].length;i++){
if(this.rulebook[cmtMode][i].subject == 'width' || this.rulebook[cmtMode][i].subject == 'height'){
if(this.rulebook[cmtMode][i].subject == 'width'){
switch(this.rulebook[cmtMode][i].operator){
case '>':if(this.rulebook[cmtMode][i].value < cmt.offsetWidth)return false;break;
case '<':if(this.rulebook[cmtMode][i].value > cmt.offsetWidth)return false;break;
case 'range':if(this.rulebook[cmtMode][i].value.max > cmt.offsetWidth && this.rulebook[cmtMode][i].min < cmt.offsetWidth)return false;break;
case '==':if(this.rulebook[cmtMode][i].value == cmt.offsetWidth)return false;break;
default:break;
}
}else{
switch(this.rulebook[cmtMode][i].operator){
case '>':if(this.rulebook[cmtMode][i].value < cmt.offsetHeight)return false;break;
case '<':if(this.rulebook[cmtMode][i].value > cmt.offsetHeight)return false;break;
case 'range':if(this.rulebook[cmtMode][i].value.max > cmt.offsetHeight && this.rulebook[cmtMode][i].min < cmt.offsetHeight)return false;break;
case '==':if(this.rulebook[cmtMode][i].value == cmt.offsetHeight)return false;break;
default:break;
}
}
}
}
return true;
}else{return true;}
}
this.doValidate = function(cmtData){
if(!this.allowTypes[cmtData.mode])
return false;
/** Create abstract cmt data **/
var abstCmtData = {
text:cmtData.text,
mode:cmtData.mode,
color:cmtData.color,
size:cmtData.size,
stime:cmtData.stime,
hash:cmtData.hash,
}
if(this.rulebook[cmtData.mode] != null && this.rulebook[cmtData.mode].length > 0){
for(var i=0;i<this.rulebook[cmtData.mode];i++){
if(!this.isMatchRule(abstCmtData,this.rulebook[cmtData.mode][i]))
return false;
}
}
for(var i=0;i<this.rulebook[cmtData.mode];i++){
if(!this.isMatchRule(abstCmtData,this.rulebook[cmtData.mode][i]))
return false;
}
return true;
};
this.addRule = function(rule){
if(this.rulebook[rule.mode + ""] == null)
this.rulebook[rule.mode + ""] = [];
/** Normalize Operators **/
switch(rule.operator){
case 'eq':
case 'equals':
case '=':rule.operator='==';break;
case 'ineq':rule.operator='!=';break;
case 'regex':
case 'matches':rule.operator='~';break;
case 'notmatch':
case 'iregex':rule.operator='!~';break;
}
this.rulebook[rule.mode].push(rule);
return (this.rulebook[rule.mode].length - 1);
};
this.addModifier = function(f){
this.modifiers.push(f);
};
this.runtimeFilter = function(cmt){
if(this.runtime == null)
return cmt;
return this.runtime(cmt);
};
this.setRuntimeFilter = function(f){
this.runtime = f;
}
}