Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 0d3f4a746cdeb11117c1a598c2a33e442056425d 1 parent 43d4ef6
@janez89 authored
View
5 index.js
@@ -0,0 +1,5 @@
+/*
+ * Created By Janez
+ */
+
+module.exports = require(__dirname + "/lib");
View
148 lib/form.js
@@ -0,0 +1,148 @@
+
+module.exports = function(fn) {
+ var qs = require('querystring'),
+ form = {};
+
+ form.begin = function (target, opts) {
+ return '<form'+ fn.toAttr(fn.mergeObject({ action: target || '', method: 'POST'}, opts)) +'>';
+ };
+
+ form.end = function () {
+ return '</form>';
+ };
+
+ form.label = function (label, opts) {
+ return '<label'+ fn.toAttr(label)+'>'+ label +'</label>';
+ };
+
+ form.inputField = function (opts) {
+ opts = opts || {};
+ opts.type = opts.type || 'text';
+ opts.disabled = opts.disabled ? 'disabled' : null;
+ //if (typeof opts.value !== 'undefined')
+ // opts.value = fn.e(opts.value);
+ return '<input'+ fn.toAttr(opts) +' />';
+ };
+
+ form.hiddenField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'hidden', name: name, value: val }, opts) );
+ };
+
+ form.textField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'text', name: name, value: val }, opts) );
+ };
+
+ form.passwordField = function (name, val, opts) {
+ return form.inputField(fn.mergeObject({ type: 'password', name: name, value: val }, opts) );
+ };
+
+ form.textArea = function (name, val, opts) {
+ opts = opts || {};
+ opts.name = name;
+ return '<textarea'+ fn.toAttr(opts) +'>'+ val +'</textarea>';
+ };
+
+ form.dropDownList = function (name, val, elements, opts) {
+ opts = opts || {},
+ label = null,
+ value = null;
+ // empty box
+ var empty = opts.empty ?
+ '\t<option'+ fn.toAttr({ value: '', selected: !val && typeof val !== 'number' ? 'selected': null }) +'>'+ opts.empty +'</option>\n' : '';
+ delete opts.empty;
+ // detect key, value
+ if (opts.label || opts.value) {
+ label = opts.label || 'label';
+ value = opts.value || 'value';
+ delete opts.label;
+ delete opts.value;
+ }
+
+ var output = '<select'+ fn.toAttr(fn.mergeObject({name: name}, opts)) +'>\n'+ empty;
+
+ if (value) {
+ for (var i in elements) {
+ var el = elements[i];
+ if (el.hasOwnProperty(value) && el.hasOwnProperty(label)) {
+ output += '\t<option'+ fn.toAttr({ value: el[value], selected: el[value] == val ? 'selected' : null }) +'>'+ el[label] +'</option>\n';
+ }
+ }
+ } else if (Array.isArray(elements)) {
+ for (var i in elements) {
+ output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
+ }
+ } else {
+ for (var i in elements) {
+ if (elements.hasOwnProperty(i)) {
+ output += '\t<option'+ fn.toAttr({ value: i, selected: i == val ? 'selected' : null }) +'>'+ elements[i] +'</option>\n';
+ }
+ }
+ }
+
+ return output + '</select>';
+ };
+
+ form.dateSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.timeSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.dateTimeSelector = function (name, value, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.radioButtonList = function (name, value, list, opts) {
+ throw new Error('Not implemented method!');
+ };
+
+ form.checkBox = function (name, checked, opts) {
+ opts = opts || {};
+ opts.type = 'checkbox';
+ opts.name = name;
+ opts.checked = checked ? 'checked': null;
+
+ return form.inputField(opts);
+ };
+
+ form.radioButton = function (name, selected, opts) {
+ opts = opts || {};
+ opts.type = 'radio';
+ opts.name = name;
+ opts.checked = checked ? 'checked': null;
+
+ return form.inputField(opts);
+ };
+
+ form.fileField = function (name, opts) {
+ opts = opts || {};
+ opts.type = 'file';
+ opts.name = name;
+
+ return form.inputField(opts);
+ };
+
+ form.button = function (label, opts) {
+ return '<button'+ fn.toAttr(fn.mergeObject({ value: label}, opts))+' />';
+ };
+
+ form.resetButton = function (label, opts) {
+ opts = opts || {};
+ if (label)
+ opts.value = label;
+ opts.type = 'reset';
+ return form.inputField(opts);
+ };
+
+ form.submitButton = function (label, opts) {
+ opts = opts || {};
+ if (label)
+ opts.value = label;
+ opts.type = 'submit';
+ return form.inputField(opts);
+ };
+
+ return form;
+};
View
66 lib/html.js
@@ -0,0 +1,66 @@
+
+module.exports = function(fn) {
+ var qs = require('querystring'),
+ html = {};
+
+ html.el = function (name, opts) {
+ return html.beginEl(name, opts) + html.endEl(name);
+ };
+
+ html.beginEl = function (name, opts) {
+ opts = opts || {};
+
+ var innerHTML = opts && typeof opts.val !== 'undefined' ? fn.e(opts.val) : ''; // escaped value
+ delete opts.val;
+ if (opts && typeof opts.html !== 'undefined') {
+ innerHTML = opts.html;
+ delete opts.html;
+ }
+ return '<'+ name + fn.toAttr(opts) +'>'+ innerHTML;
+ };
+
+ html.endEl = function (name) {
+ return '</'+ name +'>';
+ };
+
+ // meta charset
+ html.charset = function (charset) {
+ charset = charset || 'utf-8';
+ return '<meta charset="'+ charset +'">';
+ };
+
+ html.css = function (href, opts) {
+ return '<link'+ fn.toAttr(fn.mergeObject({ href: href, rel: 'stylesheet', type: 'text/css', media: 'all'}, opts)) +' />';
+ };
+
+ html.script = function (src, opts) {
+ return '<script'+ fn.toAttr(fn.mergeObject({ src: src, type: 'text/javascript'}, opts)) +'></script>';
+ };
+
+ html.a = function(link, text, opts) {
+ return '<a'+ fn.toAttr(fn.mergeObject({ href: link }, opts)) +'>'+ text +'</a>';
+ };
+
+ html.img = function (src, alt, opts) {
+ return '<img'+ fn.toAttr(fn.mergeObject({ src: src, alt: alt }, opts)) +' />';
+ };
+
+ html.imgText = function(text, opt) {
+ opt = opt || {};
+ var fgColor = opt.fgColor || 'EFEFEF';
+ var bgColor = opt.bgColor || 'AAAAAA';
+ var w = opt.w || 291;
+ var h = opt.h || 170;
+ text = text || 'nincs kép';
+ // http://www.placehold.it/291x170/EFEFEF/AAAAAA&amp;text=nincs+kép
+ return '<img src="http://www.placehold.it/'+w+'x'+h+'/'+fgColor+'/'+bgColor+'&amp;text='+qs.escape(text)+'" />';
+ };
+
+ html.refresh = function (url, sec, opts) {
+ if (!sec)
+ sec = 5;
+ return '<meta'+ fn.toAttr(fn.mergeObject({ http: { equiv: 'refresh' }, content: sec +',URL='+ url }, opts)) +' />';
+ };
+
+ return html;
+};
View
183 lib/index.js
@@ -0,0 +1,183 @@
+(function (fn){
+ "use strict";
+
+ var qs = require('querystring');
+
+ // for escape
+ var escapeExp = /[&<>"]/,
+ escapeAmpExp = /&/g,
+ escapeLtExp = /</g,
+ escapeGtExp = />/g,
+ escapeQuotExp = /"/g,
+ regExpEscape = function (str) {
+ return String(str).replace(/([.*+?\^=!:${}()|\[\]\/\\])/g, '\\$1');
+ };
+ // --- escape ----------
+ fn.e = function (text) {
+ if (text == null) {
+ return '';
+ }
+ var result = text.toString();
+ if (!escapeExp.test(result)) {
+ return result;
+ }
+ return result.replace(escapeAmpExp, '&#38;').replace(escapeLtExp, '&#60;').replace(escapeGtExp, '&#62;').replace(escapeQuotExp, '&#34;');
+ };
+
+ // http://kevin.vanzonneveld.net - PHP style number_format
+ fn.number_format = function(number, decimals, dec_point, thousands_sep) {
+ var n = !isFinite(+number) ? 0 : +number,
+ prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
+ sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
+ dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
+ s = '',
+ toFixedFix = function (n, prec) {
+ var k = Math.pow(10, prec);
+ return '' + Math.round(n * k) / k;
+ };
+ // Fix for IE parseFloat(0.55).toFixed(0) = 0;
+ s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
+ if (s[0].length > 3) {
+ s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
+ }
+ if ((s[1] || '').length < prec) {
+ s[1] = s[1] || '';
+ s[1] += new Array(prec - s[1].length + 1).join('0');
+ }
+ return s.join(dec);
+ };
+
+ fn.numberFormat = function (number, opts) {
+ opts = opts || {};
+ if (typeof opts.decpoint === 'undefined')
+ opts.decpoint = '.';
+ if (typeof opts.sep === 'undefined')
+ opts.sep = ',';
+ if (typeof opts.decimals === 'undefined')
+ opts.decimals = 0;
+ return fn.number_format(number, opts.decimals, opts.decpoint, opts.sep);
+ };
+
+ // simple merge objects
+ fn.mergeObject = function(to, from) {
+ if (to == null || from == null)
+ return to;
+
+ for (var key in from)
+ if (from.hasOwnProperty(key)){
+ if (typeof from[key] === 'object')
+ to[key] = fn.mergeObject(to[key], from[key]);
+ else
+ to[key] = from[key];
+ }
+ return to;
+ }
+
+ // --- for FROM helpers { name: 'Foo'} -> name="Foo", { data: { id: 5 } } -> data-id="5", { fo: { bar: 'val' } } -> fo-bar="val"
+ fn.toAttr = function (Obj, escape) {
+ escape = escape || false;
+ var attributes = '';
+
+ for (var key in Obj) {
+ var val = Obj[key];
+ if ( val != null && typeof val !== 'undefined' ) {
+ if (typeof val === 'object') {
+ for (var k in val){
+ if ( val[k] != null && typeof val[k] !== 'undefined' )
+ attributes += ' '+ key +'-'+ k + '="'+ ( escape ? fn.e(val[k]) : val[k] ) +'"';
+ }
+ continue;
+ }
+
+ attributes += ' '+ key + '="'+ ( escape ? fn.e(val) : val ) +'"';
+ }
+ }
+ return attributes;
+ };
+
+ fn.repeat = function(sep, count){
+ var out = '';
+ for(var i=0; i<count; i++)
+ out += sep;
+ return out;
+ };
+
+ fn.date2Digit = function(num){
+ if (num.toString().length == 1)
+ return "0"+ num.toString();
+ return num;
+ };
+
+ // http://phpjs.org/functions/nl2br/ , http://kevin.vanzonneveld.net
+ fn.nl2br = function (str, is_xhtml) {
+ var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>'; // Adjust comment to avoid issue on phpjs.org display
+ return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
+ };
+
+ fn.htmlspecialchars = function (string, quote_style, charset, double_encode) {
+ // http://kevin.vanzonneveld.net
+ var optTemp = 0,
+ i = 0,
+ noquotes = false;
+ if (typeof quote_style === 'undefined' || quote_style === null) {
+ quote_style = 2;
+ }
+ string = string.toString();
+ if (double_encode !== false) { // Put this first to avoid double-encoding
+ string = string.replace(/&/g, '&amp;');
+ }
+ string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
+ var OPTS = {
+ 'ENT_NOQUOTES': 0,
+ 'ENT_HTML_QUOTE_SINGLE': 1,
+ 'ENT_HTML_QUOTE_DOUBLE': 2,
+ 'ENT_COMPAT': 2,
+ 'ENT_QUOTES': 3,
+ 'ENT_IGNORE': 4
+ };
+ if (quote_style === 0) {
+ noquotes = true;
+ }
+ if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
+ quote_style = [].concat(quote_style);
+ for (i = 0; i < quote_style.length; i++) {
+ // Resolve string input to bitwise e.g. 'ENT_IGNORE' becomes 4
+ if (OPTS[quote_style[i]] === 0) {
+ noquotes = true;
+ }
+ else if (OPTS[quote_style[i]]) {
+ optTemp = optTemp | OPTS[quote_style[i]];
+ }
+ }
+ quote_style = optTemp;
+ }
+ if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
+ string = string.replace(/'/g, '&#039;');
+ }
+ if (!noquotes) {
+ string = string.replace(/"/g, '&quot;');
+ }
+
+ return string;
+ };
+
+ fn.strip_tags = function (input, allowed) {
+ // http://kevin.vanzonneveld.net
+ allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
+ var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
+ commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
+ return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
+ return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
+ });
+ }
+
+
+ // --- load more plugins ----------------------
+ require('fs').readdirSync(__dirname).forEach(function (file) {
+ var name = file.substr(0, file.indexOf('.'));
+ if (name.length && name !== 'index')
+ fn[name] = require(__dirname + '/'+ name)(fn);
+ });
+
+})(module.exports);
View
66 test/default.test.js
@@ -0,0 +1,66 @@
+var assert = require("assert"),
+ helper = require("../index")
+
+describe('#default', function(){
+ describe('escape', function () {
+ it('should escape <script>', function(){
+ assert.equal(helper.e('<script>'), '&#60;script&#62;')
+ })
+ })
+
+ describe('Object To Attributes', function () {
+ it('should convert', function() {
+ assert.equal(helper.toAttr({ type: "text", value: "value"}), ' type="text" value="value"')
+ })
+ it('should use html5 data', function() {
+ assert.equal(helper.toAttr({ data: { id: 1, src: 'src'}}), ' data-id="1" data-src="src"')
+ assert.equal(helper.toAttr({ 'data-id': 1, 'data-src': 'src'}), ' data-id="1" data-src="src"')
+ })
+ })
+
+ describe('Merge Objects', function () {
+ it('should marge', function(){
+ var obj1 = { name: 'Name', user: 'u'}
+ var obj2 = { age: 20, user: 'w'}
+ var merged = helper.mergeObject(obj1, obj2)
+
+ assert.equal(merged.name, 'Name')
+ assert.equal(merged.age, 20)
+ assert.equal(merged.user, 'w')
+ })
+ })
+
+ describe('number format', function () {
+ it('should 10,000', function () {
+ assert.equal(helper.numberFormat(10000), '10,000')
+ })
+ it('should 10,000,000', function () {
+ assert.equal(helper.numberFormat(10000000), '10,000,000')
+ })
+ it('should 10 000 000 with space separator', function () {
+ assert.equal(helper.numberFormat(10000000, { sep: ' '}), '10 000 000')
+ })
+ it('should 10,000.10 with 2 decimals', function () {
+ assert.equal(helper.numberFormat(10000.10, { decimals: 2}), '10,000.10')
+ })
+ })
+
+ describe('string format', function () {
+ it('should nl2br', function () {
+ assert.equal(helper.nl2br('\r\n'), '<br />\r\n')
+ assert.equal(helper.nl2br('\n\r'), '<br />\n\r')
+ assert.equal(helper.nl2br('\r'), '<br />\r')
+ assert.equal(helper.nl2br('\n'), '<br />\n')
+ })
+ it('should repeat', function () {
+ assert.equal(helper.repeat('-', 5), '-----')
+ })
+ it('sholud string tags', function () {
+ assert.equal(helper.strip_tags('<p>text <a href="url">link</a></p>', '<p>'), '<p>text link</p>')
+ assert.equal(helper.strip_tags('<p>text <a href="url">link</a></p>'), 'text link')
+ })
+ it('should use html special chars', function () {
+ assert.equal(helper.htmlspecialchars('<a href="link">label</a>', 'ENT_QUOTES'), '&lt;a href=&quot;link&quot;&gt;label&lt;/a&gt;')
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.