Skip to content

Commit

Permalink
fixes bubbling of events originating in shadow dom
Browse files Browse the repository at this point in the history
when we not explicitly portaling into the shadow, we
should bubble up to the host element and continue.
This fixes custom elements internally using open SD
  • Loading branch information
WorldSEnder committed Apr 20, 2022
1 parent 504693f commit 335783e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/yew/Cargo.toml
Expand Up @@ -54,6 +54,7 @@ features = [
"NodeList",
"PointerEvent",
"ProgressEvent",
"ShadowRoot",
"Text",
"TouchEvent",
"TransitionEvent",
Expand Down Expand Up @@ -81,7 +82,6 @@ trybuild = "1"
[dev-dependencies.web-sys]
version = "0.3"
features = [
"ShadowRoot",
"ShadowRootInit",
"ShadowRootMode",
]
Expand Down
13 changes: 10 additions & 3 deletions packages/yew/src/dom_bundle/subtree_root.rs
Expand Up @@ -9,7 +9,7 @@ use std::hash::{Hash, Hasher};
use std::rc::{Rc, Weak};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use wasm_bindgen::{prelude::wasm_bindgen, JsCast};
use web_sys::{Element, Event, EventTarget as HtmlEventTarget};
use web_sys::{Element, Event, EventTarget as HtmlEventTarget, ShadowRoot};

/// DOM-Types that capture (bubbling) events. This generally includes event targets,
/// but also subtree roots.
Expand Down Expand Up @@ -239,6 +239,13 @@ struct BrandingSearchResult {
closest_branded_ancestor: Element,
}

fn shadow_aware_parent(el: &Element) -> Option<Element> {
match el.parent_element() {
s @ Some(_) => s,
None => el.parent_node()?.dyn_ref::<ShadowRoot>().map(|h| h.host()),
}
}

/// Deduce the subtree an element is part of. This already partially starts the bubbling
/// process, as long as no listeners are encountered.
/// Subtree roots are always branded with their own subtree id.
Expand All @@ -254,7 +261,7 @@ fn find_closest_branded_element(mut el: Element, do_bubble: bool) -> Option<Bran
if let Some(tree_id) = el.subtree_id() {
break tree_id;
}
el = el.parent_element()?;
el = shadow_aware_parent(&el)?;
};
Some(BrandingSearchResult {
branding: responsible_tree_id,
Expand All @@ -276,7 +283,7 @@ fn start_bubbling_from(
if !should_bubble {
return None;
}
let parent = element.parent_element()?;
let parent = shadow_aware_parent(element)?;
subtree.bubble_to_inner_element(parent, true)
})
}
Expand Down

0 comments on commit 335783e

Please sign in to comment.