optimize bool type value parsing in domutils (#4046)

This commit is contained in:
LawyZheng
2025-11-20 15:16:24 +08:00
committed by GitHub
parent 48b3176d4e
commit 7f9916d3ca

View File

@@ -1508,6 +1508,50 @@ async function buildElementObject(
attrs["required"] = true;
}
// check DOM property of required/checked/selected/readonly/disabled
// the value from the DOM property should be the top priority
if (element.required !== undefined) {
delete attrs["required"];
delete attrs["aria-required"];
if (element.required) {
attrs["required"] = true;
}
}
if (element.checked !== undefined) {
delete attrs["checked"];
delete attrs["aria-checked"];
if (element.checked) {
attrs["checked"] = true;
} else if (
elementTagNameLower === "input" &&
(element.type === "checkbox" || element.type === "radio")
) {
// checked property always exists for checkbox and radio elements
attrs["checked"] = false;
}
}
if (element.selected !== undefined) {
delete attrs["selected"];
delete attrs["aria-selected"];
if (element.selected) {
attrs["selected"] = true;
}
}
if (element.readOnly !== undefined) {
delete attrs["readonly"];
delete attrs["aria-readonly"];
if (element.readOnly) {
attrs["readonly"] = true;
}
}
if (element.disabled !== undefined) {
delete attrs["disabled"];
delete attrs["aria-disabled"];
if (element.disabled) {
attrs["disabled"] = true;
}
}
if (elementTagNameLower === "input" || elementTagNameLower === "textarea") {
if (element.type === "password") {
attrs["value"] = element.value ? "*".repeat(element.value.length) : "";