web/SettingsInput: validate input properly, reduce padding

This commit is contained in:
wukko 2025-02-25 13:40:04 +06:00
parent 631f8bddd8
commit c45c1d13c0
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -31,24 +31,42 @@
export let showInstanceWarning = false; export let showInstanceWarning = false;
const regex = { const regex = {
url: "https?:\\/\\/[a-z0-9.\\-]+(:\\d+)?/?",
uuid: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", uuid: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
}; };
let input: HTMLInputElement; let input: HTMLInputElement;
let inputValue: string = String(get(settings)[settingContext][settingId]); let inputValue: string = String(get(settings)[settingContext][settingId]);
let inputFocused = false; let inputFocused = false;
let validInput = false; let validInput = true;
let inputHidden = true; let inputHidden = true;
$: inputType = sensitive && inputHidden ? "password" : "text"; $: inputType = sensitive && inputHidden ? "password" : "text";
const checkInput = () => { const checkInput = () => {
validInput = input.checkValidity() || inputValue === ""; // mark input as valid if it's empty to allow wiping
if (inputValue.length === 0) {
validInput = true;
return;
}
if (type === "url") {
try {
new URL(inputValue)?.origin?.toString();
validInput = true;
return;
} catch {
validInput = false;
return;
}
} else {
validInput = new RegExp(regex[type]).test(inputValue);
}
} }
const writeToSettings = (value: string, type: SettingsInputType) => { const writeToSettings = (value: string, type: SettingsInputType) => {
// we assume that the url is valid and error can't be thrown here
// since it was tested before by checkInput()
updateSetting({ updateSetting({
[settingContext]: { [settingContext]: {
[settingId]: [settingId]:
@ -63,7 +81,7 @@
await customInstanceWarning(); await customInstanceWarning();
if ($settings.processing.seenCustomWarning) { if ($settings.processing.seenCustomWarning) {
// allow writing empty strings // fall back to uuid to allow writing empty strings
return writeToSettings(inputValue, inputValue ? type : "uuid"); return writeToSettings(inputValue, inputValue ? type : "uuid");
} }
@ -81,7 +99,7 @@
aria-hidden="false" aria-hidden="false"
> >
<input <input
id="input-box" class="input-box"
bind:this={input} bind:this={input}
bind:value={inputValue} bind:value={inputValue}
on:input={() => { on:input={() => {
@ -94,9 +112,9 @@
autocomplete="off" autocomplete="off"
autocapitalize="off" autocapitalize="off"
maxlength="64" maxlength="64"
pattern={regex[type]}
aria-label={altText} aria-label={altText}
aria-hidden="false" aria-hidden="false"
aria-invalid={!validInput}
{...{ type: inputType }} {...{ type: inputType }}
/> />
@ -182,20 +200,20 @@
} }
#input-container, #input-container,
#input-box { .input-box {
font-size: 13.5px; font-size: 13px;
font-weight: 500; font-weight: 500;
min-width: 0; min-width: 0;
} }
#input-box { .input-box {
flex: 1; flex: 1;
background-color: transparent; background-color: transparent;
color: var(--secondary); color: var(--secondary);
border: none; border: none;
padding-block: 0; padding-block: 0;
padding-inline: 0; padding-inline: 0;
padding: 12px 0; padding: 11.5px 0;
} }
.input-placeholder { .input-placeholder {
@ -205,7 +223,7 @@
white-space: nowrap; white-space: nowrap;
} }
#input-box:focus-visible { .input-box:focus-visible {
box-shadow: unset !important; box-shadow: unset !important;
} }
@ -220,8 +238,7 @@
} }
.settings-input-button { .settings-input-button {
height: 42px; width: 40px;
width: 42px;
padding: 0; padding: 0;
} }