Skip to content

Commit 4e53513

Browse files
committed
extension: Improve getElement elements discovery and validation
1 parent 65b5a20 commit 4e53513

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

web/packages/extension/src/common.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,31 @@ class SelectOption implements OptionElement<string | null> {
113113
function getElement(option: Element): OptionElement<unknown> {
114114
const element = option.querySelector<HTMLInputElement | HTMLSelectElement>(
115115
"input, select",
116-
)!;
116+
);
117+
118+
if (!element) {
119+
throw new Error(
120+
"No input or select element found inside the option container.",
121+
);
122+
}
117123

118124
const label = option.querySelector<HTMLLabelElement>(
119125
`label[for="${element.id}"]`,
120-
)!;
121-
122-
const [input] = option.getElementsByTagName("input");
123-
if (input) {
124-
if (input.type === "checkbox") {
125-
return new CheckboxOption(input, label);
126-
}
126+
);
127127

128-
if (input.type === "number") {
129-
return new NumberOption(input, label);
130-
}
128+
if (!label) {
129+
throw new Error(`No label found with for="${element.id}"`);
131130
}
132131

133-
const [select] = option.getElementsByTagName("select");
134-
if (select) {
135-
return new SelectOption(select, label);
132+
if (element instanceof HTMLInputElement) {
133+
switch (element.type) {
134+
case "checkbox":
135+
return new CheckboxOption(element, label);
136+
case "number":
137+
return new NumberOption(element, label);
138+
}
139+
} else if (element instanceof HTMLSelectElement) {
140+
return new SelectOption(element, label);
136141
}
137142

138143
throw new Error("Unknown option element");

0 commit comments

Comments
 (0)