From 8d59c626eac83f21e3b4c548f94f62d7e54789f7 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 14 Jan 2023 00:19:37 -0800 Subject: [PATCH 1/4] fix: listeners should not use hard indexing --- packages/core/src/virtual_dom.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index d8272a57c..b66f53b70 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -349,7 +349,7 @@ impl VirtualDom { /// If you have multiple events, you can call this method multiple times before calling "render_with_deadline" pub fn handle_event( &mut self, - name: &str, + mut name: &str, data: Rc, element: ElementId, bubbles: bool, @@ -384,6 +384,11 @@ impl VirtualDom { data, }; + // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one + if name.starts_with("on") { + name = &name[2..]; + } + // Loop through each dynamic attribute in this template before moving up to the template's parent. while let Some(el_ref) = parent_path { // safety: we maintain references of all vnodes in the element slab @@ -394,9 +399,7 @@ impl VirtualDom { for (idx, attr) in template.dynamic_attrs.iter().enumerate() { let this_path = node_template.attr_paths[idx]; - // listeners are required to be prefixed with "on", but they come back to the virtualdom with that missing - // we should fix this so that we look for "onclick" instead of "click" - if &attr.name[2..] == name && target_path.is_ascendant(&this_path) { + if attr.name == name && target_path.is_ascendant(&this_path) { listeners.push(&attr.value); // Break if the event doesn't bubble anyways From 40a36aa8e6fcf071d4f802831b206c83a5e4e56b Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 14 Jan 2023 00:21:26 -0800 Subject: [PATCH 2/4] chore: use trim --- packages/core/src/virtual_dom.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index b66f53b70..e2c4e528e 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -385,9 +385,7 @@ impl VirtualDom { }; // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one - if name.starts_with("on") { - name = &name[2..]; - } + name = name.trim_start_matches("on"); // Loop through each dynamic attribute in this template before moving up to the template's parent. while let Some(el_ref) = parent_path { From ecaa68605da402b73716ca5425daa783e9424b21 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 14 Jan 2023 11:57:03 -0800 Subject: [PATCH 3/4] fix: trim start matches for events --- packages/core/src/virtual_dom.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index e2c4e528e..08a2bf4d0 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -349,7 +349,7 @@ impl VirtualDom { /// If you have multiple events, you can call this method multiple times before calling "render_with_deadline" pub fn handle_event( &mut self, - mut name: &str, + name: &str, data: Rc, element: ElementId, bubbles: bool, @@ -385,7 +385,6 @@ impl VirtualDom { }; // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one - name = name.trim_start_matches("on"); // Loop through each dynamic attribute in this template before moving up to the template's parent. while let Some(el_ref) = parent_path { @@ -397,7 +396,9 @@ impl VirtualDom { for (idx, attr) in template.dynamic_attrs.iter().enumerate() { let this_path = node_template.attr_paths[idx]; - if attr.name == name && target_path.is_ascendant(&this_path) { + if attr.name.trim_start_matches("on") == name + && target_path.is_ascendant(&this_path) + { listeners.push(&attr.value); // Break if the event doesn't bubble anyways From a45f16a7735978986b2884a14b52975fcb6d75b2 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 14 Jan 2023 11:57:36 -0800 Subject: [PATCH 4/4] chore: clean up comments --- packages/core/src/virtual_dom.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 08a2bf4d0..d051e4de2 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -384,8 +384,6 @@ impl VirtualDom { data, }; - // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one - // Loop through each dynamic attribute in this template before moving up to the template's parent. while let Some(el_ref) = parent_path { // safety: we maintain references of all vnodes in the element slab @@ -396,6 +394,7 @@ impl VirtualDom { for (idx, attr) in template.dynamic_attrs.iter().enumerate() { let this_path = node_template.attr_paths[idx]; + // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one if attr.name.trim_start_matches("on") == name && target_path.is_ascendant(&this_path) {