From 4f672ff4bb4cc3dad61b36474ee42789c2353260 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 19 Jun 2023 10:19:35 -0500 Subject: [PATCH] fix boolean attributes with raw boolean values --- packages/interpreter/src/common.js | 12 ++++++++---- packages/interpreter/src/sledgehammer_bindings.rs | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/interpreter/src/common.js b/packages/interpreter/src/common.js index 532c231db..c93f9eb60 100644 --- a/packages/interpreter/src/common.js +++ b/packages/interpreter/src/common.js @@ -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); } } } -} \ No newline at end of file +} + +function truthy(val) { + return val === "true" || val === true; +} diff --git a/packages/interpreter/src/sledgehammer_bindings.rs b/packages/interpreter/src/sledgehammer_bindings.rs index 6c2804b59..e661a7c16 100644 --- a/packages/interpreter/src/sledgehammer_bindings.rs +++ b/packages/interpreter/src/sledgehammer_bindings.rs @@ -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" {