Skip to content

Commit 83b1a6c

Browse files
committed
fix: validate file extension input values to make sure users always type accepted values
1 parent edb0640 commit 83b1a6c

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,21 @@ define(function (require, exports, module) {
132132
$abbrInput.on("input", Helper.toggleSaveButtonDisability);
133133
$descInput.on("input", Helper.toggleSaveButtonDisability);
134134
$templateInput.on("input", Helper.toggleSaveButtonDisability);
135-
$fileExtnInput.on("input", Helper.toggleSaveButtonDisability);
135+
136+
$fileExtnInput.on("input", function () {
137+
Helper.handleFileExtensionInput($(this));
138+
});
139+
$fileExtnInput.on("keypress", function (e) {
140+
Helper.handleFileExtensionKeypress(e, this);
141+
});
142+
$fileExtnInput.on("paste", function (e) {
143+
Helper.handleFileExtensionPaste(e, $(this));
144+
});
136145

137146
// filter input event handler
138147
$filterInput.on("keyup input", function (event) {
139148
// if user presses 'esc' we clear the input field
140-
if (event && event.key === 'Escape') {
149+
if (event && event.key === "Escape") {
141150
$(this).val("");
142151
SnippetsList.showSnippetsList();
143152
return;

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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-zA-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-zA-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

Comments
 (0)