Make the input sanitization apply to all sub-inputs

This commit is contained in:
Oliver 2026-03-01 21:47:07 -07:00
parent 1d43be188e
commit 5159db3d33

View file

@ -55,29 +55,7 @@ export async function ask(
}; };
}; };
let autofocusClaimed = false; validateInputs(data.inputs);
for (const i of data.inputs) {
i.id ??= foundry.utils.randomID(16);
i.key ??= i.label;
// Only ever allow one input to claim autofocus
i.autofocus &&= !autofocusClaimed;
autofocusClaimed ||= i.autofocus;
// Set the value's attribute name if it isn't specified explicitly
if (i.type === `input`) {
i.inputType ??= `text`;
switch (i.inputType) {
case `checkbox`:
i.type = `checkbox`;
delete i.valueAttribute;
delete i.inputType;
break;
default:
i.valueAttribute ??= `value`;
};
};
};
const promise = new Promise((resolve) => { const promise = new Promise((resolve) => {
const app = new Ask({ const app = new Ask({
@ -102,6 +80,45 @@ export function size() {
return dialogs.size; return dialogs.size;
}; };
/**
* Ensures all of the inputs are correctly valid and performs any
* mutations to them required in order to make them valid or assign
* defaults for properties that are missing.
*
* @param {AskInput[]} inputs The array of inputs for the dialog
* @param {boolean} [autofocusClaimed] Whether or not to allow
* autofocusing an input
*/
function validateInputs(inputs, autofocusClaimed = false) {
for (const i of inputs) {
i.id ??= foundry.utils.randomID(16);
i.key ??= i.label;
// Only ever allow one input to claim autofocus
i.autofocus &&= !autofocusClaimed;
autofocusClaimed ||= i.autofocus;
// Set the value's attribute name if it isn't specified explicitly
if (i.type === `input`) {
i.inputType ??= `text`;
switch (i.inputType) {
case `checkbox`:
i.type = `checkbox`;
delete i.valueAttribute;
delete i.inputType;
break;
default:
i.valueAttribute ??= `value`;
};
};
// Recurse on child inputs if required
if (i.type === `collapse` && i.inputs?.length > 0) {
validateInputs(i.inputs, autofocusClaimed);
};
};
};
export const DialogManager = { export const DialogManager = {
close, close,
ask, ask,