From 5159db3d337323163f646020d1b5c8ba578d71dc Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 1 Mar 2026 21:47:07 -0700 Subject: [PATCH] Make the input sanitization apply to all sub-inputs --- module/utils/DialogManager.mjs | 63 +++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/module/utils/DialogManager.mjs b/module/utils/DialogManager.mjs index 081e248..50642cd 100644 --- a/module/utils/DialogManager.mjs +++ b/module/utils/DialogManager.mjs @@ -55,29 +55,7 @@ export async function ask( }; }; - let autofocusClaimed = false; - 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`; - }; - }; - }; + validateInputs(data.inputs); const promise = new Promise((resolve) => { const app = new Ask({ @@ -102,6 +80,45 @@ export function 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 = { close, ask,