Merge pull request #1104 from Demonthos/fix-booleans-with-boolean-attributes

Fix boolean attributes with raw boolean values
This commit is contained in:
Jon Kelley 2023-06-30 12:11:22 -07:00 committed by GitHub
commit 4145e50bbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View file

@ -48,21 +48,25 @@ 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);
}
}
}
}
}
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" {