fix boolean attributes with raw boolean values

This commit is contained in:
Evan Almloff 2023-06-19 10:19:35 -05:00
parent 6e5fbcff63
commit 4f672ff4bb
2 changed files with 14 additions and 7 deletions

View file

@ -48,17 +48,17 @@ export function setAttributeInner(node, field, value, ns) {
node.defaultValue = value;
break;
case "checked":
node.checked = value === "true" || value === true;
node.checked = truthy(value);
break;
case "selected":
node.selected = value === "true" || value === true;
node.selected = truthy(value);
break;
case "dangerous_inner_html":
node.innerHTML = value;
break;
default:
// https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
if (value === "false" && bool_attrs.hasOwnProperty(name)) {
if (!truthy(value) && bool_attrs.hasOwnProperty(name)) {
node.removeAttribute(name);
} else {
node.setAttribute(name, value);
@ -66,3 +66,7 @@ export function setAttributeInner(node, field, value, ns) {
}
}
}
function truthy(val) {
return val === "true" || val === true;
}

View file

@ -78,17 +78,17 @@ mod js {
node.defaultValue = value;
break;
case "checked":
node.checked = value === "true";
node.checked = truthy(value);
break;
case "selected":
node.selected = value === "true";
node.selected = truthy(value);
break;
case "dangerous_inner_html":
node.innerHTML = value;
break;
default:
// https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
if (value === "false" && bool_attrs.hasOwnProperty(name)) {
if (!truthy(value) && bool_attrs.hasOwnProperty(name)) {
node.removeAttribute(name);
} else {
node.setAttribute(name, value);
@ -164,6 +164,9 @@ mod js {
selected: true,
truespeed: true,
};
function truthy(val) {
return val === "true" || val === true;
}
"#;
extern "C" {