Prevent pasting non slug or db safe characters (#12951)

This commit is contained in:
Azri Kahar
2022-04-22 20:04:52 +08:00
committed by GitHub
parent 1d1e8489b6
commit f319a7eaae

View File

@@ -156,7 +156,7 @@ function processValue(event: KeyboardEvent) {
const value = (event.target as HTMLInputElement).value;
if (props.slug === true) {
const slugSafeCharacters = 'abcdefghijklmnopqrstuvwxyz01234567890-_~ '.split('');
const slugSafeCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789-_~ '.split('');
const isAllowed = slugSafeCharacters.includes(key) || systemKeys.includes(key) || key.startsWith('arrow');
@@ -170,7 +170,7 @@ function processValue(event: KeyboardEvent) {
}
if (props.dbSafe === true) {
const dbSafeCharacters = 'abcdefghijklmnopqrstuvwxyz01234567890_ '.split('');
const dbSafeCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789_ '.split('');
const isAllowed = dbSafeCharacters.includes(key) || systemKeys.includes(key) || key.startsWith('arrow');
@@ -210,6 +210,9 @@ function emitValue(event: InputEvent) {
}
} else {
if (props.slug === true) {
// prevent pasting of non slugSafeCharacters from bypassing the keydown checks
value = value.replace(/[^a-zA-Z0-9-_~]/g, '');
const endsWithSpace = value.endsWith(' ');
value = slugify(value, { separator: props.slugSeparator, preserveTrailingDash: true });
if (endsWithSpace) value += props.slugSeparator;
@@ -217,6 +220,8 @@ function emitValue(event: InputEvent) {
if (props.dbSafe === true) {
value = value.replace(/\s/g, '_');
// prevent pasting of non dbSafeCharacters from bypassing the keydown checks
value = value.replace(/[^a-zA-Z0-9_]/g, '');
// Replace é -> e etc
value = value.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}