github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/helpers/handlebars.js (about) 1 'use strict'; 2 /*jslint eqeq: true*/ 3 4 Handlebars.registerHelper('sanitize', function (text) { 5 var result; 6 7 if (text === undefined) { return ''; } 8 9 result = sanitizeHtml(text, { 10 allowedTags: [ 'div', 'span', 'b', 'i', 'em', 'strong', 'a', 'br', 'table', 'tbody', 'tr', 'th', 'td' ], 11 allowedAttributes: { 12 'div': [ 'class' ], 13 'span': [ 'class' ], 14 'table': [ 'class' ], 15 'td': [ 'class' ], 16 'th': [ 'colspan' ], 17 'a': [ 'href' ] 18 } 19 }); 20 21 return new Handlebars.SafeString(result); 22 }); 23 24 Handlebars.registerHelper('renderTextParam', function(param) { 25 var result, type = 'text', idAtt = ''; 26 var paramType = param.type || param.schema && param.schema.type || ''; 27 var isArray = paramType.toLowerCase() === 'array' || param.allowMultiple; 28 var defaultValue = isArray && Array.isArray(param.default) ? param.default.join('\n') : param.default; 29 var name = Handlebars.Utils.escapeExpression(param.name); 30 var valueId = Handlebars.Utils.escapeExpression(param.valueId); 31 paramType = Handlebars.Utils.escapeExpression(paramType); 32 33 var dataVendorExtensions = Object.keys(param).filter(function(property) { 34 // filter X-data- properties 35 return property.match(/^X-data-/i) !== null; 36 }).reduce(function(result, property) { 37 // remove X- from property name, so it results in html attributes like data-foo='bar' 38 return result += ' ' + property.substring(2, property.length) + '=\'' + param[property] + '\''; 39 }, ''); 40 41 if(param.format && param.format === 'password') { 42 type = 'password'; 43 } 44 45 if(valueId) { 46 idAtt = ' id=\'' + valueId + '\''; 47 } 48 49 if (defaultValue) { 50 defaultValue = sanitizeHtml(defaultValue); 51 } else { 52 defaultValue = ''; 53 } 54 55 if(isArray) { 56 result = '<textarea class=\'body-textarea' + (param.required ? ' required' : '') + '\' name=\'' + name + '\'' + idAtt + dataVendorExtensions; 57 result += ' placeholder=\'Provide multiple values in new lines' + (param.required ? ' (at least one required).' : '.') + '\'>'; 58 result += defaultValue + '</textarea>'; 59 } else { 60 var parameterClass = 'parameter'; 61 if(param.required) { 62 parameterClass += ' required'; 63 } 64 result = '<input class=\'' + parameterClass + '\' minlength=\'' + (param.required ? 1 : 0) + '\''; 65 result += ' name=\'' + name +'\' placeholder=\'' + (param.required ? '(required)' : '') + '\'' + idAtt + dataVendorExtensions; 66 result += ' type=\'' + type + '\' value=\'' + defaultValue + '\'/>'; 67 } 68 return new Handlebars.SafeString(result); 69 }); 70 71 Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { 72 73 switch (operator) { 74 case '==': 75 return (v1 == v2) ? options.fn(this) : options.inverse(this); 76 case '===': 77 return (v1 === v2) ? options.fn(this) : options.inverse(this); 78 case '<': 79 return (v1 < v2) ? options.fn(this) : options.inverse(this); 80 case '<=': 81 return (v1 <= v2) ? options.fn(this) : options.inverse(this); 82 case '>': 83 return (v1 > v2) ? options.fn(this) : options.inverse(this); 84 case '>=': 85 return (v1 >= v2) ? options.fn(this) : options.inverse(this); 86 case '&&': 87 return (v1 && v2) ? options.fn(this) : options.inverse(this); 88 case '||': 89 return (v1 || v2) ? options.fn(this) : options.inverse(this); 90 default: 91 return options.inverse(this); 92 } 93 }); 94 95 Handlebars.registerHelper('escape', function (value) { 96 var text = Handlebars.Utils.escapeExpression(value); 97 98 return new Handlebars.SafeString(text); 99 });