From bea7717257700d0e50e9b35588cb2a96ce86981d Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 7 Sep 2024 11:45:54 +0100 Subject: [PATCH] Determine checkbox checked status based on presence of checked attribute (ignoring value) --- packages/blitz/src/renderer/render.rs | 6 +---- packages/dom/src/document.rs | 32 +++++++++++++-------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/blitz/src/renderer/render.rs b/packages/blitz/src/renderer/render.rs index b3cea7bf..9e60e623 100644 --- a/packages/blitz/src/renderer/render.rs +++ b/packages/blitz/src/renderer/render.rs @@ -1213,11 +1213,7 @@ impl ElementCx<'_> { if self.element.local_name() == "input" && matches!(self.element.attr(local_name!("type")), Some("checkbox")) { - let checked: bool = self - .element - .attr(local_name!("checked")) - .and_then(|c| c.parse().ok()) - .unwrap_or_default(); + let checked = self.element.attr(local_name!("checked")).is_some(); // TODO this should be coming from css accent-color, but I couldn't find how to retrieve it let accent_color = self.style.get_inherited_text().color.as_vello(); diff --git a/packages/dom/src/document.rs b/packages/dom/src/document.rs index 6bc4c583..9bf00f3a 100644 --- a/packages/dom/src/document.rs +++ b/packages/dom/src/document.rs @@ -308,26 +308,24 @@ impl Document { } pub fn toggle_checkbox(el: &mut ElementNodeData) { - let checked_attr_opt = el + let is_checked = el .attrs - .iter_mut() - .find(|attr| attr.name.local == local_name!("checked")); + .iter() + .any(|attr| attr.name.local == local_name!("checked")); - let checked_attr = if let Some(attr) = checked_attr_opt { - attr - } else { - let attr = Attribute { - name: QualName::new(None, ns!(html), local_name!("checked")), - value: String::from("false"), - }; - el.attrs.push(attr); + if is_checked { el.attrs - .iter_mut() - .find(|attr| attr.name.local == local_name!("checked")) - .unwrap() - }; - let checked = checked_attr.value.parse().unwrap_or(false); - checked_attr.value = (!checked).to_string(); + .retain(|attr| attr.name.local != local_name!("checked")) + } else { + el.attrs.push(Attribute { + name: QualName { + prefix: None, + ns: ns!(html), + local: local_name!("checked"), + }, + value: String::new(), + }) + } } pub fn root_node(&self) -> &Node {