Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion moli-dom/src/native/host/html_serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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,
Expand All @@ -58,6 +80,7 @@ impl DomHost {
registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
) -> Option<String> {
self.serialize_html_with_shadow_roots(
&self.dom,
handle,
HtmlSerializationTarget::ChildrenOnly,
scripting_enabled_for_node,
Expand All @@ -77,6 +100,7 @@ impl DomHost {
registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
) -> Option<String> {
self.serialize_html_with_shadow_roots(
&self.dom,
handle,
HtmlSerializationTarget::IncludeNode,
scripting_enabled_for_node,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ChildBrowsingContextDocumentSnapshot> {
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<ChildBrowsingContextDocumentSnapshot> {
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
Expand All @@ -67,132 +28,15 @@ impl JsContextHost {
})
.collect::<Vec<_>>();
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 {
url: url.as_str().to_owned(),
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<ChildBrowsingContextDocumentSnapshot> {
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<ChildBrowsingContextDocumentSnapshot> {
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<ChildBrowsingContextDocumentSnapshot> {
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
}
}
45 changes: 29 additions & 16 deletions moli-renderer-v8/src/runtime/page_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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::<Vec<_>>();

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;
};
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions moli-renderer-v8/src/script_vm/child_frame_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::native_bridge::ChildBrowsingContextDocumentSnapshot> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading
Loading