Skip to content

Commit bb3f115

Browse files
committed
Use full filter
1 parent faae1c3 commit bb3f115

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

Gruntfile.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = (grunt) ->
44

55
# !! Compile configurations
66
License = '/*!Copyright(c) CommentCoreLibrary (//github.com/jabbany/CommentCoreLibrary) - Licensed under the MIT License */'
7-
FilterType = "Simple" # "Comment" || "Simple"
7+
FilterType = "Comment" # "Comment" || "Simple"
88
# !! End of config area
99

1010
CSS = [

build/CommentCoreLibrary.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ Array.prototype.binsert = function(what,how){
3232
};
3333

3434
/**
35-
* Comment Filters Module Simplified (only supports modifiers & types)
35+
* Comment Filters Module
3636
* @license MIT
3737
* @author Jim Chen
3838
*/
3939
function CommentFilter(){
40+
this.rulebook = {"all":[]};
4041
this.modifiers = [];
4142
this.runtime = null;
4243
this.allowTypes = {
@@ -54,16 +55,86 @@ function CommentFilter(){
5455
}
5556
return cmt;
5657
};
58+
this.isMatchRule = function(cmtData,rule){
59+
switch(rule['operator']){
60+
case '==':if(cmtData[rule['subject']] == rule['value']){return false;};break;
61+
case '>':if(cmtData[rule['subject']] > rule['value']){return false;};break;
62+
case '<':if(cmtData[rule['subject']] < rule['value']){return false;};break;
63+
case 'range':if(cmtData[rule['subject']] > rule.value.min && cmtData[rule['subject']] < rule.value.max){return false;};break;
64+
case '!=':if(cmtData[rule['subject']] != rule.value){return false;}break;
65+
case '~':if(new RegExp(rule.value).test(cmtData[rule[subject]])){return false;}break;
66+
case '!~':if(!(new RegExp(rule.value).test(cmtData[rule[subject]]))){return false;}break;
67+
}
68+
return true;
69+
};
5770
this.beforeSend = function(cmt){
58-
return cmt;
71+
//Check with the rules upon size
72+
var cmtMode = cmt.data.mode;
73+
if(this.rulebook[cmtMode]!=null){
74+
for(var i=0;i<this.rulebook[cmtMode].length;i++){
75+
if(this.rulebook[cmtMode][i].subject == 'width' || this.rulebook[cmtMode][i].subject == 'height'){
76+
if(this.rulebook[cmtMode][i].subject == 'width'){
77+
switch(this.rulebook[cmtMode][i].operator){
78+
case '>':if(this.rulebook[cmtMode][i].value < cmt.offsetWidth)return false;break;
79+
case '<':if(this.rulebook[cmtMode][i].value > cmt.offsetWidth)return false;break;
80+
case 'range':if(this.rulebook[cmtMode][i].value.max > cmt.offsetWidth && this.rulebook[cmtMode][i].min < cmt.offsetWidth)return false;break;
81+
case '==':if(this.rulebook[cmtMode][i].value == cmt.offsetWidth)return false;break;
82+
default:break;
83+
}
84+
}else{
85+
switch(this.rulebook[cmtMode][i].operator){
86+
case '>':if(this.rulebook[cmtMode][i].value < cmt.offsetHeight)return false;break;
87+
case '<':if(this.rulebook[cmtMode][i].value > cmt.offsetHeight)return false;break;
88+
case 'range':if(this.rulebook[cmtMode][i].value.max > cmt.offsetHeight && this.rulebook[cmtMode][i].min < cmt.offsetHeight)return false;break;
89+
case '==':if(this.rulebook[cmtMode][i].value == cmt.offsetHeight)return false;break;
90+
default:break;
91+
}
92+
}
93+
}
94+
}
95+
return true;
96+
}else{return true;}
5997
}
6098
this.doValidate = function(cmtData){
6199
if(!this.allowTypes[cmtData.mode])
62100
return false;
101+
/** Create abstract cmt data **/
102+
var abstCmtData = {
103+
text:cmtData.text,
104+
mode:cmtData.mode,
105+
color:cmtData.color,
106+
size:cmtData.size,
107+
stime:cmtData.stime,
108+
hash:cmtData.hash,
109+
}
110+
if(this.rulebook[cmtData.mode] != null && this.rulebook[cmtData.mode].length > 0){
111+
for(var i=0;i<this.rulebook[cmtData.mode];i++){
112+
if(!this.isMatchRule(abstCmtData,this.rulebook[cmtData.mode][i]))
113+
return false;
114+
}
115+
}
116+
for(var i=0;i<this.rulebook[cmtData.mode];i++){
117+
if(!this.isMatchRule(abstCmtData,this.rulebook[cmtData.mode][i]))
118+
return false;
119+
}
63120
return true;
64121
};
65122
this.addRule = function(rule){
66-
123+
if(this.rulebook[rule.mode + ""] == null)
124+
this.rulebook[rule.mode + ""] = [];
125+
/** Normalize Operators **/
126+
switch(rule.operator){
127+
case 'eq':
128+
case 'equals':
129+
case '=':rule.operator='==';break;
130+
case 'ineq':rule.operator='!=';break;
131+
case 'regex':
132+
case 'matches':rule.operator='~';break;
133+
case 'notmatch':
134+
case 'iregex':rule.operator='!~';break;
135+
}
136+
this.rulebook[rule.mode].push(rule);
137+
return (this.rulebook[rule.mode].length - 1);
67138
};
68139
this.addModifier = function(f){
69140
this.modifiers.push(f);

0 commit comments

Comments
 (0)