diff --git a/moli-dom/src/native/host/html_serialization/mod.rs b/moli-dom/src/native/host/html_serialization/mod.rs index c4dc5950680..1e40babf9ef 100644 --- a/moli-dom/src/native/host/html_serialization/mod.rs +++ b/moli-dom/src/native/host/html_serialization/mod.rs @@ -49,6 +49,28 @@ 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. + pub fn get_html_from_copy( + &self, + dom: &NativeDom, + handle: DomHandle, + scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool, + explicit_shadow_roots: &[DomHandle], + ) -> Option { + self.serialize_html_with_shadow_roots( + dom, + handle, + HtmlSerializationTarget::ChildrenOnly, + scripting_enabled_for_node, + ShadowRootInclusion::SerializableOrExplicit { + serializable: false, + explicit: explicit_shadow_roots, + }, + None, + ) + } + pub fn get_html_with_shadow_root_registry_attribute_policy( &self, handle: DomHandle, @@ -58,6 +80,7 @@ impl DomHost { registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>, ) -> Option { self.serialize_html_with_shadow_roots( + &self.dom, handle, HtmlSerializationTarget::ChildrenOnly, scripting_enabled_for_node, @@ -77,6 +100,7 @@ impl DomHost { registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>, ) -> Option { self.serialize_html_with_shadow_roots( + &self.dom, handle, HtmlSerializationTarget::IncludeNode, scripting_enabled_for_node, @@ -87,6 +111,7 @@ impl DomHost { fn serialize_html_with_shadow_roots( &self, + dom: &NativeDom, handle: DomHandle, target: HtmlSerializationTarget, scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool, @@ -108,7 +133,7 @@ impl DomHost { Some(HtmlSerializedShadowRoot::new(shadow_root, template_start)) }; serialize_html_with_shadow_root_provider( - &self.dom, + dom, handle, target, scripting_enabled_for_node, 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 0f5e8be5313..d33a9a05bab 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 @@ -1,56 +1,17 @@ use super::super::JsContextHost; -use super::{ChildBrowsingContextDocumentSnapshot, MAX_CHILD_FRAME_TREE_DEPTH}; -use crate::document_runtime::DomHandle; -use crate::dom::native::Node; -use url::Url; +use super::ChildBrowsingContextDocumentSnapshot; +use crate::dom::native::{NativeDom, Node}; impl JsContextHost { - pub(crate) fn child_browsing_context_document_snapshot_by_frame_id( - &mut self, - frame_id: &str, - ) -> Option { - if let Some(owner_node_id) = self.child_browsing_context_owner_node_id_by_frame_id(frame_id) - { - if let Some(snapshot) = - self.live_child_browsing_context_document_snapshot(owner_node_id) - { - return Some(snapshot); - } - let snapshot = self.child_browsing_context_snapshot_markup(owner_node_id)?; - return Some(ChildBrowsingContextDocumentSnapshot { - url: snapshot.url.as_str().to_owned(), - markup: snapshot.markup, - }); - } - - for handle in self.child_browsing_context_handles_in_document_order() { - let Some(entry) = self.child_browsing_contexts.get(&handle) else { - continue; - }; - let parent_frame_id = entry.frame_id().to_owned(); - let Some(snapshot) = self.child_browsing_context_snapshot_markup(handle) else { - continue; - }; - let scripting_enabled = self.child_browsing_context_scripting_enabled(handle); - if let Some(snapshot) = self.find_detached_child_document_snapshot_by_frame_id( - &parent_frame_id, - frame_id, - &snapshot.url, - &snapshot.markup, - scripting_enabled, - 0, - ) { - return Some(snapshot); - } - } - - None - } - - fn live_child_browsing_context_document_snapshot( + /// Serializes a loaded child frame's Document from `dom`, an export copy + /// of the live DOM, with the child Document's shadow roots and scripting + /// flag, as the live child snapshot does. + pub(crate) fn child_browsing_context_document_snapshot_from_copy( &self, - handle: DomHandle, + frame_id: &str, + dom: &NativeDom, ) -> 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 explicit_shadow_roots = self @@ -67,10 +28,10 @@ impl JsContextHost { }) .collect::>(); let scripting_enabled_for_node = |node| self.node_document_scripting_enabled(node); - let markup = self.dom_host().get_html( + let markup = self.dom_host().get_html_from_copy( + dom, document_handle, &scripting_enabled_for_node, - false, &explicit_shadow_roots, )?; Some(ChildBrowsingContextDocumentSnapshot { @@ -78,121 +39,4 @@ impl JsContextHost { markup, }) } - - fn find_detached_child_document_snapshot_by_frame_id( - &mut self, - parent_frame_id: &str, - target_frame_id: &str, - document_url: &Url, - markup: &str, - scripting_enabled: bool, - depth: usize, - ) -> Option { - if depth >= MAX_CHILD_FRAME_TREE_DEPTH { - return None; - } - - let document = crate::parser::HtmlParser::with_scripting_enabled(scripting_enabled) - .parse(document_url.clone(), markup.to_owned()); - let document_base_url = document - .document() - .map(|doc| doc.base_url().clone()) - .unwrap_or_else(|| document_url.clone()); - self.find_detached_child_document_snapshot_by_frame_id_in_document( - &document, - document.document_node_id(), - &document_base_url, - parent_frame_id, - target_frame_id, - scripting_enabled, - depth, - ) - } - - fn find_detached_child_document_snapshot_by_frame_id_in_document( - &mut self, - document: &crate::dom::native::NativeDom, - root: DomHandle, - document_base_url: &Url, - parent_frame_id: &str, - target_frame_id: &str, - document_scripting_enabled: bool, - depth: usize, - ) -> Option { - let mut ordinal = 0usize; - self.find_detached_child_document_snapshot_by_frame_id_in_subtree( - document, - root, - document_base_url, - parent_frame_id, - target_frame_id, - document_scripting_enabled, - depth, - &mut ordinal, - ) - } - - fn find_detached_child_document_snapshot_by_frame_id_in_subtree( - &mut self, - document: &crate::dom::native::NativeDom, - root: DomHandle, - document_base_url: &Url, - parent_frame_id: &str, - target_frame_id: &str, - document_scripting_enabled: bool, - depth: usize, - ordinal: &mut usize, - ) -> Option { - for child in document.child_ids(root) { - if self.is_detached_child_browsing_context_host_handle(document, child) { - *ordinal += 1; - let frame_id = format!("{parent_frame_id}/child-{}", *ordinal); - let bootstrap = Self::detached_child_browsing_context_bootstrap( - document, - child, - document_base_url, - ); - let snapshot = self.detached_child_browsing_context_snapshot_markup( - &bootstrap, - document_base_url, - )?; - let scripting_enabled = Self::detached_child_document_scripting_enabled( - document, - child, - document_scripting_enabled, - &snapshot, - ); - if frame_id == target_frame_id { - return Some(ChildBrowsingContextDocumentSnapshot { - url: snapshot.url.as_str().to_owned(), - markup: snapshot.markup, - }); - } - if let Some(found) = self.find_detached_child_document_snapshot_by_frame_id( - &frame_id, - target_frame_id, - &snapshot.url, - &snapshot.markup, - scripting_enabled, - depth + 1, - ) { - return Some(found); - } - continue; - } - if let Some(found) = self.find_detached_child_document_snapshot_by_frame_id_in_subtree( - document, - child, - document_base_url, - parent_frame_id, - target_frame_id, - document_scripting_enabled, - depth, - ordinal, - ) { - return Some(found); - } - } - None - } } diff --git a/moli-renderer-v8/src/runtime/page_dump.rs b/moli-renderer-v8/src/runtime/page_dump.rs index 3c3768c3879..0d1627db5ff 100644 --- a/moli-renderer-v8/src/runtime/page_dump.rs +++ b/moli-renderer-v8/src/runtime/page_dump.rs @@ -26,10 +26,19 @@ impl PageVm { .to_owned(); inject_base_href(&mut dom, &href); } - if options.with_frames { - self.inline_child_frame_markups_into_dump_dom(&mut dom); + let child_frames = if options.with_frames { + self.vm().live_child_document_handles_in_snapshot_order() + } else { + Vec::new() + }; + // Child Documents live in the same DOM copy, so strip them before + // their markup is folded into the owners' `srcdoc` attributes. + let top_document = dom.document_node_id(); + apply_strip_options(&mut dom, top_document, options.strip); + for (_, _, child_document) in &child_frames { + apply_strip_options(&mut dom, *child_document, options.strip); } - apply_strip_options(&mut dom, options.strip); + self.inline_child_frame_markups_into_dump_dom(&mut dom, child_frames); dom.serialize_document() } @@ -100,18 +109,18 @@ impl PageVm { render_markdown_view(dom, styles, base_url.as_ref(), strip) } - fn inline_child_frame_markups_into_dump_dom(&mut self, dom: &mut NativeDom) { - let child_frames = self - .vm() - .live_child_document_handles_in_snapshot_order() - .into_iter() - .map(|(frame_id, owner_node_id, _)| (frame_id, owner_node_id)) - .collect::>(); - - for (frame_id, owner_node_id) in child_frames { + fn inline_child_frame_markups_into_dump_dom( + &self, + dom: &mut NativeDom, + child_frames: Vec<(String, NativeNodeId, NativeNodeId)>, + ) { + // Snapshot order lists a parent frame before its descendants. Walk it + // backwards so a nested owner's `srcdoc` is already in the copy when + // its parent Document is serialized. + for (frame_id, owner_node_id, _) in child_frames.into_iter().rev() { let Some(snapshot) = self - .vm_mut() - .child_browsing_context_document_snapshot_by_frame_id(&frame_id) + .vm() + .child_browsing_context_document_snapshot_from_copy(&frame_id, dom) else { continue; }; @@ -165,13 +174,17 @@ fn ensure_head_node(dom: &mut NativeDom) -> NativeNodeId { head } -fn apply_strip_options(dom: &mut NativeDom, strip: RendererPageDumpStripOptions) { +fn apply_strip_options( + dom: &mut NativeDom, + root: NativeNodeId, + strip: RendererPageDumpStripOptions, +) { if !strip.js && !strip.ui && !strip.css { return; } let mut node_ids = Vec::new(); - collect_node_ids(dom, dom.document_node_id(), &mut node_ids); + collect_node_ids(dom, root, &mut node_ids); for node_id in &node_ids { if strip.css { diff --git a/moli-renderer-v8/src/script_vm/child_frame_runtime.rs b/moli-renderer-v8/src/script_vm/child_frame_runtime.rs index 1a23ceafa8e..df8b312d7b0 100644 --- a/moli-renderer-v8/src/script_vm/child_frame_runtime.rs +++ b/moli-renderer-v8/src/script_vm/child_frame_runtime.rs @@ -433,13 +433,14 @@ impl ScriptVm { .detached_child_browsing_context_document_snapshots_for_dom_snapshot(top_frame_id) } - pub(crate) fn child_browsing_context_document_snapshot_by_frame_id( - &mut self, + pub(crate) fn child_browsing_context_document_snapshot_from_copy( + &self, frame_id: &str, + dom: &crate::dom::native::NativeDom, ) -> Option { self._context_host - .borrow_mut() - .child_browsing_context_document_snapshot_by_frame_id(frame_id) + .borrow() + .child_browsing_context_document_snapshot_from_copy(frame_id, dom) } pub(crate) fn take_pending_location_navigation_with_seed( diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/child_document_lifecycle.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/child_document_lifecycle.rs index 27f13ca10ed..d865e5bc1c1 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/child_document_lifecycle.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/child_document_lifecycle.rs @@ -2089,13 +2089,6 @@ async fn detached_frame_tree_snapshot_reuses_each_child_document_scripting_polic vec![Some("sandboxed-fallback")], "script-disallowed noscript contents must remain markup in snapshot fallback parsing" ); - let nested_frame_id = sandboxed_tree[0].child_frames[0].frame_id.clone(); - assert!( - sandboxed_vm - .child_browsing_context_document_snapshot_by_frame_id(&nested_frame_id) - .is_some(), - "frame-id lookup must use the same scripting-disabled snapshot parser" - ); } #[tokio::test] async fn child_document_open_in_dom_content_loaded_yields_to_timer() { diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs index 5d4160e3b2b..e74acabc271 100644 --- a/moli/tests/fetch_cli.rs +++ b/moli/tests/fetch_cli.rs @@ -1141,6 +1141,33 @@ fn cli_fetch_implicit_html_dump_is_quiet_by_default() -> Result<()> { Ok(()) } +#[test] +fn cli_dump_html_with_frames_nests_and_strips_child_documents() -> Result<()> { + // INNER and the shadow tree exist only in the live child Documents, never + // in the original srcdoc attributes. + let html = r#"

Top

"#; + let url = format!("data:text/html;base64,{}", BASE64_STANDARD.encode(html)); + let output = run_fetch_cli_with_args(&url, &["--with-frames"])?; + assert!(output.status.success()); + let stdout = String::from_utf8_lossy(&output.stdout); + // The inner Document is serialized inside the outer frame's srcdoc, so it + // is escaped twice. + assert!(stdout.contains("&gt;INNER&lt;"), "{stdout}"); + assert!( + stdout.contains("<template shadowrootmode="open"><b>SHADOWTEXT"), + "{stdout}" + ); + assert!(stdout.contains("var secret=1"), "{stdout}"); + + let output = run_fetch_cli_with_args(&url, &["--with-frames", "--strip-mode", "full"])?; + assert!(output.status.success()); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stdout.contains("&gt;INNER&lt;"), "{stdout}"); + assert!(!stdout.contains("secret"), "{stdout}"); + assert!(!stdout.contains("color:red"), "{stdout}"); + Ok(()) +} + #[test] fn cli_dump_full_screenshot_captures_tall_page_without_prior_layout() -> Result<()> { let html = "
";