diff --git a/moli-dom/src/native/host/document.rs b/moli-dom/src/native/host/document.rs index 93584dd7a67..0c3f0d477b4 100644 --- a/moli-dom/src/native/host/document.rs +++ b/moli-dom/src/native/host/document.rs @@ -1578,13 +1578,15 @@ impl DomHost { .collect() } + /// Indexed frame owners in shadow-including order, including a frame root. pub fn child_browsing_context_host_candidate_handles_in_subtree_in_document_order( &self, root: DomHandle, ) -> Vec { - let Some(root_node) = self.node(root) else { + if self.node(root).is_none() { return Vec::new(); - }; + } + let connected = self.is_connected(root); let mut handles = self .child_browsing_context_host_candidates .borrow() @@ -1592,21 +1594,14 @@ impl DomHost { .copied() .filter(|handle| { self.is_child_browsing_context_host_candidate(*handle) - && root_node.contains(&self.dom, *handle) + // Host-inclusive ancestry also crosses template.content. + // A connected frame tree must exclude those inert owners. + && (!connected || self.is_connected(*handle)) + && self.is_host_including_inclusive_ancestor(root, *handle) }) .collect::>(); handles.sort_unstable_by(|left, right| { - let Some(left_node) = self.node(*left) else { - return std::cmp::Ordering::Greater; - }; - let position = left_node.compare_document_position(&self.dom, *right); - if position & Node::DOCUMENT_POSITION_FOLLOWING != 0 { - std::cmp::Ordering::Less - } else if position & Node::DOCUMENT_POSITION_PRECEDING != 0 { - std::cmp::Ordering::Greater - } else { - left.index().cmp(&right.index()) - } + self.compare_handles_in_shadow_including_tree_order(*left, *right) }); handles.dedup(); handles diff --git a/moli-dom/src/native/host/html_serialization/mod.rs b/moli-dom/src/native/host/html_serialization/mod.rs index 1e40babf9ef..b3e84e345d4 100644 --- a/moli-dom/src/native/host/html_serialization/mod.rs +++ b/moli-dom/src/native/host/html_serialization/mod.rs @@ -51,12 +51,14 @@ impl DomHost { /// Like [`Self::get_html`], but serializes `dom`, an edited copy of this /// host's DOM (for example an export copy), with this host's shadow roots. + /// Exports can omit stylesheet metadata without changing the live roots. pub fn get_html_from_copy( &self, dom: &NativeDom, handle: DomHandle, scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool, explicit_shadow_roots: &[DomHandle], + include_adopted_style_sheets: bool, ) -> Option { self.serialize_html_with_shadow_roots( dom, @@ -68,6 +70,7 @@ impl DomHost { explicit: explicit_shadow_roots, }, None, + include_adopted_style_sheets, ) } @@ -89,6 +92,7 @@ impl DomHost { explicit: explicit_shadow_roots, }, registry_attribute_policy, + true, ) } @@ -106,6 +110,7 @@ impl DomHost { scripting_enabled_for_node, shadow_root_inclusion, registry_attribute_policy, + true, ) } @@ -117,10 +122,14 @@ impl DomHost { scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool, shadow_root_inclusion: ShadowRootInclusion<'_>, registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>, + include_adopted_style_sheets: bool, ) -> Option { let shadow_root_provider = |host| { - let (shadow_root, init) = + let (shadow_root, mut init) = self.serialized_shadow_root_for_host(host, shadow_root_inclusion)?; + if !include_adopted_style_sheets { + init.set_adopted_style_sheets(None); + } let mut template_start = String::new(); self.write_shadow_root_template_start( host, diff --git a/moli-dom/src/native/host/html_serialization/tests.rs b/moli-dom/src/native/host/html_serialization/tests.rs index d0395fa18b4..80f5ead7140 100644 --- a/moli-dom/src/native/host/html_serialization/tests.rs +++ b/moli-dom/src/native/host/html_serialization/tests.rs @@ -282,3 +282,40 @@ fn shadow_excluding_outer_html_matches_native_serializer() { ); } } + +#[test] +fn copy_export_can_omit_shadow_stylesheets_without_mutating_live_metadata() { + let mut host = DomHost::from_dom(NativeDom::new_html(test_url())); + let element = host.create_element("div"); + assert!(host.append_child(host.document_node_id(), element)); + let mut init = ShadowRootInit::new("closed"); + init.set_serializable(true); + init.set_clonable(true); + init.set_adopted_style_sheets(Some("data:text/css,b%7Bcolor:red%7D".to_owned())); + let root = host.attach_shadow_root_with_init(element, init).unwrap(); + let child = host.create_element("b"); + let text = host.create_text_node("KEEP"); + assert!(host.append_child(root, child)); + assert!(host.append_child(child, text)); + let before = host + .get_html(element, &scripting_enabled, true, &[]) + .unwrap(); + let copy = host.dom().clone(); + assert_eq!( + host.get_html_from_copy(©, element, &scripting_enabled, &[root], true), + Some(before.clone()) + ); + assert_eq!( + host.get_html_from_copy(©, element, &scripting_enabled, &[root], false) + .as_deref(), + Some(concat!( + "" + )) + ); + assert!(before.contains("shadowrootadoptedstylesheets=")); + assert_eq!( + host.get_html(element, &scripting_enabled, true, &[]), + Some(before) + ); +} diff --git a/moli-dom/src/native/mod.rs b/moli-dom/src/native/mod.rs index 57b7954c752..893ee2fafdc 100644 --- a/moli-dom/src/native/mod.rs +++ b/moli-dom/src/native/mod.rs @@ -1335,6 +1335,75 @@ mod tests { ); } + #[test] + fn child_browsing_context_candidates_follow_shadow_including_subtree_order() { + let mut host = DomHost::from_dom(NativeDom::new_html(test_url())); + host.reset_html_document_shell(); + let body = host.document_body_handle().unwrap(); + // Creation/index order deliberately differs from shadow-including order. + let light_frame = host.create_element("iframe"); + let first_host = host.create_element("div"); + let last_host = host.create_element("section"); + assert!(host.append_child(body, first_host)); + assert!(host.append_child(body, last_host)); + assert!(host.append_child(first_host, light_frame)); + let root = host.attach_shadow_root(first_host, "closed").unwrap(); + let nested_host = host.create_element("div"); + let nested_root = host.attach_shadow_root(nested_host, "open").unwrap(); + let nested_frame = host.create_element("iframe"); + assert!(host.append_child(nested_root, nested_frame)); + let shadow_frame = host.create_element("iframe"); + assert!(host.append_child(root, shadow_frame)); + assert!(host.append_child(root, nested_host)); + let last_root = host.attach_shadow_root(last_host, "open").unwrap(); + let last_frame = host.create_element("iframe"); + assert!(host.append_child(last_root, last_frame)); + let detached_host = host.create_element("div"); + let detached_root = host.attach_shadow_root(detached_host, "closed").unwrap(); + let detached_frame = host.create_element("iframe"); + assert!(host.append_child(detached_root, detached_frame)); + let template = host.create_element("template"); + let contents = host.parser_template_contents_handle(template).unwrap(); + let inert_frame = host.create_element("iframe"); + assert!(host.append_child(contents, inert_frame)); + assert!(host.append_child(body, template)); + assert!(host.is_connected(template)); + assert!(!host.is_connected(inert_frame)); + let document = host.document_handle(); + assert!(host.is_connected(nested_frame)); + assert!(!host.dom().contains(document, nested_frame)); + assert_eq!( + host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order( + document + ), + vec![shadow_frame, nested_frame, light_frame, last_frame] + ); + assert_eq!( + host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order(root), + vec![shadow_frame, nested_frame] + ); + assert!(host.insert_before(body, last_host, Some(first_host))); + assert_eq!( + host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order( + document + ), + vec![last_frame, shadow_frame, nested_frame, light_frame] + ); + assert!(host.remove_child(body, first_host)); + assert_eq!( + host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order( + document + ), + vec![last_frame] + ); + assert_eq!( + host.child_browsing_context_host_candidate_handles_in_subtree_in_document_order( + first_host + ), + vec![shadow_frame, nested_frame, light_frame] + ); + } + #[test] fn option_value_collects_nested_text_without_script_content() { let mut dom = NativeDom::new_html(test_url()); diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs index d33a9a05bab..8aae0e5921c 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs @@ -10,10 +10,28 @@ impl JsContextHost { &self, frame_id: &str, dom: &NativeDom, + include_shadow_style_sheets: bool, ) -> Option { let handle = self.child_browsing_context_owner_node_id_by_frame_id(frame_id)?; let document_handle = self.child_browsing_context_document_handle(handle)?; let url = self.child_browsing_context_current_url(handle)?; + let markup = + self.document_markup_from_copy(document_handle, dom, include_shadow_style_sheets)?; + Some(ChildBrowsingContextDocumentSnapshot { + url: url.as_str().to_owned(), + markup, + }) + } + + /// Serializes `document_handle` (the page or a child frame Document) from + /// `dom`, an export copy of the live DOM, including that Document's + /// shadow roots and with its scripting flag. + pub(crate) fn document_markup_from_copy( + &self, + document_handle: crate::document_runtime::DomHandle, + dom: &NativeDom, + include_shadow_style_sheets: bool, + ) -> Option { let explicit_shadow_roots = self .dom_host() .snapshot_shadow_root_bindings() @@ -28,15 +46,12 @@ impl JsContextHost { }) .collect::>(); let scripting_enabled_for_node = |node| self.node_document_scripting_enabled(node); - let markup = self.dom_host().get_html_from_copy( + self.dom_host().get_html_from_copy( dom, document_handle, &scripting_enabled_for_node, &explicit_shadow_roots, - )?; - Some(ChildBrowsingContextDocumentSnapshot { - url: url.as_str().to_owned(), - markup, - }) + include_shadow_style_sheets, + ) } } diff --git a/moli-renderer-v8/src/runtime/page_dom.rs b/moli-renderer-v8/src/runtime/page_dom.rs index 5645cf11bdd..5199cb15abb 100644 --- a/moli-renderer-v8/src/runtime/page_dom.rs +++ b/moli-renderer-v8/src/runtime/page_dom.rs @@ -3525,11 +3525,16 @@ impl PageVm { } pub(crate) fn serialize_html(&self) -> String { + let host = self.vm().document_runtime.dom_host(); + let dom = host.dom(); + if host.snapshot_shadow_root_bindings().is_empty() { + return dom.serialize_document(); + } + // Include shadow roots as `