@@ -197,6 +197,83 @@ define(function (require, exports, module) {
197197 }
198198 }
199199
200+ /**
201+ * validates and sanitizes file extension input
202+ *
203+ * @param {string } value - The input value to sanitize
204+ * @returns {string } - The sanitized value
205+ */
206+ function sanitizeFileExtensionInput ( value ) {
207+ value = value . replace ( / [ ^ a - z A - Z , . \s ] / g, "" ) ; // we only allow a-z, A-Z, comma, dot, space
208+ value = value . replace ( / \. { 2 , } / g, "." ) ; // don't allow 2 consecutive dots
209+ value = value . replace ( / ( \. ) \1+ / g, "$1" ) ; // prevent two dots next to each other
210+ return value ;
211+ }
212+
213+ /**
214+ * handles file extension input event with validation
215+ *
216+ * @param {jQuery } $input - The input element
217+ */
218+ function handleFileExtensionInput ( $input ) {
219+ let value = $input . val ( ) ;
220+ const sanitizedValue = sanitizeFileExtensionInput ( value ) ;
221+ $input . val ( sanitizedValue ) ;
222+ toggleSaveButtonDisability ( ) ;
223+ }
224+
225+ /**
226+ * Handles file extension keypress event validation
227+ *
228+ * @param {Event } e - The keypress event
229+ * @param {HTMLElement } input - The input element
230+ * @returns {boolean } - Whether to allow the keypress
231+ */
232+ function handleFileExtensionKeypress ( e , input ) {
233+ const char = String . fromCharCode ( e . which ) ;
234+ const allowed = / ^ [ a - z A - Z , . \s ] $ / ;
235+
236+ // prevent two consecutive dots
237+ if ( char === "." && input . value . slice ( - 1 ) === "." ) {
238+ e . preventDefault ( ) ;
239+ return false ;
240+ }
241+
242+ if ( ! allowed . test ( char ) ) {
243+ e . preventDefault ( ) ;
244+ return false ;
245+ }
246+
247+ return true ;
248+ }
249+
250+ /**
251+ * Handles file extension paste event with validation
252+ *
253+ * @param {Event } e - The paste event
254+ * @param {jQuery } $input - The input element
255+ */
256+ function handleFileExtensionPaste ( e , $input ) {
257+ e . preventDefault ( ) ;
258+
259+ const clipboardData = ( e . originalEvent || e ) . clipboardData . getData ( "text" ) ;
260+ let sanitized = sanitizeFileExtensionInput ( clipboardData ) ;
261+
262+ // insert sanitized value at current cursor position
263+ const input = $input [ 0 ] ;
264+ const start = input . selectionStart ;
265+ const end = input . selectionEnd ;
266+ const currentValue = input . value ;
267+
268+ input . value = currentValue . substring ( 0 , start ) + sanitized + currentValue . substring ( end ) ;
269+
270+ // move the cursor to the end of the inserted text
271+ const newPos = start + sanitized . length ;
272+ input . setSelectionRange ( newPos , newPos ) ;
273+
274+ toggleSaveButtonDisability ( ) ;
275+ }
276+
200277 exports . toggleSaveButtonDisability = toggleSaveButtonDisability ;
201278 exports . createHintItem = createHintItem ;
202279 exports . clearAllInputFields = clearAllInputFields ;
@@ -206,4 +283,8 @@ define(function (require, exports, module) {
206283 exports . hasExactMatchingSnippet = hasExactMatchingSnippet ;
207284 exports . getMatchingSnippets = getMatchingSnippets ;
208285 exports . updateSnippetsCount = updateSnippetsCount ;
286+ exports . sanitizeFileExtensionInput = sanitizeFileExtensionInput ;
287+ exports . handleFileExtensionInput = handleFileExtensionInput ;
288+ exports . handleFileExtensionKeypress = handleFileExtensionKeypress ;
289+ exports . handleFileExtensionPaste = handleFileExtensionPaste ;
209290} ) ;
0 commit comments