-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed old filter system and add version to output file
- Loading branch information
Showing
4 changed files
with
110 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,109 @@ | ||
/** | ||
* Comment Filters Module | ||
* Comment Filters Module Simplified | ||
* @license MIT | ||
* @author Jim Chen | ||
*/ | ||
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; | ||
} | ||
} | ||
var CommentFilter = (function () { | ||
|
||
function _match (rule, cmtData) { | ||
var path = rule.subject.split('.'); | ||
var extracted = cmtData; | ||
while (path.length > 0) { | ||
var item = path.shift(); | ||
if (item === '') { | ||
continue; | ||
} | ||
if (extracted.hasOwnProperty(item)) { | ||
extracted = extracted[item]; | ||
} | ||
if (extracted === null || typeof extracted === 'undefined') { | ||
extracted = null; | ||
break; | ||
} | ||
} | ||
if (extracted === null) { | ||
// Null precondition implies anything | ||
return true; | ||
} | ||
switch (rule.op) { | ||
case '~': | ||
case 'regexp': | ||
return (new RegExp(rule.value)).test(extracted.toString()); | ||
case '=': | ||
case 'eq': | ||
return rule.value === extracted.toString(); | ||
case 'NOT': | ||
return !_match(rule.value, cmtData); | ||
case 'AND': | ||
if (Array.isArray(rule.value)) { | ||
return rule.value.every(function (r) { | ||
return _match(r, cmtData); | ||
}); | ||
} else { | ||
return false; | ||
} | ||
case 'OR': | ||
if (Array.isArray(rule.value)) { | ||
return rule.value.some(function (r) { | ||
return _match(r, cmtData); | ||
}); | ||
} else { | ||
return false; | ||
} | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function CommentFilter() { | ||
this.rules = []; | ||
this.modifiers = []; | ||
this.allowUnknownTypes = true; | ||
this.allowTypes = { | ||
'1': true, | ||
'2': true, | ||
'4': true, | ||
'5': true, | ||
'6': true, | ||
'7': true, | ||
'8': true, | ||
'17': true | ||
}; | ||
} | ||
|
||
CommentFilter.prototype.doModify = function (cmt) { | ||
for (var k=0; k < this.modifiers.length; k++) { | ||
cmt = this.modifiers[k](cmt); | ||
} | ||
return cmt; | ||
}; | ||
|
||
CommentFilter.prototype.beforeSend = function (cmt) { | ||
return cmt; | ||
} | ||
|
||
CommentFilter.prototype.doValidate = function (cmtData) { | ||
if (cmtData.mode.toString() in this.allowTypes && | ||
!this.allowTypes[cmtData.mode.toString()]) { | ||
return false; | ||
} | ||
return this.rules.every(function (rule) { | ||
// Decide if matched | ||
var matched = _match(rule, cmtData); | ||
return rule.mode === 'accept' ? matched : !matched; | ||
}); | ||
}; | ||
|
||
CommentFilter.prototype.addRule = function (rule) { | ||
if (rule.mode !== 'accept' && rule.mode !== 'reject') { | ||
throw new Error('Rule must be of accept type or reject type.'); | ||
} | ||
this.rules.push(rule); | ||
}; | ||
|
||
CommentFilter.prototype.addModifier = function (f) { | ||
this.modifiers.push(f); | ||
}; | ||
|
||
return CommentFilter; | ||
})(); |
This file was deleted.
Oops, something went wrong.