(node: Node, tagName: string)
| 35 | * such as HTMLIonSelectOptionElement. |
| 36 | */ |
| 37 | export const findCheckedOption = <T extends HTMLElement & { value?: any | null }>(node: Node, tagName: string) => { |
| 38 | /** |
| 39 | * https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType |
| 40 | * The above check ensures "node" is an Element (nodeType 1). |
| 41 | */ |
| 42 | if (node.nodeType !== 1) { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
| 46 | // HTMLElement inherits from Element, so we cast "el" as T. |
| 47 | const el = node as T; |
| 48 | |
| 49 | const options: T[] = el.tagName === tagName.toUpperCase() ? [el] : Array.from(el.querySelectorAll(tagName)); |
| 50 | |
| 51 | return options.find((o: T) => o.value === el.value); |
| 52 | }; |
no test coverage detected