FeDirect/static/dom.mts

44 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2025-01-14 14:52:31 +01:00
// I would've LOVED to use generics for this but unfortunately that's not possible.
// Type safety, but at what cost... >~< thanks TypeScript
2025-01-14 15:58:30 +01:00
export function findDialogOrFail(on: Element, selector: string): HTMLDialogElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLDialogElement))
throw new Error(`${selector} isn't a dialog`);
return element;
}
2025-01-14 14:52:31 +01:00
export function findFormOrFail(on: Element, selector: string): HTMLFormElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLFormElement))
throw new Error(`${selector} isn't a form`);
return element;
}
export function findInputOrFail(on: Element, selector: string): HTMLInputElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLInputElement))
throw new Error(`${selector} isn't an input`);
return element;
}
export function findButtonOrFail(on: Element, selector: string): HTMLButtonElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLButtonElement))
throw new Error(`${selector} isn't a button`);
return element;
}
export function findSelectOrFail(on: Element, selector: string): HTMLSelectElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLSelectElement))
throw new Error(`${selector} isn't a select`);
return element;
}
export function findImageOrFail(on: Element, selector: string): HTMLImageElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLImageElement))
throw new Error(`${selector} isn't an image`);
return element;
}