11'use strict' ;
22/*jslint eqeq: true*/
33
4- Handlebars . registerHelper ( 'sanitize' , function ( html ) {
5- // Strip the script tags from the html, and return it as a Handlebars.SafeString
4+ var _sanitize = function ( html ) {
5+ // Strip the script tags from the html and inline evenhandlers
66 html = html . replace ( / < s c r i p t \b [ ^ < ] * (?: (? ! < \/ s c r i p t > ) < [ ^ < ] * ) * < \/ s c r i p t > / gi, '' ) ;
7- return new Handlebars . SafeString ( html ) ;
8- } ) ;
7+ html = html . replace ( / ( o n \w + = " [ ^ " ] * " ) * ( o n \w + = ' [ ^ ' ] * ' ) * ( o n \w + = \w * \( \w * \) ) * / gi, '' ) ;
8+
9+ return html ;
10+ } ;
11+
12+ var sanitize = function ( html ) {
13+ var _html ;
14+
15+ if ( _ . isUndefined ( html ) || _ . isNull ( html ) ) {
16+ return new Handlebars . SafeString ( '' ) ;
17+ }
18+
19+ if ( _ . isNumber ( html ) ) {
20+ return new Handlebars . SafeString ( html ) ;
21+ }
22+
23+ if ( _ . isObject ( html ) ) {
24+ _html = JSON . stringify ( html ) ;
25+ return new Handlebars . SafeString ( JSON . parse ( _sanitize ( _html ) ) ) ;
26+ }
27+
28+ return new Handlebars . SafeString ( _sanitize ( html ) ) ;
29+ } ;
30+
31+ Handlebars . registerHelper ( 'sanitize' , sanitize ) ;
932
1033Handlebars . registerHelper ( 'renderTextParam' , function ( param ) {
1134 var result , type = 'text' , idAtt = '' ;
1235 var paramType = param . type || param . schema && param . schema . type || '' ;
1336 var isArray = paramType . toLowerCase ( ) === 'array' || param . allowMultiple ;
1437 var defaultValue = isArray && Array . isArray ( param . default ) ? param . default . join ( '\n' ) : param . default ;
38+ var name = Handlebars . Utils . escapeExpression ( param . name ) ;
39+ var valueId = Handlebars . Utils . escapeExpression ( param . valueId ) ;
40+ paramType = Handlebars . Utils . escapeExpression ( paramType ) ;
1541
1642 var dataVendorExtensions = Object . keys ( param ) . filter ( function ( property ) {
1743 // filter X-data- properties
@@ -21,24 +47,18 @@ Handlebars.registerHelper('renderTextParam', function(param) {
2147 return result += ' ' + property . substring ( 2 , property . length ) + '=\'' + param [ property ] + '\'' ;
2248 } , '' ) ;
2349
24- if ( typeof defaultValue === 'undefined' ) {
25- defaultValue = '' ;
26- }
27-
2850 if ( param . format && param . format === 'password' ) {
2951 type = 'password' ;
3052 }
3153
32- if ( param . valueId ) {
33- idAtt = ' id=\'' + param . valueId + '\'' ;
54+ if ( valueId ) {
55+ idAtt = ' id=\'' + valueId + '\'' ;
3456 }
3557
36- if ( typeof defaultValue === 'string' || defaultValue instanceof String ) {
37- defaultValue = defaultValue . replace ( / ' / g, ''' ) ;
38- }
58+ defaultValue = sanitize ( defaultValue ) ;
3959
4060 if ( isArray ) {
41- result = '<textarea class=\'body-textarea' + ( param . required ? ' required' : '' ) + '\' name=\'' + param . name + '\'' + idAtt + dataVendorExtensions ;
61+ result = '<textarea class=\'body-textarea' + ( param . required ? ' required' : '' ) + '\' name=\'' + name + '\'' + idAtt + dataVendorExtensions ;
4262 result += ' placeholder=\'Provide multiple values in new lines' + ( param . required ? ' (at least one required).' : '.' ) + '\'>' ;
4363 result += defaultValue + '</textarea>' ;
4464 } else {
@@ -47,7 +67,7 @@ Handlebars.registerHelper('renderTextParam', function(param) {
4767 parameterClass += ' required' ;
4868 }
4969 result = '<input class=\'' + parameterClass + '\' minlength=\'' + ( param . required ? 1 : 0 ) + '\'' ;
50- result += ' name=\'' + param . name + '\' placeholder=\'' + ( param . required ? '(required)' : '' ) + '\'' + idAtt + dataVendorExtensions ;
70+ result += ' name=\'' + name + '\' placeholder=\'' + ( param . required ? '(required)' : '' ) + '\'' + idAtt + dataVendorExtensions ;
5171 result += ' type=\'' + type + '\' value=\'' + defaultValue + '\'/>' ;
5272 }
5373 return new Handlebars . SafeString ( result ) ;
@@ -76,3 +96,9 @@ Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
7696 return options . inverse ( this ) ;
7797 }
7898} ) ;
99+
100+ Handlebars . registerHelper ( 'escape' , function ( value ) {
101+ var text = Handlebars . Utils . escapeExpression ( value ) ;
102+
103+ return new Handlebars . SafeString ( text ) ;
104+ } ) ;
0 commit comments