FeDirect/static/dom.mts

65 lines
No EOL
2.4 KiB
TypeScript

// I would've LOVED to use generics for this but unfortunately that's not possible.
// Type safety, but at what cost... >~< thanks TypeScript
export function findOlOrFail(on: Element, selector: string): HTMLOListElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLOListElement))
throw new Error(`${selector} isn't an ol`);
return element;
}
export function findPreOrFail(on: Element, selector: string): HTMLPreElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLPreElement))
throw new Error(`${selector} isn't a pre`);
return element;
}
export function findParagraphOrFail(on: Element, selector: string): HTMLParagraphElement {
const element = on.querySelector(selector);
if (!(element instanceof HTMLParagraphElement))
throw new Error(`${selector} isn't a paragraph`);
return element;
}
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;
}
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;
}