|
28 | 28 |
|
29 | 29 | 'use strict'; |
30 | 30 |
|
31 | | - $.formUtils.registerLoadedModule('sanitize'); |
32 | | - |
33 | | - var inputsThatCantBeSanitized = '[type="button"], [type="submit"], [type="radio"], [type="checkbox"], [type="reset"], [type="search"]', |
34 | | - sanitizeCommands = { |
35 | | - upper : function(val) { |
36 | | - return val.toLocaleUpperCase(); |
37 | | - }, |
38 | | - lower : function(val) { |
39 | | - return val.toLocaleLowerCase(); |
40 | | - }, |
41 | | - trim : function(val) { |
42 | | - return $.trim(val); |
43 | | - }, |
44 | | - trimLeft : function(val) { |
45 | | - return val.replace(/^\s+/,''); |
46 | | - }, |
47 | | - trimRight : function(val) { |
48 | | - return val.replace(/\s+$/,''); |
49 | | - }, |
50 | | - capitalize : function(val) { |
51 | | - var words = val.split(' '); |
| 31 | + $.formUtils.addSanitizer({ |
| 32 | + name : 'upper', |
| 33 | + sanitizerFunction : function(val) { |
| 34 | + return val.toLocaleUpperCase(); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + $.formUtils.addSanitizer({ |
| 39 | + name : 'lower', |
| 40 | + sanitizerFunction : function(val) { |
| 41 | + return val.toLocaleLowerCase(); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + $.formUtils.addSanitizer({ |
| 46 | + name : 'trim', |
| 47 | + sanitizerFunction : function(val) { |
| 48 | + return $.trim(val); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + $.formUtils.addSanitizer({ |
| 53 | + name : 'trimLeft', |
| 54 | + sanitizerFunction : function(val) { |
| 55 | + return val.replace(/^\s+/,''); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + $.formUtils.addSanitizer({ |
| 60 | + name : 'trimRight', |
| 61 | + sanitizerFunction : function(val) { |
| 62 | + return val.replace(/\s+$/,''); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + $.formUtils.addSanitizer({ |
| 67 | + name : 'capitalize', |
| 68 | + sanitizerFunction : function(val) { |
| 69 | + var words = val.split(' '); |
52 | 70 | $.each(words, function(i, word) { |
53 | 71 | words[i] = word.substr(0,1).toUpperCase() + word.substr(1, word.length); |
54 | 72 | }); |
55 | 73 | return words.join(' '); |
56 | | - }, |
57 | | - insert : function(val, $input, pos) { |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + $.formUtils.addSanitizer({ |
| 78 | + name : 'insert', |
| 79 | + sanitizerFunction : function(val, $input, pos) { |
58 | 80 | var extra = ($input.attr('data-sanitize-insert-'+pos) || '').replace(/\[SPACE\]/g, ' '); |
59 | 81 | if ( (pos === 'left' && val.indexOf(extra) === 0) || (pos === 'right' && val.substring(val.length - extra.length) === extra)) { |
60 | 82 | return val; |
61 | 83 | } |
62 | 84 | return (pos === 'left' ? extra:'') + val + (pos === 'right' ? extra : ''); |
63 | | - }, |
64 | | - insertRight : function(val, $input) { |
65 | | - return this.insert(val, $input, 'right'); |
66 | | - }, |
67 | | - insertLeft : function(val, $input) { |
68 | | - return this.insert(val, $input, 'left'); |
69 | | - }, |
70 | | - numberFormat : function(val, $input) { |
71 | | - if (val.length === 0) { |
72 | | - return val; |
73 | | - } |
74 | | - if ( 'numeral' in window ) { |
75 | | - //If this has been previously formatted, it needs to be unformatted first before being reformatted. |
76 | | - //Else numeral will fail |
77 | | - val = numeral().unformat(val); |
78 | | - val = numeral(val).format( $input.attr('data-sanitize-number-format') ); |
79 | | - } |
80 | | - else { |
81 | | - throw new ReferenceError('Using sanitation function "numberFormat" requires that you include numeral.js ' + |
82 | | - '(http://numeraljs.com/)'); |
83 | | - } |
84 | | - return val; |
85 | | - }, |
86 | | - strip: function(val, $input) { |
87 | | - var toRemove = $input.attr('data-sanitize-strip') || ''; |
88 | | - $.split(toRemove, function(char) { |
89 | | - var regex = new RegExp($.isNumeric(char) ? char : '\\'+char, 'g'); |
90 | | - val = val.replace(regex, ''); |
91 | | - }); |
92 | | - return val; |
93 | | - }, |
94 | | - escape : function(val, $input) { |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + $.formUtils.addSanitizer({ |
| 89 | + name : 'insertRight', |
| 90 | + sanitizerFunction : function(val, $input) { |
| 91 | + return $.formUtils.sanitizers.insert.sanitizerFunction(val, $input, 'right'); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + $.formUtils.addSanitizer({ |
| 96 | + name : 'insertLeft', |
| 97 | + sanitizerFunction : function(val, $input) { |
| 98 | + return $.formUtils.sanitizers.insert.sanitizerFunction(val, $input, 'left'); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + $.formUtils.addSanitizer({ |
| 103 | + name : 'numberFormat', |
| 104 | + sanitizerFunction : function(val, $input) { |
| 105 | + if (val.length === 0) { |
| 106 | + return val; |
| 107 | + } |
| 108 | + if ( 'numeral' in window ) { |
| 109 | + //If this has been previously formatted, it needs to be unformatted first before being reformatted. |
| 110 | + //Else numeral will fail |
| 111 | + val = numeral().unformat(val); |
| 112 | + val = numeral(val).format( $input.attr('data-sanitize-number-format') ); |
| 113 | + } |
| 114 | + else { |
| 115 | + throw new ReferenceError('Using sanitation function "numberFormat" requires that you include numeral.js ' + |
| 116 | + '(http://numeraljs.com/)'); |
| 117 | + } |
| 118 | + return val; |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + $.formUtils.addSanitizer({ |
| 123 | + name : 'strip', |
| 124 | + sanitizerFunction : function(val, $input) { |
| 125 | + var toRemove = $input.attr('data-sanitize-strip') || ''; |
| 126 | + $.split(toRemove, function(char) { |
| 127 | + var regex = new RegExp($.isNumeric(char) ? char : '\\'+char, 'g'); |
| 128 | + val = val.replace(regex, ''); |
| 129 | + }); |
| 130 | + return val; |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + $.formUtils.addSanitizer({ |
| 135 | + name : 'escape', |
| 136 | + sanitizerFunction : function(val, $input) { |
95 | 137 | var isEscaped = $input.valAttr('is-escaped'), |
96 | 138 | entities = { |
97 | 139 | '<' : '__%AMP%__lt;', |
|
117 | 159 | }); |
118 | 160 |
|
119 | 161 | return val.replace(new RegExp('__\%AMP\%__', 'g'), '&'); |
120 | | - } |
121 | | - }, |
| 162 | + } |
| 163 | + }); |
| 164 | + |
| 165 | + $.formUtils.registerLoadedModule('sanitize'); |
| 166 | + |
| 167 | + var inputsThatCantBeSanitized = '[type="button"], [type="submit"], [type="radio"], [type="checkbox"], [type="reset"], [type="search"]', |
122 | 168 | setupSanitation = function(evt, $forms, config) { |
123 | 169 |
|
124 | 170 | if ( !$forms ) { |
125 | 171 | $forms = $('form'); |
126 | 172 | } |
127 | | - if ( !$forms.each ) { |
| 173 | + if ( !$forms.each ) { |
128 | 174 | $forms = $($forms); |
129 | 175 | } |
130 | 176 |
|
131 | 177 | var execSanitationCommands = function() { |
| 178 | + |
132 | 179 | var $input = $(this), |
133 | 180 | value = $input.val(); |
134 | | - $.split($input.attr('data-sanitize'), function(command) { |
135 | | - if ( command in sanitizeCommands ) { |
136 | | - value = sanitizeCommands[command](value, $input, config); |
137 | | - } |
138 | | - else { |
| 181 | + $.split($input.attr('data-sanitize'), function(command) { |
| 182 | + |
| 183 | + var sanitizer = $.formUtils.sanitizers[command]; |
| 184 | + |
| 185 | + if (sanitizer) { |
| 186 | + value = sanitizer.sanitizerFunction(value, $input, config); |
| 187 | + } else { |
139 | 188 | throw new Error('Use of unknown sanitize command "'+command+'"'); |
140 | 189 | } |
| 190 | + |
141 | 191 | }); |
142 | 192 | $input |
143 | 193 | .val(value) |
|
0 commit comments