Permalink
Browse files

Merged node-filter

  • Loading branch information...
1 parent 2fd461d commit f9e916f9e93b3c2c5483767f49256d6777217396 U-chriso\Chris committed Oct 27, 2010
Showing with 706 additions and 5 deletions.
  1. 0 LICENSE
  2. +1 −1 README.md
  3. 0 index.js
  4. +290 −0 lib/entities.js
  5. +90 −0 lib/filter.js
  6. +7 −1 lib/index.js
  7. +1 −1 lib/validator.js
  8. +201 −0 lib/xss.js
  9. +2 −2 package.json
  10. +114 −0 test/filter.test.js
  11. 0 test/validator.test.js
View
0 LICENSE 100644 → 100755
No changes.
View
2 README.md 100644 → 100755
@@ -18,7 +18,7 @@ To install node-validator, use [npm](http://github.com/isaacs/npm):
isAlpha()
isAlphanumeric()
isNumeric()
- isInt() //isNumeric accepts '001' but this doesn't
+ isInt() //isNumeric accepts '001' but isInt doesn't
isLowercase()
isUppercase()
isDecimal()
View
0 index.js 100644 → 100755
No changes.
View
@@ -0,0 +1,290 @@
+var entities = {
+ ' ': ' ',
+ '¡': '¡',
+ '¢': '¢',
+ '£': '£',
+ '¤': '',
+ '¥': '¥',
+ '¦': 'Š',
+ '§': '§',
+ '¨': 'š',
+ '©': '©',
+ 'ª': 'ª',
+ '«': '«',
+ '¬': '¬',
+ '­': '­',
+ '®': '®',
+ '¯': '¯',
+ '°': '°',
+ '±': '±',
+ '²': '²',
+ '³': '³',
+ '´': 'Ž',
+ 'µ': 'µ',
+ '¶': '',
+ '·': '·',
+ '¸': 'ž',
+ '¹': '¹',
+ 'º': 'º',
+ '»': '»',
+ '¼': 'Œ',
+ '½': 'œ',
+ '¾': 'Ÿ',
+ '¿': '¿',
+ 'À': 'À',
+ 'Á': 'Á',
+ 'Â': 'Â',
+ 'Ã': 'Ã',
+ 'Ä': 'Ä',
+ 'Å': 'Å',
+ 'Æ': 'Æ',
+ 'Ç': 'Ç',
+ 'È': 'È',
+ 'É': 'É',
+ 'Ê': 'Ê',
+ 'Ë': 'Ë',
+ 'Ì': 'Ì',
+ 'Í': 'Í',
+ 'Î': 'Î',
+ 'Ï': 'Ï',
+ 'Ð': 'Ð',
+ 'Ñ': 'Ñ',
+ 'Ò': 'Ò',
+ 'Ó': 'Ó',
+ 'Ô': 'Ô',
+ 'Õ': 'Õ',
+ 'Ö': 'Ö',
+ '×': '×',
+ 'Ø': 'Ø',
+ 'Ù': 'Ù',
+ 'Ú': 'Ú',
+ 'Û': 'Û',
+ 'Ü': 'Ü',
+ 'Ý': 'Ý',
+ 'Þ': 'Þ',
+ 'ß': 'ß',
+ 'à': 'à',
+ 'á': 'á',
+ 'â': 'â',
+ 'ã': 'ã',
+ 'ä': 'ä',
+ 'å': 'å',
+ 'æ': 'æ',
+ 'ç': 'ç',
+ 'è': 'è',
+ 'é': 'é',
+ 'ê': 'ê',
+ 'ë': 'ë',
+ 'ì': 'ì',
+ 'í': 'í',
+ 'î': 'î',
+ 'ï': 'ï',
+ 'ð': 'ð',
+ 'ñ': 'ñ',
+ 'ò': 'ò',
+ 'ó': 'ó',
+ 'ô': 'ô',
+ 'õ': 'õ',
+ 'ö': 'ö',
+ '÷': '÷',
+ 'ø': 'ø',
+ 'ù': 'ù',
+ 'ú': 'ú',
+ 'û': 'û',
+ 'ü': 'ü',
+ 'ý': 'ý',
+ 'þ': 'þ',
+ 'ÿ': 'ÿ',
+ '"': '"',
+ '&lt;': '<',
+ '&gt;': '>',
+ '&apos;': '&#39;',
+ '&minus;': '&#8722;',
+ '&circ;': '&#710;',
+ '&tilde;': '&#732;',
+ '&Scaron;': '&#352;',
+ '&lsaquo;': '&#8249;',
+ '&OElig;': '&#338;',
+ '&lsquo;': '&#8216;',
+ '&rsquo;': '&#8217;',
+ '&ldquo;': '&#8220;',
+ '&rdquo;': '&#8221;',
+ '&bull;': '&#8226;',
+ '&ndash;': '&#8211;',
+ '&mdash;': '&#8212;',
+ '&trade;': '&#8482;',
+ '&scaron;': '&#353;',
+ '&rsaquo;': '&#8250;',
+ '&oelig;': '&#339;',
+ '&Yuml;': '&#376;',
+ '&fnof;': '&#402;',
+ '&Alpha;': '&#913;',
+ '&Beta;': '&#914;',
+ '&Gamma;': '&#915;',
+ '&Delta;': '&#916;',
+ '&Epsilon;': '&#917;',
+ '&Zeta;': '&#918;',
+ '&Eta;': '&#919;',
+ '&Theta;': '&#920;',
+ '&Iota;': '&#921;',
+ '&Kappa;': '&#922;',
+ '&Lambda;': '&#923;',
+ '&Mu;': '&#924;',
+ '&Nu;': '&#925;',
+ '&Xi;': '&#926;',
+ '&Omicron;': '&#927;',
+ '&Pi;': '&#928;',
+ '&Rho;': '&#929;',
+ '&Sigma;': '&#931;',
+ '&Tau;': '&#932;',
+ '&Upsilon;': '&#933;',
+ '&Phi;': '&#934;',
+ '&Chi;': '&#935;',
+ '&Psi;': '&#936;',
+ '&Omega;': '&#937;',
+ '&alpha;': '&#945;',
+ '&beta;': '&#946;',
+ '&gamma;': '&#947;',
+ '&delta;': '&#948;',
+ '&epsilon;': '&#949;',
+ '&zeta;': '&#950;',
+ '&eta;': '&#951;',
+ '&theta;': '&#952;',
+ '&iota;': '&#953;',
+ '&kappa;': '&#954;',
+ '&lambda;': '&#955;',
+ '&mu;': '&#956;',
+ '&nu;': '&#957;',
+ '&xi;': '&#958;',
+ '&omicron;': '&#959;',
+ '&pi;': '&#960;',
+ '&rho;': '&#961;',
+ '&sigmaf;': '&#962;',
+ '&sigma;': '&#963;',
+ '&tau;': '&#964;',
+ '&upsilon;': '&#965;',
+ '&phi;': '&#966;',
+ '&chi;': '&#967;',
+ '&psi;': '&#968;',
+ '&omega;': '&#969;',
+ '&thetasym;': '&#977;',
+ '&upsih;': '&#978;',
+ '&piv;': '&#982;',
+ '&ensp;': '&#8194;',
+ '&emsp;': '&#8195;',
+ '&thinsp;': '&#8201;',
+ '&zwnj;': '&#8204;',
+ '&zwj;': '&#8205;',
+ '&lrm;': '&#8206;',
+ '&rlm;': '&#8207;',
+ '&sbquo;': '&#8218;',
+ '&bdquo;': '&#8222;',
+ '&dagger;': '&#8224;',
+ '&Dagger;': '&#8225;',
+ '&hellip;': '&#8230;',
+ '&permil;': '&#8240;',
+ '&prime;': '&#8242;',
+ '&Prime;': '&#8243;',
+ '&oline;': '&#8254;',
+ '&frasl;': '&#8260;',
+ '&euro;': '&#8364;',
+ '&image;': '&#8465;',
+ '&weierp;': '&#8472;',
+ '&real;': '&#8476;',
+ '&alefsym;': '&#8501;',
+ '&larr;': '&#8592;',
+ '&uarr;': '&#8593;',
+ '&rarr;': '&#8594;',
+ '&darr;': '&#8595;',
+ '&harr;': '&#8596;',
+ '&crarr;': '&#8629;',
+ '&lArr;': '&#8656;',
+ '&uArr;': '&#8657;',
+ '&rArr;': '&#8658;',
+ '&dArr;': '&#8659;',
+ '&hArr;': '&#8660;',
+ '&forall;': '&#8704;',
+ '&part;': '&#8706;',
+ '&exist;': '&#8707;',
+ '&empty;': '&#8709;',
+ '&nabla;': '&#8711;',
+ '&isin;': '&#8712;',
+ '&notin;': '&#8713;',
+ '&ni;': '&#8715;',
+ '&prod;': '&#8719;',
+ '&sum;': '&#8721;',
+ '&lowast;': '&#8727;',
+ '&radic;': '&#8730;',
+ '&prop;': '&#8733;',
+ '&infin;': '&#8734;',
+ '&ang;': '&#8736;',
+ '&and;': '&#8743;',
+ '&or;': '&#8744;',
+ '&cap;': '&#8745;',
+ '&cup;': '&#8746;',
+ '&int;': '&#8747;',
+ '&there4;': '&#8756;',
+ '&sim;': '&#8764;',
+ '&cong;': '&#8773;',
+ '&asymp;': '&#8776;',
+ '&ne;': '&#8800;',
+ '&equiv;': '&#8801;',
+ '&le;': '&#8804;',
+ '&ge;': '&#8805;',
+ '&sub;': '&#8834;',
+ '&sup;': '&#8835;',
+ '&nsub;': '&#8836;',
+ '&sube;': '&#8838;',
+ '&supe;': '&#8839;',
+ '&oplus;': '&#8853;',
+ '&otimes;': '&#8855;',
+ '&perp;': '&#8869;',
+ '&sdot;': '&#8901;',
+ '&lceil;': '&#8968;',
+ '&rceil;': '&#8969;',
+ '&lfloor;': '&#8970;',
+ '&rfloor;': '&#8971;',
+ '&lang;': '&#9001;',
+ '&rang;': '&#9002;',
+ '&loz;': '&#9674;',
+ '&spades;': '&#9824;',
+ '&clubs;': '&#9827;',
+ '&hearts;': '&#9829;',
+ '&diams;': '&#9830;'
+};
+
+exports.decode = function (str) {
+ if (!~str.indexOf('&')) return str;
+
+ //Decode literal entities
+ for (var i in entities) {
+ str = str.replace(i, entities[i]);
+ }
+
+ str = str.replace('&nbsp;', ' ');
+
+ //Decode hex entities
+ str = str.replace(/&#x(0*[0-9a-f]{2,5});?/gi, function (m, code) {
+ return String.fromCharCode(parseInt(+code, 16));
+ });
+
+ //Decode numeric entities
+ str = str.replace(/&#([0-9]{2,4});?/gi, function (m, code) {
+ return String.fromCharCode(+code);
+ });
+
+ str = str.replace('&amp;', '&');
+
+ return str;
+}
+
+exports.encode = function (str) {
+ str = str.replace('&', '&amp;');
+
+ //Encode literal entities
+ for (var i in entities) {
+ str = str.replace(entities[i], i);
+ }
+
+ return str;
+}
View
@@ -0,0 +1,90 @@
+var entities = require('./entities');
+ xss = require('./xss');
+
+var Filter = exports.Filter = function() {}
+
+Filter.prototype.modify = function(str) {
+ this.str = str;
+}
+
+Filter.prototype.sanitize = function(str) {
+ this.str = str;
+ return this;
+}
+
+//Create some aliases - may help code readability
+Filter.prototype.convert = Filter.prototype.sanitize;
+
+Filter.prototype.xss = function(is_image) {
+ this.str = xss.clean(this.str, is_image);
+ this.modify(this.str);
+ return this.str;
+}
+
+Filter.prototype.entityDecode = function() {
+ this.str = entities.decode(this.str);
+ this.modify(this.str);
+ return this.str;
+}
+
+Filter.prototype.entityEncode = function() {
+ this.str = entities.encode(this.str);
+ this.modify(this.str);
+ return this.str;
+}
+
+Filter.prototype.ltrim = function() {
+ this.str = this.str.replace(/^(?:\s|\t|\r|\n)+/g, '');
+ this.modify(this.str);
+ return this.str;
+}
+
+Filter.prototype.rtrim = function() {
+ this.str = this.str.replace(/(?:\s|\t|\r|\n)+$/g, '');
+ this.modify(this.str);
+ return this.str;
+}
+
+Filter.prototype.trim = function() {
+ this.modify(this.str.replace(/^(?:\s|\t|\r|\n)+|(?:\s|\t|\r|\n)+$/g, ''));
+ return this.str;
+}
+
+Filter.prototype.ifNull = function(replace) {
+ if (!this.str || this.str === '') {
+ this.modify(replace);
+ }
+ return this.str;
+}
+
+Filter.prototype.toFloat = function() {
+ this.modify(parseFloat(this.str));
+ return this.str;
+}
+
+Filter.prototype.toInt = function() {
+ this.modify(parseInt(this.str));
+ return this.str;
+}
+
+//Any strings with length > 0 (except for '0' and 'false') are considered true,
+//all other strings are false
+Filter.prototype.toBoolean = function() {
+ if (!this.str || this.str == '0' || this.str == 'false' || this.str == '') {
+ this.modify(false);
+ } else {
+ this.modify(true);
+ }
+ return this.str;
+}
+
+//String must be equal to '1' or 'true' to be considered true, all other strings
+//are false
+Filter.prototype.toBooleanStrict = function() {
+ if (this.str == '1' || this.str == 'true') {
+ this.modify(true);
+ } else {
+ this.modify(false);
+ }
+ return this.str;
+}
Oops, something went wrong.

0 comments on commit f9e916f

Please sign in to comment.