Skip to content

Commit

Permalink
fixes bubbling of events originating in shadow dom (#2627)
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 59e2f82 commit c2b2f2c
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

1 comment on commit c2b2f2c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yew master branch benchmarks (Lower is better)

Benchmark suite Current: c2b2f2c Previous: 59e2f82 Ratio
yew-struct-keyed 01_run1k 166.332 209.4815 0.79
yew-struct-keyed 02_replace1k 194.4335 223.65249999999995 0.87
yew-struct-keyed 03_update10th1k_x16 380.1905 390.5425 0.97
yew-struct-keyed 04_select1k 69.12100000000001 70.779 0.98
yew-struct-keyed 05_swap1k 86.1845 98.236 0.88
yew-struct-keyed 06_remove-one-1k 27.597 32.1105 0.86
yew-struct-keyed 07_create10k 2988.3755 3254.23 0.92
yew-struct-keyed 08_create1k-after1k_x2 414.525 463.937 0.89
yew-struct-keyed 09_clear1k_x8 188.3615 181.343 1.04
yew-struct-keyed 21_ready-memory 1.457233428955078 1.457233428955078 1
yew-struct-keyed 22_run-memory 1.6934738159179688 1.6618614196777344 1.02
yew-struct-keyed 23_update5-memory 1.6953849792480469 1.695598602294922 1.00
yew-struct-keyed 24_run5-memory 1.708965301513672 1.94403076171875 0.88
yew-struct-keyed 25_run-clear-memory 1.3271331787109375 1.3280715942382812 1.00
yew-struct-keyed 31_startup-ci 1736.384 1880.985 0.92
yew-struct-keyed 32_startup-bt 27.64 36.852 0.75
yew-struct-keyed 33_startup-mainthreadcost 229.276 274.8040000000001 0.83
yew-struct-keyed 34_startup-totalbytes 328.7392578125 328.7392578125 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.