perf(dom): discover shadow-tree frames through candidate index

Use the existing shadow-including containment and tree-order helpers to find
frame owners directly, rather than scanning every retained shadow host.
Keep disconnected queries structural because compaction may drop candidates.

Restore shadow-frame candidates during the existing reconnection walk.
Cover ordering, reinsertion, clone/import/adoption, inert template contents
and foreign elements.
This commit is contained in:
ldm0
2026-11-21 20:03:38 +08:00
parent e3836bb52e
commit e7390db761
3 changed files with 169 additions and 0 deletions
+2
View File
@@ -16,6 +16,8 @@ mod clone;
mod collections;
mod document;
mod flat_tree;
#[cfg(test)]
mod frame_candidate_tests;
mod html_serialization;
mod mutation;
mod parser;
+10
View File
@@ -2168,6 +2168,16 @@ impl DomHost {
let mut touched_shadow_root = false;
let mut stack = vec![(root, owner_document, connected)];
while let Some((handle, owner_document, connected)) = stack.pop() {
// A detached shadow tree may have lost its frame candidates to
// compaction. Restore them during the existing connection walk.
if connected && let Some(element) = self.node(handle).and_then(Node::as_element) {
self.record_child_browsing_context_host_candidate_if_needed(
handle,
element.local_name(),
element.namespace(),
);
}
stack.extend(
self.child_handles_reversed(handle)
.map(|child| (child, owner_document, connected)),
@@ -0,0 +1,157 @@
use super::*;
fn document() -> (DomHost, DomHandle) {
let mut host = DomHost::from_dom(NativeDom::new_html(
Url::parse("https://shadow-frame-candidates.test/").unwrap(),
));
host.reset_html_document_shell();
let body = host.document_body_handle().unwrap();
(host, body)
}
fn frames(host: &DomHost, root: DomHandle) -> Vec<DomHandle> {
host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order(root)
}
#[test]
fn frame_candidates_follow_physical_shadow_order_after_tree_moves() {
let (mut host, body) = document();
let outer = host.create_element("div");
let shadow = host.attach_shadow_root(outer, "open").unwrap();
// Creation order differs from both light and shadow-including tree order.
let light = host.create_element("iframe");
let last = host.create_element("embed");
let nested = host.create_element("frame");
let first = host.create_element("object");
let nested_host = host.create_element("span");
let nested_shadow = host.attach_shadow_root(nested_host, "closed").unwrap();
let outside = host.create_element("iframe");
assert!(host.append_child(body, outside));
assert!(host.append_child(shadow, first));
assert!(host.append_child(shadow, nested_host));
assert!(host.append_child(nested_shadow, nested));
assert!(host.append_child(shadow, last));
assert!(host.append_child(outer, light));
assert!(host.append_child(body, outer));
assert_eq!(frames(&host, outer), vec![first, nested, last, light]);
assert_eq!(frames(&host, shadow), vec![first, nested, last]);
assert_eq!(frames(&host, light), vec![light]);
assert_eq!(
frames(&host, body),
vec![outside, first, nested, last, light]
);
assert_eq!(
host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order(body),
vec![outside, first, nested, last, light],
"the canonical native query includes physical shadow descendants",
);
assert!(host.append_child(outer, first));
assert_eq!(frames(&host, outer), vec![nested, last, light, first]);
assert!(host.append_child(shadow, nested));
assert_eq!(frames(&host, outer), vec![last, nested, light, first]);
}
#[test]
fn reconnecting_shadow_trees_restores_compacted_frame_candidates() {
let (mut host, body) = document();
let outer = host.create_element("div");
let shadow = host.attach_shadow_root(outer, "closed").unwrap();
let frame = host.create_element("iframe");
assert!(host.append_child(shadow, frame));
host.compact_child_browsing_context_host_candidates();
assert!(
host.child_browsing_context_host_candidate_handles()
.is_empty()
);
assert_eq!(frames(&host, outer), vec![frame]);
assert!(host.append_child(body, outer));
assert_eq!(
host.child_browsing_context_host_candidate_handles(),
vec![frame]
);
assert_eq!(frames(&host, body), vec![frame]);
assert!(host.remove_child(body, outer));
host.compact_child_browsing_context_host_candidates();
assert!(frames(&host, body).is_empty());
assert_eq!(frames(&host, shadow), vec![frame]);
assert!(host.append_child(body, outer));
assert_eq!(
host.child_browsing_context_host_candidate_handles(),
vec![frame]
);
assert_eq!(frames(&host, body), vec![frame]);
}
#[test]
fn cloned_imported_and_adopted_shadow_frames_keep_candidate_membership() {
let (mut host, body) = document();
let source = host.create_element("section");
let mut init = ShadowRootInit::new("closed");
init.set_clonable(true);
let shadow = host.attach_shadow_root_with_init(source, init).unwrap();
let source_frame = host.create_element("iframe");
assert!(host.append_child(shadow, source_frame));
let cloned = host.clone_node(source, true).unwrap();
let imported = host
.import_node(host.document_handle(), source, true)
.unwrap();
let clone_frame = host
.first_child(host.shadow_root_handle(cloned).unwrap())
.unwrap();
let imported_frame = host
.first_child(host.shadow_root_handle(imported).unwrap())
.unwrap();
host.compact_child_browsing_context_host_candidates();
assert!(host.append_child(body, cloned));
assert!(host.append_child(body, imported));
assert_eq!(frames(&host, body), vec![clone_frame, imported_frame]);
let secondary = host.create_detached_html_document();
assert_eq!(host.adopt_node(secondary, cloned), Some(cloned));
assert!(host.append_child(secondary, cloned));
host.compact_child_browsing_context_host_candidates();
assert_eq!(frames(&host, body), vec![imported_frame]);
assert_eq!(frames(&host, secondary), vec![clone_frame]);
assert_eq!(host.owner_document_handle(clone_frame), Some(secondary));
assert!(host.append_child(body, cloned));
assert_eq!(frames(&host, body), vec![imported_frame, clone_frame]);
assert_eq!(
host.owner_document_handle(clone_frame),
Some(host.document_handle())
);
}
#[test]
fn frame_candidates_exclude_inert_template_contents_and_foreign_elements() {
let (mut host, body) = document();
let template = host.create_element("template");
let contents = host
.node(template)
.unwrap()
.as_element()
.unwrap()
.template_contents()
.unwrap();
let inert_frame = host.create_element("iframe");
assert!(host.append_child(contents, inert_frame));
assert!(host.append_child(body, template));
let outer = host.create_element("section");
let shadow = host.attach_shadow_root(outer, "closed").unwrap();
let foreign = host
.create_element_ns(Some("http://www.w3.org/2000/svg"), "iframe")
.unwrap();
let live_frame = host.create_element("iframe");
assert!(host.append_child(shadow, foreign));
assert!(host.append_child(shadow, live_frame));
assert!(host.append_child(body, outer));
host.compact_child_browsing_context_host_candidates();
assert_eq!(frames(&host, body), vec![live_frame]);
assert!(frames(&host, template).is_empty());
assert_eq!(frames(&host, contents), vec![inert_frame]);
}