Merge pull request #1021 from Demonthos/fix-web-event-bubbling-on-text-nodes

Fix web events starting on a text node
This commit is contained in:
Jon Kelley 2023-05-18 13:15:40 +02:00 committed by GitHub
commit 7f74927d82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -380,22 +380,38 @@ extern "C" {
} }
fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> { fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> {
let mut target = event let target = event
.target() .target()
.expect("missing target") .expect("missing target")
.dyn_into::<web_sys::Element>() .dyn_into::<web_sys::Node>()
.expect("not a valid element"); .expect("not a valid node");
let mut current_target_element = target.dyn_ref::<web_sys::Element>().cloned();
loop { loop {
match target.get_attribute("data-dioxus-id").map(|f| f.parse()) { match (
Some(Ok(id)) => return Some((ElementId(id), target)), current_target_element
Some(Err(_)) => return None, .as_ref()
.and_then(|el| el.get_attribute("data-dioxus-id").map(|f| f.parse())),
current_target_element,
) {
// This node is an element, and has a dioxus id, so we can stop walking
(Some(Ok(id)), Some(target)) => return Some((ElementId(id), target)),
// walk the tree upwards until we actually find an event target // Walk the tree upwards until we actually find an event target
None => match target.parent_element() { (None, target_element) => {
Some(parent) => target = parent, let parent = match target_element.as_ref() {
None => return None, Some(el) => el.parent_element(),
}, // if this is the first node and not an element, we need to get the parent from the target node
None => target.parent_element(),
};
match parent {
Some(parent) => current_target_element = Some(parent),
_ => return None,
}
}
// This node is an element with an invalid dioxus id, give up
_ => return None,
} }
} }
} }