Skip if selected (#604)

This commit is contained in:
LawyZheng
2024-07-16 01:41:56 +08:00
committed by GitHub
parent 33ab70dd8b
commit 9b51df4ffd
2 changed files with 21 additions and 12 deletions

View File

@@ -605,7 +605,13 @@ function getSelectOptions(element) {
text: removeMultipleSpaces(option.textContent),
});
}
return selectOptions;
const selectedOption = element.querySelector("option:checked");
if (!selectedOption) {
return [selectOptions, ""];
}
return [selectOptions, removeMultipleSpaces(selectedOption.textContent)];
}
function getListboxOptions(element) {
@@ -776,15 +782,16 @@ async function buildTreeFromBody(frame = "main.frame", open_select = false) {
// assign shadowHostId to the shadowHost element if it doesn't have unique_id
if (!shadowHostId) {
shadowHostId = uniqueId();
shadowHost.setAttribute("unique_id", shadowHostId);
shadowHostEle.setAttribute("unique_id", shadowHostId);
}
elementObj.shadowHost = shadowHostId;
}
// get options for select element or for listbox element
let selectOptions = null;
let selectedValue = "";
if (elementTagNameLower === "select") {
selectOptions = getSelectOptions(element);
[selectOptions, selectedValue] = getSelectOptions(element);
} else if (attrs["role"] && attrs["role"].toLowerCase() === "listbox") {
// if "role" key is inside attrs, then get all the elements with role "option" and get their text
selectOptions = getListboxOptions(element);
@@ -840,6 +847,9 @@ async function buildTreeFromBody(frame = "main.frame", open_select = false) {
if (selectOptions) {
elementObj.options = selectOptions;
}
if (selectedValue) {
elementObj.attributes["selected"] = selectedValue;
}
return elementObj;
}