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
23 changes: 9 additions & 14 deletions moli-dom/src/native/host/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,35 +1578,30 @@ 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<DomHandle> {
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()
.iter()
.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::<Vec<_>>();
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
Expand Down
11 changes: 10 additions & 1 deletion moli-dom/src/native/host/html_serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
self.serialize_html_with_shadow_roots(
dom,
Expand All @@ -68,6 +70,7 @@ impl DomHost {
explicit: explicit_shadow_roots,
},
None,
include_adopted_style_sheets,
)
}

Expand All @@ -89,6 +92,7 @@ impl DomHost {
explicit: explicit_shadow_roots,
},
registry_attribute_policy,
true,
)
}

Expand All @@ -106,6 +110,7 @@ impl DomHost {
scripting_enabled_for_node,
shadow_root_inclusion,
registry_attribute_policy,
true,
)
}

Expand All @@ -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<String> {
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,
Expand Down
37 changes: 37 additions & 0 deletions moli-dom/src/native/host/html_serialization/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&copy, element, &scripting_enabled, &[root], true),
Some(before.clone())
);
assert_eq!(
host.get_html_from_copy(&copy, element, &scripting_enabled, &[root], false)
.as_deref(),
Some(concat!(
"<template shadowrootmode=\"closed\" shadowrootserializable=\"\" ",
"shadowrootclonable=\"\"><b>KEEP</b></template>"
))
);
assert!(before.contains("shadowrootadoptedstylesheets="));
assert_eq!(
host.get_html(element, &scripting_enabled, true, &[]),
Some(before)
);
}
69 changes: 69 additions & 0 deletions moli-dom/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@ impl JsContextHost {
&self,
frame_id: &str,
dom: &NativeDom,
include_shadow_style_sheets: bool,
) -> 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 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<String> {
let explicit_shadow_roots = self
.dom_host()
.snapshot_shadow_root_bindings()
Expand All @@ -28,15 +46,12 @@ impl JsContextHost {
})
.collect::<Vec<_>>();
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,
)
}
}
13 changes: 9 additions & 4 deletions moli-renderer-v8/src/runtime/page_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<template shadowrootmode>`, like the
// post-processed dump and child frames.
self.vm()
.document_runtime
.dom_host()
.dom()
.serialize_document()
.document_markup_from_copy(dom.document_node_id(), dom, true)
.unwrap_or_else(|| dom.serialize_document())
}

pub(crate) fn client_rect_for_object_id(
Expand Down
27 changes: 24 additions & 3 deletions moli-renderer-v8/src/runtime/page_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,29 @@ impl PageVm {
// 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();
let shadow_roots = self
.vm()
.document_runtime
.dom_host()
.snapshot_shadow_root_bindings();
apply_strip_options(&mut dom, top_document, options.strip);
for (_, _, child_document) in &child_frames {
apply_strip_options(&mut dom, *child_document, options.strip);
}
self.inline_child_frame_markups_into_dump_dom(&mut dom, child_frames);
// Shadow trees are separate fragments in the copy; strip them too.
for binding in &shadow_roots {
apply_strip_options(&mut dom, binding.root, options.strip);
}
self.inline_child_frame_markups_into_dump_dom(&mut dom, child_frames, !options.strip.css);

dom.serialize_document()
if shadow_roots.is_empty() {
return dom.serialize_document();
}
// Serialize shadow roots as `<template shadowrootmode>`, as child
// frames already do.
self.vm()
.document_markup_from_copy(top_document, &dom, !options.strip.css)
.unwrap_or_else(|| dom.serialize_document())
}

fn render_markdown_dump(&self, strip: RendererPageDumpStripOptions) -> String {
Expand Down Expand Up @@ -113,14 +129,19 @@ impl PageVm {
&self,
dom: &mut NativeDom,
child_frames: Vec<(String, NativeNodeId, NativeNodeId)>,
include_shadow_style_sheets: bool,
) {
// 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()
.child_browsing_context_document_snapshot_from_copy(&frame_id, dom)
.child_browsing_context_document_snapshot_from_copy(
&frame_id,
dom,
include_shadow_style_sheets,
)
else {
continue;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ async fn outer_html_document_command_includes_only_author_shadow_roots() {

let ordinary = outer_html_for_renderer_document(&page, false).await;
let serialize_html = serialize_html_for_renderer_page(&page).await;
assert_eq!(ordinary, serialize_html);
assert!(!ordinary.contains("shadowrootmode"));
assert!(!ordinary.contains("shadow"));

let including_shadow = outer_html_for_renderer_document(&page, true).await;
assert_eq!(serialize_html, including_shadow);
assert!(including_shadow.contains(concat!(
"<x-host id=\"host\"><template shadowrootmode=\"closed\">",
"<span>shadow</span></template>light</x-host>"
Expand Down
20 changes: 19 additions & 1 deletion moli-renderer-v8/src/script_vm/child_frame_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,28 @@ impl ScriptVm {
&self,
frame_id: &str,
dom: &crate::dom::native::NativeDom,
include_shadow_style_sheets: bool,
) -> Option<crate::native_bridge::ChildBrowsingContextDocumentSnapshot> {
self._context_host
.borrow()
.child_browsing_context_document_snapshot_from_copy(frame_id, dom)
.child_browsing_context_document_snapshot_from_copy(
frame_id,
dom,
include_shadow_style_sheets,
)
}

pub(crate) fn document_markup_from_copy(
&self,
document: crate::dom::NodeId,
dom: &crate::dom::native::NativeDom,
include_shadow_style_sheets: bool,
) -> Option<String> {
self._context_host.borrow().document_markup_from_copy(
document,
dom,
include_shadow_style_sheets,
)
}

pub(crate) fn take_pending_location_navigation_with_seed(
Expand Down
Loading
Loading