Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(dom): organize HTML serialization modules

ldm0277dd67f1b62
diff --git a/moli-dom/src/native/host.rs b/moli-dom/src/native/host.rs
index e878400d3..6db720db3 100644
--- a/moli-dom/src/native/host.rs
+++ b/moli-dom/src/native/host.rs
@@ -14,14 +14,14 @@ use super::{Element, LiveDomNodeMetadata, NativeDom, NativeNodeId, Node, NodeTyp
mod clone;
mod collections;
mod document;
+mod html_serialization;
mod mutation;
mod parser;
mod query_index;
-mod serialize;
mod stylesheet_candidates;
mod types;
-pub use serialize::{ShadowRootInclusion, ShadowRootRegistryAttributePolicy};
+pub use html_serialization::{ShadowRootInclusion, ShadowRootRegistryAttributePolicy};
pub use stylesheet_candidates::StylesheetCandidateTreeScopeSnapshots;
pub(crate) use stylesheet_candidates::{StylesheetCandidateChanges, StylesheetCandidateRegistries};
diff --git a/moli-dom/src/native/host/html_serialization/mod.rs b/moli-dom/src/native/host/html_serialization/mod.rs
new file mode 100644
index 000000000..454bce6ad
--- /dev/null
+++ b/moli-dom/src/native/host/html_serialization/mod.rs
@@ -0,0 +1,193 @@
+use super::*;
+use crate::native::html_serialization::{
+ HtmlSerializationTarget, HtmlSerializedShadowRoot, escape_html_attribute_into_string,
+ serialize_html_with_shadow_root_provider,
+};
+
+pub type ShadowRootRegistryAttributePolicy<'a> =
+ dyn Fn(DomHandle, DomHandle, &ShadowRootInit) -> bool + 'a;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum ShadowRootInclusion<'a> {
+ None,
+ SerializableOrExplicit {
+ serializable: bool,
+ explicit: &'a [DomHandle],
+ },
+ AllAuthorForInspector,
+}
+
+impl ShadowRootInclusion<'_> {
+ fn markup_profile(self) -> ShadowRootMarkupProfile {
+ match self {
+ Self::AllAuthorForInspector => ShadowRootMarkupProfile::Inspector,
+ Self::None | Self::SerializableOrExplicit { .. } => ShadowRootMarkupProfile::WebApi,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum ShadowRootMarkupProfile {
+ WebApi,
+ Inspector,
+}
+
+impl DomHost {
+ pub fn get_html(
+ &self,
+ handle: DomHandle,
+ scripting_enabled: bool,
+ serializable_shadow_roots: bool,
+ explicit_shadow_roots: &[DomHandle],
+ ) -> Option<String> {
+ self.get_html_with_shadow_root_registry_attribute_policy(
+ handle,
+ scripting_enabled,
+ serializable_shadow_roots,
+ explicit_shadow_roots,
+ None,
+ )
+ }
+
+ pub fn get_html_with_shadow_root_registry_attribute_policy(
+ &self,
+ handle: DomHandle,
+ scripting_enabled: bool,
+ serializable_shadow_roots: bool,
+ explicit_shadow_roots: &[DomHandle],
+ registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
+ ) -> Option<String> {
+ self.serialize_html_with_shadow_roots(
+ handle,
+ HtmlSerializationTarget::ChildrenOnly,
+ scripting_enabled,
+ ShadowRootInclusion::SerializableOrExplicit {
+ serializable: serializable_shadow_roots,
+ explicit: explicit_shadow_roots,
+ },
+ registry_attribute_policy,
+ )
+ }
+
+ pub fn outer_html_with_shadow_roots(
+ &self,
+ handle: DomHandle,
+ scripting_enabled: bool,
+ shadow_root_inclusion: ShadowRootInclusion<'_>,
+ registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
+ ) -> Option<String> {
+ self.serialize_html_with_shadow_roots(
+ handle,
+ HtmlSerializationTarget::IncludeNode,
+ scripting_enabled,
+ shadow_root_inclusion,
+ registry_attribute_policy,
+ )
+ }
+
+ fn serialize_html_with_shadow_roots(
+ &self,
+ handle: DomHandle,
+ target: HtmlSerializationTarget,
+ scripting_enabled: bool,
+ shadow_root_inclusion: ShadowRootInclusion<'_>,
+ registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
+ ) -> Option<String> {
+ let shadow_root_provider = |host| {
+ let (shadow_root, init) =
+ self.serialized_shadow_root_for_host(host, shadow_root_inclusion)?;
+ let mut template_start = String::new();
+ self.write_shadow_root_template_start(
+ host,
+ shadow_root,
+ &init,
+ shadow_root_inclusion.markup_profile(),
+ &mut template_start,
+ registry_attribute_policy,
+ );
+ Some(HtmlSerializedShadowRoot::new(shadow_root, template_start))
+ };
+ serialize_html_with_shadow_root_provider(
+ &self.dom,
+ handle,
+ target,
+ scripting_enabled,
+ &shadow_root_provider,
+ )
+ }
+
+ fn serialized_shadow_root_for_host(
+ &self,
+ host: DomHandle,
+ shadow_root_inclusion: ShadowRootInclusion<'_>,
+ ) -> Option<(DomHandle, ShadowRootInit)> {
+ let state = self.shadow_roots_by_host.borrow().get(&host)?.clone();
+ let included = match shadow_root_inclusion {
+ ShadowRootInclusion::None => false,
+ ShadowRootInclusion::SerializableOrExplicit {
+ serializable,
+ explicit,
+ } => serializable && state.init.serializable() || explicit.contains(&state.handle),
+ // DomHost stores author shadow roots. Generated user-agent trees are
+ // Inspector projections owned by the renderer and never enter this map.
+ ShadowRootInclusion::AllAuthorForInspector => true,
+ };
+ if included {
+ Some((state.handle, state.init))
+ } else {
+ None
+ }
+ }
+
+ fn write_shadow_root_template_start(
+ &self,
+ host: DomHandle,
+ shadow_root: DomHandle,
+ init: &ShadowRootInit,
+ markup_profile: ShadowRootMarkupProfile,
+ out: &mut String,
+ registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
+ ) {
+ out.push_str("<template shadowrootmode=\"");
+ escape_html_attribute_into_string(init.mode(), out);
+ out.push('"');
+ if init.delegates_focus() {
+ out.push_str(" shadowrootdelegatesfocus=\"\"");
+ }
+ if init.serializable() {
+ out.push_str(" shadowrootserializable=\"\"");
+ }
+ if markup_profile == ShadowRootMarkupProfile::WebApi && init.slot_assignment() != "named" {
+ out.push_str(" shadowrootslotassignment=\"");
+ escape_html_attribute_into_string(init.slot_assignment(), out);
+ out.push('"');
+ }
+ if init.clonable() {
+ out.push_str(" shadowrootclonable=\"\"");
+ }
+ let serialize_registry_attribute = registry_attribute_policy
+ .map(|policy| policy(host, shadow_root, init))
+ .unwrap_or_else(|| init.null_custom_element_registry());
+ if serialize_registry_attribute {
+ out.push_str(" shadowrootcustomelementregistry=\"\"");
+ }
+ if markup_profile == ShadowRootMarkupProfile::WebApi
+ && let Some(reference_target) = init.reference_target()
+ {
+ out.push_str(" shadowrootreferencetarget=\"");
+ escape_html_attribute_into_string(reference_target, out);
+ out.push('"');
+ }
+ if markup_profile == ShadowRootMarkupProfile::WebApi
+ && let Some(adopted_style_sheets) = init.adopted_style_sheets()
+ {
+ out.push_str(" shadowrootadoptedstylesheets=\"");
+ escape_html_attribute_into_string(adopted_style_sheets, out);
+ out.push('"');
+ }
+ out.push('>');
+ }
+}
+
+#[cfg(test)]
+mod tests;
diff --git a/moli-dom/src/native/host/html_serialization/tests.rs b/moli-dom/src/native/host/html_serialization/tests.rs
new file mode 100644
index 000000000..5aba50820
--- /dev/null
+++ b/moli-dom/src/native/host/html_serialization/tests.rs
@@ -0,0 +1,255 @@
+use super::*;
+
+fn test_url() -> url::Url {
+ url::Url::parse("https://inspector-shadow-serialization.test/").expect("test URL")
+}
+
+#[test]
+fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
+ let element = host.create_element("section");
+ assert!(host.set_attribute(element, "id", "host"));
+ assert!(host.append_child(host.document_node_id(), element));
+
+ let mut init = ShadowRootInit::new("open");
+ init.set_delegates_focus(true);
+ init.set_serializable(true);
+ init.set_slot_assignment("manual");
+ init.set_clonable(true);
+ init.set_reference_target(Some("target&name".to_owned()));
+ init.set_adopted_style_sheets(Some("sheet-a sheet-b".to_owned()));
+ let shadow_root = host
+ .attach_shadow_root_with_init(element, init)
+ .expect("shadow root");
+ let shadow_child = host.create_element("span");
+ let shadow_text = host.create_text_node("shadow <value>");
+ assert!(host.append_child(shadow_root, shadow_child));
+ assert!(host.append_child(shadow_child, shadow_text));
+ let light_text = host.create_text_node("light & more");
+ assert!(host.append_child(element, light_text));
+
+ assert_eq!(
+ host.outer_html_with_shadow_roots(element, true, ShadowRootInclusion::None, None)
+ .as_deref(),
+ Some("<section id=\"host\">light &amp; more</section>")
+ );
+
+ let include_registry = |_: DomHandle, _: DomHandle, _: &ShadowRootInit| true;
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ element,
+ true,
+ ShadowRootInclusion::AllAuthorForInspector,
+ Some(&include_registry),
+ )
+ .as_deref(),
+ Some(concat!(
+ "<section id=\"host\"><template shadowrootmode=\"open\" ",
+ "shadowrootdelegatesfocus=\"\" shadowrootserializable=\"\" ",
+ "shadowrootclonable=\"\" shadowrootcustomelementregistry=\"\">",
+ "<span>shadow &lt;value&gt;</span></template>light &amp; more</section>"
+ ))
+ );
+
+ assert_eq!(
+ host.get_html_with_shadow_root_registry_attribute_policy(
+ element,
+ true,
+ false,
+ &[shadow_root],
+ Some(&include_registry),
+ )
+ .as_deref(),
+ Some(concat!(
+ "<template shadowrootmode=\"open\" shadowrootdelegatesfocus=\"\" ",
+ "shadowrootserializable=\"\" shadowrootslotassignment=\"manual\" ",
+ "shadowrootclonable=\"\" shadowrootcustomelementregistry=\"\" ",
+ "shadowrootreferencetarget=\"target&amp;name\" ",
+ "shadowrootadoptedstylesheets=\"sheet-a sheet-b\">",
+ "<span>shadow &lt;value&gt;</span></template>light &amp; more"
+ ))
+ );
+}
+
+#[test]
+fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
+ let outer_host = host.create_element("x-outer");
+ assert!(host.append_child(host.document_node_id(), outer_host));
+ let outer_root = host
+ .attach_shadow_root(outer_host, "open")
+ .expect("outer shadow root");
+ let inner_host = host.create_element("x-inner");
+ assert!(host.append_child(outer_root, inner_host));
+ let inner_root = host
+ .attach_shadow_root(inner_host, "closed")
+ .expect("inner shadow root");
+ let closed_child = host.create_element("b");
+ let closed_text = host.create_text_node("closed");
+ assert!(host.append_child(inner_root, closed_child));
+ assert!(host.append_child(closed_child, closed_text));
+ let inner_light = host.create_text_node("inner-light");
+ let outer_light = host.create_text_node("outer-light");
+ assert!(host.append_child(inner_host, inner_light));
+ assert!(host.append_child(outer_host, outer_light));
+
+ assert_eq!(
+ host.outer_html_with_shadow_roots(outer_host, true, ShadowRootInclusion::None, None)
+ .as_deref(),
+ Some("<x-outer>outer-light</x-outer>")
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ outer_host,
+ true,
+ ShadowRootInclusion::SerializableOrExplicit {
+ serializable: false,
+ explicit: &[outer_root],
+ },
+ None,
+ )
+ .as_deref(),
+ Some(concat!(
+ "<x-outer><template shadowrootmode=\"open\">",
+ "<x-inner>inner-light</x-inner></template>outer-light</x-outer>"
+ ))
+ );
+ let all_author = concat!(
+ "<x-outer><template shadowrootmode=\"open\"><x-inner>",
+ "<template shadowrootmode=\"closed\"><b>closed</b></template>",
+ "inner-light</x-inner></template>outer-light</x-outer>"
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ outer_host,
+ true,
+ ShadowRootInclusion::AllAuthorForInspector,
+ None,
+ )
+ .as_deref(),
+ Some(all_author)
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ host.document_node_id(),
+ true,
+ ShadowRootInclusion::AllAuthorForInspector,
+ None,
+ )
+ .as_deref(),
+ Some(all_author)
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ outer_root,
+ true,
+ ShadowRootInclusion::AllAuthorForInspector,
+ None,
+ )
+ .as_deref(),
+ Some(concat!(
+ "<x-inner><template shadowrootmode=\"closed\"><b>closed</b></template>",
+ "inner-light</x-inner>"
+ ))
+ );
+}
+
+#[test]
+fn inspector_outer_html_walks_deep_shadow_trees_iteratively() {
+ const DEPTH: usize = 4096;
+
+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
+ let root = host.create_element("div");
+ assert!(host.append_child(host.document_node_id(), root));
+ let mut shadow_host = root;
+ for index in 0..DEPTH {
+ let mode = if index % 2 == 0 { "open" } else { "closed" };
+ let shadow_root = host
+ .attach_shadow_root(shadow_host, mode)
+ .expect("deep shadow root");
+ let child_host = host.create_element("div");
+ assert!(host.append_child(shadow_root, child_host));
+ shadow_host = child_host;
+ }
+ let leaf = host.create_text_node("leaf");
+ assert!(host.append_child(shadow_host, leaf));
+
+ assert_eq!(
+ host.outer_html_with_shadow_roots(root, true, ShadowRootInclusion::None, None)
+ .as_deref(),
+ Some("<div></div>")
+ );
+ let html = host
+ .outer_html_with_shadow_roots(root, true, ShadowRootInclusion::AllAuthorForInspector, None)
+ .expect("deep inspector outer HTML");
+ assert_eq!(html.matches("<template shadowrootmode=").count(), DEPTH);
+ assert!(html.starts_with("<div><template shadowrootmode=\"open\"><div>"));
+ assert!(html.contains("<template shadowrootmode=\"closed\"><div>"));
+ assert!(html.contains("leaf</div></template></div></template>"));
+ assert!(html.ends_with("</div>"));
+}
+
+#[test]
+fn shadow_excluding_outer_html_matches_native_serializer() {
+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
+ let document = host.document_node_id();
+ let doctype = host.create_document_type("html", "public-id", "system-id");
+ let root = host.create_element("main");
+ assert!(host.set_attribute(root, "data-value", "<&\""));
+ let script = host.create_element("script");
+ let script_text = host.create_text_node("if (left < right && value > 0) {}");
+ assert!(host.append_child(script, script_text));
+ let template = host.create_element("template");
+ let template_contents = host
+ .parser_template_contents_handle(template)
+ .expect("template contents");
+ let template_child = host.create_element("span");
+ let template_text = host.create_text_node("template <text> & value");
+ assert!(host.append_child(template_contents, template_child));
+ assert!(host.append_child(template_child, template_text));
+ let input = host.create_element("input");
+ let comment = host.create_comment("comment");
+ let cdata = host.create_cdata_section("cdata <value>");
+ let processing_instruction = host.create_processing_instruction("target", "value");
+
+ assert!(host.append_child(document, doctype));
+ assert!(host.append_child(document, root));
+ for child in [
+ script,
+ template,
+ input,
+ comment,
+ cdata,
+ processing_instruction,
+ ] {
+ assert!(host.append_child(root, child));
+ }
+
+ let fragment = host.create_document_fragment();
+ let fragment_child = host.create_element("aside");
+ assert!(host.append_child(fragment, fragment_child));
+
+ for handle in [
+ document,
+ doctype,
+ root,
+ script,
+ script_text,
+ template,
+ template_contents,
+ template_child,
+ template_text,
+ input,
+ comment,
+ cdata,
+ processing_instruction,
+ fragment,
+ fragment_child,
+ ] {
+ assert_eq!(
+ host.outer_html_with_shadow_roots(handle, true, ShadowRootInclusion::None, None),
+ host.dom().outer_html(handle),
+ "shadow-excluding host serializer diverged for {handle:?}"
+ );
+ }
+}
diff --git a/moli-dom/src/native/host/serialize.rs b/moli-dom/src/native/host/serialize.rs
deleted file mode 100644
index 3021cc8b1..000000000
--- a/moli-dom/src/native/host/serialize.rs
+++ /dev/null
@@ -1,451 +0,0 @@
-use super::*;
-use crate::native::serialize::{
- HtmlSerializationOptions, HtmlSerializationTarget, HtmlSerializedShadowRoot,
- escape_html_attribute, serialize_html_into_sink,
-};
-
-pub type ShadowRootRegistryAttributePolicy<'a> =
- dyn Fn(DomHandle, DomHandle, &ShadowRootInit) -> bool + 'a;
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub enum ShadowRootInclusion<'a> {
- None,
- SerializableOrExplicit {
- serializable: bool,
- explicit: &'a [DomHandle],
- },
- AllAuthorForInspector,
-}
-
-impl ShadowRootInclusion<'_> {
- fn markup_profile(self) -> ShadowRootMarkupProfile {
- match self {
- Self::AllAuthorForInspector => ShadowRootMarkupProfile::Inspector,
- Self::None | Self::SerializableOrExplicit { .. } => ShadowRootMarkupProfile::WebApi,
- }
- }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-enum ShadowRootMarkupProfile {
- WebApi,
- Inspector,
-}
-
-impl DomHost {
- pub fn get_html(
- &self,
- handle: DomHandle,
- scripting_enabled: bool,
- serializable_shadow_roots: bool,
- explicit_shadow_roots: &[DomHandle],
- ) -> Option<String> {
- self.get_html_with_shadow_root_registry_attribute_policy(
- handle,
- scripting_enabled,
- serializable_shadow_roots,
- explicit_shadow_roots,
- None,
- )
- }
-
- pub fn get_html_with_shadow_root_registry_attribute_policy(
- &self,
- handle: DomHandle,
- scripting_enabled: bool,
- serializable_shadow_roots: bool,
- explicit_shadow_roots: &[DomHandle],
- registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
- ) -> Option<String> {
- self.serialize_html_with_shadow_roots(
- handle,
- HtmlSerializationTarget::ChildrenOnly,
- scripting_enabled,
- ShadowRootInclusion::SerializableOrExplicit {
- serializable: serializable_shadow_roots,
- explicit: explicit_shadow_roots,
- },
- registry_attribute_policy,
- )
- }
-
- pub fn outer_html_with_shadow_roots(
- &self,
- handle: DomHandle,
- scripting_enabled: bool,
- shadow_root_inclusion: ShadowRootInclusion<'_>,
- registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
- ) -> Option<String> {
- self.serialize_html_with_shadow_roots(
- handle,
- HtmlSerializationTarget::IncludeNode,
- scripting_enabled,
- shadow_root_inclusion,
- registry_attribute_policy,
- )
- }
-
- fn serialize_html_with_shadow_roots(
- &self,
- handle: DomHandle,
- target: HtmlSerializationTarget,
- scripting_enabled: bool,
- shadow_root_inclusion: ShadowRootInclusion<'_>,
- registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
- ) -> Option<String> {
- let shadow_root_provider = |host| {
- let (shadow_root, init) =
- self.serialized_shadow_root_for_host(host, shadow_root_inclusion)?;
- let mut template_start = String::new();
- self.write_shadow_root_template_start(
- host,
- shadow_root,
- &init,
- shadow_root_inclusion.markup_profile(),
- &mut template_start,
- registry_attribute_policy,
- );
- Some(HtmlSerializedShadowRoot::new(shadow_root, template_start))
- };
- let options = HtmlSerializationOptions::new(target, scripting_enabled)
- .with_shadow_root_provider(&shadow_root_provider);
- let mut html = String::new();
- serialize_html_into_sink(&self.dom, handle, options, &mut html).then_some(html)
- }
-
- fn serialized_shadow_root_for_host(
- &self,
- host: DomHandle,
- shadow_root_inclusion: ShadowRootInclusion<'_>,
- ) -> Option<(DomHandle, ShadowRootInit)> {
- let state = self.shadow_roots_by_host.borrow().get(&host)?.clone();
- let included = match shadow_root_inclusion {
- ShadowRootInclusion::None => false,
- ShadowRootInclusion::SerializableOrExplicit {
- serializable,
- explicit,
- } => serializable && state.init.serializable() || explicit.contains(&state.handle),
- // DomHost stores author shadow roots. Generated user-agent trees are
- // Inspector projections owned by the renderer and never enter this map.
- ShadowRootInclusion::AllAuthorForInspector => true,
- };
- if included {
- Some((state.handle, state.init))
- } else {
- None
- }
- }
-
- fn write_shadow_root_template_start(
- &self,
- host: DomHandle,
- shadow_root: DomHandle,
- init: &ShadowRootInit,
- markup_profile: ShadowRootMarkupProfile,
- out: &mut String,
- registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
- ) {
- out.push_str("<template shadowrootmode=\"");
- escape_html_attribute(init.mode(), out);
- out.push('"');
- if init.delegates_focus() {
- out.push_str(" shadowrootdelegatesfocus=\"\"");
- }
- if init.serializable() {
- out.push_str(" shadowrootserializable=\"\"");
- }
- if markup_profile == ShadowRootMarkupProfile::WebApi && init.slot_assignment() != "named" {
- out.push_str(" shadowrootslotassignment=\"");
- escape_html_attribute(init.slot_assignment(), out);
- out.push('"');
- }
- if init.clonable() {
- out.push_str(" shadowrootclonable=\"\"");
- }
- let serialize_registry_attribute = registry_attribute_policy
- .map(|policy| policy(host, shadow_root, init))
- .unwrap_or_else(|| init.null_custom_element_registry());
- if serialize_registry_attribute {
- out.push_str(" shadowrootcustomelementregistry=\"\"");
- }
- if markup_profile == ShadowRootMarkupProfile::WebApi
- && let Some(reference_target) = init.reference_target()
- {
- out.push_str(" shadowrootreferencetarget=\"");
- escape_html_attribute(reference_target, out);
- out.push('"');
- }
- if markup_profile == ShadowRootMarkupProfile::WebApi
- && let Some(adopted_style_sheets) = init.adopted_style_sheets()
- {
- out.push_str(" shadowrootadoptedstylesheets=\"");
- escape_html_attribute(adopted_style_sheets, out);
- out.push('"');
- }
- out.push('>');
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn test_url() -> url::Url {
- url::Url::parse("https://inspector-shadow-serialization.test/").expect("test URL")
- }
-
- #[test]
- fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
- let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
- let element = host.create_element("section");
- assert!(host.set_attribute(element, "id", "host"));
- assert!(host.append_child(host.document_node_id(), element));
-
- let mut init = ShadowRootInit::new("open");
- init.set_delegates_focus(true);
- init.set_serializable(true);
- init.set_slot_assignment("manual");
- init.set_clonable(true);
- init.set_reference_target(Some("target&name".to_owned()));
- init.set_adopted_style_sheets(Some("sheet-a sheet-b".to_owned()));
- let shadow_root = host
- .attach_shadow_root_with_init(element, init)
- .expect("shadow root");
- let shadow_child = host.create_element("span");
- let shadow_text = host.create_text_node("shadow <value>");
- assert!(host.append_child(shadow_root, shadow_child));
- assert!(host.append_child(shadow_child, shadow_text));
- let light_text = host.create_text_node("light & more");
- assert!(host.append_child(element, light_text));
-
- assert_eq!(
- host.outer_html_with_shadow_roots(element, true, ShadowRootInclusion::None, None)
- .as_deref(),
- Some("<section id=\"host\">light &amp; more</section>")
- );
-
- let include_registry = |_: DomHandle, _: DomHandle, _: &ShadowRootInit| true;
- assert_eq!(
- host.outer_html_with_shadow_roots(
- element,
- true,
- ShadowRootInclusion::AllAuthorForInspector,
- Some(&include_registry),
- )
- .as_deref(),
- Some(concat!(
- "<section id=\"host\"><template shadowrootmode=\"open\" ",
- "shadowrootdelegatesfocus=\"\" shadowrootserializable=\"\" ",
- "shadowrootclonable=\"\" shadowrootcustomelementregistry=\"\">",
- "<span>shadow &lt;value&gt;</span></template>light &amp; more</section>"
- ))
- );
-
- assert_eq!(
- host.get_html_with_shadow_root_registry_attribute_policy(
- element,
- true,
- false,
- &[shadow_root],
- Some(&include_registry),
- )
- .as_deref(),
- Some(concat!(
- "<template shadowrootmode=\"open\" shadowrootdelegatesfocus=\"\" ",
- "shadowrootserializable=\"\" shadowrootslotassignment=\"manual\" ",
- "shadowrootclonable=\"\" shadowrootcustomelementregistry=\"\" ",
- "shadowrootreferencetarget=\"target&amp;name\" ",
- "shadowrootadoptedstylesheets=\"sheet-a sheet-b\">",
- "<span>shadow &lt;value&gt;</span></template>light &amp; more"
- ))
- );
- }
-
- #[test]
- fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
- let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
- let outer_host = host.create_element("x-outer");
- assert!(host.append_child(host.document_node_id(), outer_host));
- let outer_root = host
- .attach_shadow_root(outer_host, "open")
- .expect("outer shadow root");
- let inner_host = host.create_element("x-inner");
- assert!(host.append_child(outer_root, inner_host));
- let inner_root = host
- .attach_shadow_root(inner_host, "closed")
- .expect("inner shadow root");
- let closed_child = host.create_element("b");
- let closed_text = host.create_text_node("closed");
- assert!(host.append_child(inner_root, closed_child));
- assert!(host.append_child(closed_child, closed_text));
- let inner_light = host.create_text_node("inner-light");
- let outer_light = host.create_text_node("outer-light");
- assert!(host.append_child(inner_host, inner_light));
- assert!(host.append_child(outer_host, outer_light));
-
- assert_eq!(
- host.outer_html_with_shadow_roots(outer_host, true, ShadowRootInclusion::None, None)
- .as_deref(),
- Some("<x-outer>outer-light</x-outer>")
- );
- assert_eq!(
- host.outer_html_with_shadow_roots(
- outer_host,
- true,
- ShadowRootInclusion::SerializableOrExplicit {
- serializable: false,
- explicit: &[outer_root],
- },
- None,
- )
- .as_deref(),
- Some(concat!(
- "<x-outer><template shadowrootmode=\"open\">",
- "<x-inner>inner-light</x-inner></template>outer-light</x-outer>"
- ))
- );
- let all_author = concat!(
- "<x-outer><template shadowrootmode=\"open\"><x-inner>",
- "<template shadowrootmode=\"closed\"><b>closed</b></template>",
- "inner-light</x-inner></template>outer-light</x-outer>"
- );
- assert_eq!(
- host.outer_html_with_shadow_roots(
- outer_host,
- true,
- ShadowRootInclusion::AllAuthorForInspector,
- None,
- )
- .as_deref(),
- Some(all_author)
- );
- assert_eq!(
- host.outer_html_with_shadow_roots(
- host.document_node_id(),
- true,
- ShadowRootInclusion::AllAuthorForInspector,
- None,
- )
- .as_deref(),
- Some(all_author)
- );
- assert_eq!(
- host.outer_html_with_shadow_roots(
- outer_root,
- true,
- ShadowRootInclusion::AllAuthorForInspector,
- None,
- )
- .as_deref(),
- Some(concat!(
- "<x-inner><template shadowrootmode=\"closed\"><b>closed</b></template>",
- "inner-light</x-inner>"
- ))
- );
- }
-
- #[test]
- fn inspector_outer_html_walks_deep_shadow_trees_iteratively() {
- const DEPTH: usize = 4096;
-
- let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
- let root = host.create_element("div");
- assert!(host.append_child(host.document_node_id(), root));
- let mut shadow_host = root;
- for index in 0..DEPTH {
- let mode = if index % 2 == 0 { "open" } else { "closed" };
- let shadow_root = host
- .attach_shadow_root(shadow_host, mode)
- .expect("deep shadow root");
- let child_host = host.create_element("div");
- assert!(host.append_child(shadow_root, child_host));
- shadow_host = child_host;
- }
- let leaf = host.create_text_node("leaf");
- assert!(host.append_child(shadow_host, leaf));
-
- assert_eq!(
- host.outer_html_with_shadow_roots(root, true, ShadowRootInclusion::None, None)
- .as_deref(),
- Some("<div></div>")
- );
- let html = host
- .outer_html_with_shadow_roots(
- root,
- true,
- ShadowRootInclusion::AllAuthorForInspector,
- None,
- )
- .expect("deep inspector outer HTML");
- assert_eq!(html.matches("<template shadowrootmode=").count(), DEPTH);
- assert!(html.starts_with("<div><template shadowrootmode=\"open\"><div>"));
- assert!(html.contains("<template shadowrootmode=\"closed\"><div>"));
- assert!(html.contains("leaf</div></template></div></template>"));
- assert!(html.ends_with("</div>"));
- }
-
- #[test]
- fn shadow_excluding_outer_html_matches_native_serializer() {
- let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
- let document = host.document_node_id();
- let doctype = host.create_document_type("html", "public-id", "system-id");
- let root = host.create_element("main");
- assert!(host.set_attribute(root, "data-value", "<&\""));
- let script = host.create_element("script");
- let script_text = host.create_text_node("if (left < right && value > 0) {}");
- assert!(host.append_child(script, script_text));
- let template = host.create_element("template");
- let template_contents = host
- .parser_template_contents_handle(template)
- .expect("template contents");
- let template_child = host.create_element("span");
- let template_text = host.create_text_node("template <text> & value");
- assert!(host.append_child(template_contents, template_child));
- assert!(host.append_child(template_child, template_text));
- let input = host.create_element("input");
- let comment = host.create_comment("comment");
- let cdata = host.create_cdata_section("cdata <value>");
- let processing_instruction = host.create_processing_instruction("target", "value");
-
- assert!(host.append_child(document, doctype));
- assert!(host.append_child(document, root));
- for child in [
- script,
- template,
- input,
- comment,
- cdata,
- processing_instruction,
- ] {
- assert!(host.append_child(root, child));
- }
-
- let fragment = host.create_document_fragment();
- let fragment_child = host.create_element("aside");
- assert!(host.append_child(fragment, fragment_child));
-
- for handle in [
- document,
- doctype,
- root,
- script,
- script_text,
- template,
- template_contents,
- template_child,
- template_text,
- input,
- comment,
- cdata,
- processing_instruction,
- fragment,
- fragment_child,
- ] {
- assert_eq!(
- host.outer_html_with_shadow_roots(handle, true, ShadowRootInclusion::None, None),
- host.dom().outer_html(handle),
- "shadow-excluding host serializer diverged for {handle:?}"
- );
- }
- }
-}
diff --git a/moli-dom/src/native/html_serialization/engine.rs b/moli-dom/src/native/html_serialization/engine.rs
new file mode 100644
index 000000000..9cc377a2c
--- /dev/null
+++ b/moli-dom/src/native/html_serialization/engine.rs
@@ -0,0 +1,489 @@
+use super::super::NativeDom;
+use super::super::node::{NativeNodeId, Node, NodeData};
+use super::HtmlSerializationLimitExceeded;
+
+trait HtmlSerializationSink {
+ fn push_str(&mut self, value: &str);
+ fn push(&mut self, value: char);
+ fn limit_exceeded(&self) -> bool;
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(in crate::native) enum HtmlSerializationTarget {
+ IncludeNode,
+ ChildrenOnly,
+}
+
+pub(in crate::native) struct HtmlSerializedShadowRoot {
+ root: NativeNodeId,
+ template_start: String,
+}
+
+impl HtmlSerializedShadowRoot {
+ pub(in crate::native) fn new(root: NativeNodeId, template_start: String) -> Self {
+ Self {
+ root,
+ template_start,
+ }
+ }
+}
+
+trait HtmlShadowRootProvider {
+ fn serialized_shadow_root_for_host(
+ &self,
+ host: NativeNodeId,
+ ) -> Option<HtmlSerializedShadowRoot>;
+}
+
+impl<F> HtmlShadowRootProvider for F
+where
+ F: Fn(NativeNodeId) -> Option<HtmlSerializedShadowRoot>,
+{
+ fn serialized_shadow_root_for_host(
+ &self,
+ host: NativeNodeId,
+ ) -> Option<HtmlSerializedShadowRoot> {
+ self(host)
+ }
+}
+
+#[derive(Clone, Copy)]
+struct HtmlSerializationOptions<'a> {
+ target: HtmlSerializationTarget,
+ scripting_enabled: bool,
+ shadow_root_provider: Option<&'a dyn HtmlShadowRootProvider>,
+}
+
+impl<'a> HtmlSerializationOptions<'a> {
+ const fn new(target: HtmlSerializationTarget, scripting_enabled: bool) -> Self {
+ Self {
+ target,
+ scripting_enabled,
+ shadow_root_provider: None,
+ }
+ }
+
+ const fn with_shadow_root_provider(
+ mut self,
+ shadow_root_provider: &'a dyn HtmlShadowRootProvider,
+ ) -> Self {
+ self.shadow_root_provider = Some(shadow_root_provider);
+ self
+ }
+}
+
+impl HtmlSerializationSink for String {
+ fn push_str(&mut self, value: &str) {
+ String::push_str(self, value);
+ }
+
+ fn push(&mut self, value: char) {
+ String::push(self, value);
+ }
+
+ fn limit_exceeded(&self) -> bool {
+ false
+ }
+}
+
+fn escape_html_text<S>(value: &str, out: &mut S)
+where
+ S: HtmlSerializationSink + ?Sized,
+{
+ for ch in value.chars() {
+ if out.limit_exceeded() {
+ return;
+ }
+ match ch {
+ '&' => out.push_str("&amp;"),
+ '<' => out.push_str("&lt;"),
+ '>' => out.push_str("&gt;"),
+ '\u{00A0}' => out.push_str("&nbsp;"),
+ _ => out.push(ch),
+ }
+ }
+}
+
+fn escape_html_attribute<S>(value: &str, out: &mut S)
+where
+ S: HtmlSerializationSink + ?Sized,
+{
+ for ch in value.chars() {
+ if out.limit_exceeded() {
+ return;
+ }
+ match ch {
+ '&' => out.push_str("&amp;"),
+ '"' => out.push_str("&quot;"),
+ '<' => out.push_str("&lt;"),
+ '>' => out.push_str("&gt;"),
+ _ => out.push(ch),
+ }
+ }
+}
+
+fn serialize_cdata_section<S>(value: &str, out: &mut S, raw_text_parent: bool, html_document: bool)
+where
+ S: HtmlSerializationSink + ?Sized,
+{
+ if html_document {
+ if raw_text_parent {
+ out.push_str(value);
+ } else {
+ escape_html_text(value, out);
+ }
+ } else {
+ out.push_str("<![CDATA[");
+ out.push_str(value);
+ out.push_str("]]>");
+ }
+}
+
+fn is_void_html_element(namespace: &str, local_name: &str) -> bool {
+ namespace == "http://www.w3.org/1999/xhtml"
+ && matches!(
+ local_name,
+ "area"
+ | "base"
+ | "basefont"
+ | "bgsound"
+ | "br"
+ | "col"
+ | "embed"
+ | "frame"
+ | "hr"
+ | "img"
+ | "input"
+ | "keygen"
+ | "link"
+ | "meta"
+ | "param"
+ | "source"
+ | "track"
+ | "wbr"
+ )
+}
+
+enum HtmlSerializationFrame<'a> {
+ Node(NativeNodeId),
+ Children(NativeNodeId),
+ ShadowRootTemplate(HtmlSerializedShadowRoot),
+ CloseElement(&'a str),
+ CloseShadowRootTemplate,
+}
+
+fn serialize_html_into_sink<S>(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ options: HtmlSerializationOptions<'_>,
+ out: &mut S,
+) -> bool
+where
+ S: HtmlSerializationSink,
+{
+ let Some(node) = dom.node(node_id) else {
+ return false;
+ };
+ if options.target == HtmlSerializationTarget::ChildrenOnly
+ && node
+ .as_element()
+ .is_some_and(|element| is_void_html_element(element.namespace(), element.local_name()))
+ {
+ return true;
+ }
+
+ let mut stack = match options.target {
+ HtmlSerializationTarget::IncludeNode => vec![HtmlSerializationFrame::Node(node_id)],
+ HtmlSerializationTarget::ChildrenOnly => {
+ let mut stack = vec![HtmlSerializationFrame::Children(node_id)];
+ push_shadow_root_frame(node_id, options.shadow_root_provider, &mut stack);
+ stack
+ }
+ };
+
+ while !out.limit_exceeded() {
+ let Some(frame) = stack.pop() else {
+ break;
+ };
+ match frame {
+ HtmlSerializationFrame::Node(node_id) => {
+ serialize_html_node_frame(dom, node_id, options, out, &mut stack);
+ }
+ HtmlSerializationFrame::Children(node_id) => {
+ push_child_html_serialization_frames(dom, node_id, &mut stack);
+ }
+ HtmlSerializationFrame::ShadowRootTemplate(shadow_root) => {
+ out.push_str(&shadow_root.template_start);
+ stack.push(HtmlSerializationFrame::CloseShadowRootTemplate);
+ stack.push(HtmlSerializationFrame::Children(shadow_root.root));
+ }
+ HtmlSerializationFrame::CloseElement(local_name) => {
+ out.push_str("</");
+ out.push_str(local_name);
+ out.push('>');
+ }
+ HtmlSerializationFrame::CloseShadowRootTemplate => {
+ out.push_str("</template>");
+ }
+ }
+ }
+ true
+}
+
+fn serialize_html_node_frame<'a, S>(
+ dom: &'a NativeDom,
+ node_id: NativeNodeId,
+ options: HtmlSerializationOptions<'_>,
+ out: &mut S,
+ stack: &mut Vec<HtmlSerializationFrame<'a>>,
+) where
+ S: HtmlSerializationSink,
+{
+ let Some(node) = dom.node(node_id) else {
+ return;
+ };
+ match node.data() {
+ NodeData::Document(_) | NodeData::DocumentFragment(_) => {
+ stack.push(HtmlSerializationFrame::Children(node_id));
+ }
+ NodeData::DocumentType(document_type) => {
+ out.push_str("<!DOCTYPE ");
+ out.push_str(document_type.name());
+ if !document_type.public_id().is_empty() || !document_type.system_id().is_empty() {
+ out.push_str(" PUBLIC \"");
+ out.push_str(document_type.public_id());
+ out.push_str("\" \"");
+ out.push_str(document_type.system_id());
+ out.push('"');
+ }
+ out.push('>');
+ }
+ NodeData::Element(element) => {
+ out.push('<');
+ out.push_str(element.local_name());
+ if let Some(is_name) = element.custom_element_is_name()
+ && !element.has_attribute("is")
+ {
+ out.push_str(" is=\"");
+ escape_html_attribute(is_name, out);
+ out.push('"');
+ }
+ for attribute in element.attributes() {
+ if out.limit_exceeded() {
+ return;
+ }
+ out.push(' ');
+ attribute.push_html_serialized_name(|part| out.push_str(part));
+ out.push_str("=\"");
+ escape_html_attribute(attribute.value(), out);
+ out.push('"');
+ }
+ out.push('>');
+
+ if !is_void_html_element(element.namespace(), element.local_name()) {
+ stack.push(HtmlSerializationFrame::CloseElement(element.local_name()));
+ stack.push(HtmlSerializationFrame::Children(node_id));
+ push_shadow_root_frame(node_id, options.shadow_root_provider, stack);
+ }
+ }
+ NodeData::Text(text) => {
+ if text_data_serializes_literally(dom, node_id, options.scripting_enabled) {
+ out.push_str(text.data());
+ } else {
+ escape_html_text(text.data(), out);
+ }
+ }
+ NodeData::CDataSection(cdata) => {
+ serialize_cdata_section(
+ cdata.data(),
+ out,
+ text_data_serializes_literally(dom, node_id, options.scripting_enabled),
+ dom.node_document_is_html_document(node_id).unwrap_or(false),
+ );
+ }
+ NodeData::Comment(comment) => {
+ out.push_str("<!--");
+ out.push_str(comment.data());
+ out.push_str("-->");
+ }
+ NodeData::ProcessingInstruction(processing_instruction) => {
+ out.push_str("<?");
+ out.push_str(processing_instruction.target());
+ if !processing_instruction.data().is_empty() {
+ out.push(' ');
+ out.push_str(processing_instruction.data());
+ }
+ out.push_str("?>");
+ }
+ }
+}
+
+fn push_child_html_serialization_frames<'a>(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ stack: &mut Vec<HtmlSerializationFrame<'a>>,
+) {
+ let children_root = dom
+ .node(node_id)
+ .and_then(Node::as_element)
+ .and_then(|element| element.template_contents())
+ .unwrap_or(node_id);
+ stack.extend(
+ dom.child_ids_reversed(children_root)
+ .map(HtmlSerializationFrame::Node),
+ );
+}
+
+fn push_shadow_root_frame(
+ host: NativeNodeId,
+ shadow_root_provider: Option<&dyn HtmlShadowRootProvider>,
+ stack: &mut Vec<HtmlSerializationFrame<'_>>,
+) {
+ let Some(shadow_root) =
+ shadow_root_provider.and_then(|provider| provider.serialized_shadow_root_for_host(host))
+ else {
+ return;
+ };
+ stack.push(HtmlSerializationFrame::ShadowRootTemplate(shadow_root));
+}
+
+fn text_data_serializes_literally(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ scripting_enabled: bool,
+) -> bool {
+ if !dom.node_document_is_html_document(node_id).unwrap_or(false) {
+ return false;
+ }
+ let Some(parent) = dom
+ .parent_node(node_id)
+ .and_then(|parent| dom.node(parent))
+ .and_then(Node::as_element)
+ else {
+ return false;
+ };
+ if parent.namespace() != "http://www.w3.org/1999/xhtml" {
+ return false;
+ }
+ matches!(
+ parent.local_name(),
+ "style" | "script" | "xmp" | "iframe" | "noembed" | "noframes" | "plaintext"
+ ) || scripting_enabled && parent.local_name() == "noscript"
+}
+
+struct BoundedHtmlSerialization {
+ output: String,
+ max_bytes: usize,
+ exceeded: bool,
+}
+
+impl BoundedHtmlSerialization {
+ fn new(max_bytes: usize) -> Self {
+ Self {
+ output: String::new(),
+ max_bytes,
+ exceeded: false,
+ }
+ }
+
+ fn finish(self) -> Result<String, HtmlSerializationLimitExceeded> {
+ if self.exceeded {
+ Err(HtmlSerializationLimitExceeded {
+ max_bytes: self.max_bytes,
+ })
+ } else {
+ Ok(self.output)
+ }
+ }
+}
+
+impl HtmlSerializationSink for BoundedHtmlSerialization {
+ fn push_str(&mut self, value: &str) {
+ if self.exceeded {
+ return;
+ }
+ if self
+ .output
+ .len()
+ .checked_add(value.len())
+ .is_none_or(|length| length > self.max_bytes)
+ {
+ self.exceeded = true;
+ return;
+ }
+ self.output.push_str(value);
+ }
+
+ fn push(&mut self, value: char) {
+ if self.exceeded {
+ return;
+ }
+ if self
+ .output
+ .len()
+ .checked_add(value.len_utf8())
+ .is_none_or(|length| length > self.max_bytes)
+ {
+ self.exceeded = true;
+ return;
+ }
+ self.output.push(value);
+ }
+
+ fn limit_exceeded(&self) -> bool {
+ self.exceeded
+ }
+}
+
+pub(super) fn serialize_html(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ target: HtmlSerializationTarget,
+ scripting_enabled: bool,
+) -> Option<String> {
+ let mut html = String::new();
+ serialize_html_into_sink(
+ dom,
+ node_id,
+ HtmlSerializationOptions::new(target, scripting_enabled),
+ &mut html,
+ )
+ .then_some(html)
+}
+
+pub(in crate::native) fn serialize_html_with_shadow_root_provider<F>(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ target: HtmlSerializationTarget,
+ scripting_enabled: bool,
+ shadow_root_provider: &F,
+) -> Option<String>
+where
+ F: Fn(NativeNodeId) -> Option<HtmlSerializedShadowRoot>,
+{
+ let options = HtmlSerializationOptions::new(target, scripting_enabled)
+ .with_shadow_root_provider(shadow_root_provider);
+ let mut html = String::new();
+ serialize_html_into_sink(dom, node_id, options, &mut html).then_some(html)
+}
+
+pub(in crate::native) fn escape_html_attribute_into_string(value: &str, out: &mut String) {
+ escape_html_attribute(value, out);
+}
+
+pub(super) fn serialize_html_with_limit(
+ dom: &NativeDom,
+ node_id: NativeNodeId,
+ max_bytes: usize,
+) -> Result<Option<String>, HtmlSerializationLimitExceeded> {
+ let mut out = BoundedHtmlSerialization::new(max_bytes);
+ if !serialize_html_into_sink(
+ dom,
+ node_id,
+ HtmlSerializationOptions::new(HtmlSerializationTarget::IncludeNode, true),
+ &mut out,
+ ) {
+ return Ok(None);
+ }
+ out.finish().map(Some)
+}
diff --git a/moli-dom/src/native/html_serialization/mod.rs b/moli-dom/src/native/html_serialization/mod.rs
new file mode 100644
index 000000000..fe7a953a9
--- /dev/null
+++ b/moli-dom/src/native/html_serialization/mod.rs
@@ -0,0 +1,92 @@
+mod engine;
+
+#[cfg(test)]
+mod tests;
+
+use super::{NativeDom, NativeNodeId};
+pub(super) use engine::{
+ HtmlSerializationTarget, HtmlSerializedShadowRoot, escape_html_attribute_into_string,
+ serialize_html_with_shadow_root_provider,
+};
+use engine::{serialize_html, serialize_html_with_limit};
+
+/// A bounded serialization stopped before appending bytes past its limit.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct HtmlSerializationLimitExceeded {
+ pub max_bytes: usize,
+}
+
+impl std::fmt::Display for HtmlSerializationLimitExceeded {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ formatter,
+ "serialized HTML exceeds the {}-byte output limit",
+ self.max_bytes
+ )
+ }
+}
+
+impl std::error::Error for HtmlSerializationLimitExceeded {}
+
+impl NativeDom {
+ pub fn serialize_document(&self) -> String {
+ self.serialize_document_with_scripting_enabled(true)
+ }
+
+ pub fn serialize_document_with_scripting_enabled(&self, scripting_enabled: bool) -> String {
+ serialize_html(
+ self,
+ self.document_node_id,
+ HtmlSerializationTarget::ChildrenOnly,
+ scripting_enabled,
+ )
+ .unwrap_or_default()
+ }
+
+ pub fn outer_html(&self, node_id: NativeNodeId) -> Option<String> {
+ self.outer_html_with_scripting_enabled(node_id, true)
+ }
+
+ pub fn outer_html_with_scripting_enabled(
+ &self,
+ node_id: NativeNodeId,
+ scripting_enabled: bool,
+ ) -> Option<String> {
+ serialize_html(
+ self,
+ node_id,
+ HtmlSerializationTarget::IncludeNode,
+ scripting_enabled,
+ )
+ }
+
+ /// Serializes one subtree without ever growing the output beyond `max_bytes`.
+ ///
+ /// This is intended for bounded derived consumers such as a fresh inline
+ /// SVG image parse. Web-exposed `outerHTML` continues to use the unbounded
+ /// serializer because truncation would violate its string contract.
+ pub fn outer_html_with_limit(
+ &self,
+ node_id: NativeNodeId,
+ max_bytes: usize,
+ ) -> Result<Option<String>, HtmlSerializationLimitExceeded> {
+ serialize_html_with_limit(self, node_id, max_bytes)
+ }
+
+ pub fn inner_html(&self, node_id: NativeNodeId) -> Option<String> {
+ self.inner_html_with_scripting_enabled(node_id, true)
+ }
+
+ pub fn inner_html_with_scripting_enabled(
+ &self,
+ node_id: NativeNodeId,
+ scripting_enabled: bool,
+ ) -> Option<String> {
+ serialize_html(
+ self,
+ node_id,
+ HtmlSerializationTarget::ChildrenOnly,
+ scripting_enabled,
+ )
+ }
+}
diff --git a/moli-dom/src/native/html_serialization/tests.rs b/moli-dom/src/native/html_serialization/tests.rs
new file mode 100644
index 000000000..e822072d9
--- /dev/null
+++ b/moli-dom/src/native/html_serialization/tests.rs
@@ -0,0 +1,250 @@
+use super::*;
+use crate::native::DomHost;
+
+fn test_url() -> url::Url {
+ url::Url::parse("https://serialization.test/").expect("test URL")
+}
+
+#[test]
+fn html_serializers_share_the_complete_void_element_set() {
+ let mut dom = NativeDom::new_html(test_url());
+ let container = dom.create_element("div");
+ let mut expected = String::new();
+ let mut void_elements = Vec::new();
+ for local_name in [
+ "area", "base", "basefont", "bgsound", "br", "col", "embed", "frame", "hr", "img", "input",
+ "keygen", "link", "meta", "param", "source", "track", "wbr",
+ ] {
+ let element = dom.create_element(local_name);
+ let ignored_child = dom.create_element("span");
+ assert!(dom.append_child(element, ignored_child));
+ assert_eq!(dom.outer_html(element), Some(format!("<{local_name}>")));
+ assert_eq!(dom.inner_html(element).as_deref(), Some(""));
+ assert!(dom.append_child(container, element));
+ expected.push_str(&format!("<{local_name}>"));
+ void_elements.push(element);
+ }
+ assert_eq!(
+ dom.inner_html(container).as_deref(),
+ Some(expected.as_str())
+ );
+
+ let foreign_param = dom
+ .create_element_ns(Some("http://www.w3.org/2000/svg"), "param")
+ .expect("SVG element");
+ assert_eq!(
+ dom.outer_html(foreign_param).as_deref(),
+ Some("<param></param>")
+ );
+
+ let mut host = DomHost::from_dom(dom);
+ let param = host.create_element("param");
+ assert!(host.append_child(container, param));
+ assert_eq!(
+ host.get_html(container, true, false, &[]).as_deref(),
+ Some(format!("{expected}<param>").as_str())
+ );
+ for element in void_elements {
+ assert_eq!(
+ host.get_html(element, true, false, &[]).as_deref(),
+ Some("")
+ );
+ }
+}
+
+#[test]
+fn html_text_serialization_uses_the_actual_parent_and_scripting_mode() {
+ let mut dom = NativeDom::new_html(test_url());
+ let container = dom.create_element("main");
+ let mut cases = Vec::new();
+ for (local_name, literal_with_scripting, literal_without_scripting) in [
+ ("style", true, true),
+ ("script", true, true),
+ ("xmp", true, true),
+ ("iframe", true, true),
+ ("noembed", true, true),
+ ("noframes", true, true),
+ ("plaintext", true, true),
+ ("noscript", true, false),
+ ("textarea", false, false),
+ ("title", false, false),
+ ("div", false, false),
+ ] {
+ let element = dom.create_element(local_name);
+ let text = dom.create_text_node("<&");
+ assert!(dom.append_child(element, text));
+ assert!(dom.append_child(container, element));
+ cases.push((
+ element,
+ local_name,
+ literal_with_scripting,
+ literal_without_scripting,
+ ));
+ }
+
+ for &(element, _, literal_with_scripting, literal_without_scripting) in &cases {
+ assert_eq!(
+ dom.inner_html_with_scripting_enabled(element, true)
+ .as_deref(),
+ Some(if literal_with_scripting {
+ "<&"
+ } else {
+ "&lt;&amp;"
+ })
+ );
+ assert_eq!(
+ dom.inner_html_with_scripting_enabled(element, false)
+ .as_deref(),
+ Some(if literal_without_scripting {
+ "<&"
+ } else {
+ "&lt;&amp;"
+ })
+ );
+ }
+
+ let host = DomHost::from_dom(dom);
+ for &(element, local_name, literal_with_scripting, literal_without_scripting) in &cases {
+ for (scripting_enabled, literal) in [
+ (true, literal_with_scripting),
+ (false, literal_without_scripting),
+ ] {
+ let contents = if literal { "<&" } else { "&lt;&amp;" };
+ assert_eq!(
+ host.get_html_with_shadow_root_registry_attribute_policy(
+ element,
+ scripting_enabled,
+ false,
+ &[],
+ None,
+ )
+ .as_deref(),
+ Some(contents),
+ "children-only serialization diverged for {local_name}"
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ element,
+ scripting_enabled,
+ crate::native::ShadowRootInclusion::None,
+ None,
+ ),
+ Some(format!("<{local_name}>{contents}</{local_name}>")),
+ "node serialization diverged for {local_name}"
+ );
+ }
+ }
+}
+
+#[test]
+fn bounded_outer_html_stops_before_exceeding_the_output_limit() {
+ let mut dom = NativeDom::new_html(test_url());
+ let element = dom.create_element("div");
+ let expected = "<div></div>";
+
+ assert_eq!(
+ dom.outer_html_with_limit(element, expected.len()),
+ Ok(Some(expected.to_owned()))
+ );
+ assert_eq!(
+ dom.outer_html_with_limit(element, expected.len() - 1),
+ Err(HtmlSerializationLimitExceeded {
+ max_bytes: expected.len() - 1,
+ })
+ );
+}
+
+#[test]
+fn html_serializers_apply_attribute_serialized_name_rules() {
+ let mut dom = NativeDom::new_html(test_url());
+ let container = dom.create_element("section");
+ let element = dom
+ .create_element_ns(Some("urn:element"), "div")
+ .expect("namespaced element");
+ assert!(dom.set_attribute_ns(
+ element,
+ Some("http://www.w3.org/XML/1998/namespace"),
+ Some("alternate"),
+ "lang",
+ "en-us",
+ ));
+ assert!(dom.set_attribute_ns(
+ element,
+ Some("http://www.w3.org/2000/xmlns/"),
+ None,
+ "binding",
+ "urn:binding",
+ ));
+ assert!(dom.set_attribute_ns(
+ element,
+ Some("http://www.w3.org/2000/xmlns/"),
+ None,
+ "xmlns",
+ "urn:default",
+ ));
+ assert!(dom.set_attribute_ns(
+ element,
+ Some("http://www.w3.org/1999/xlink"),
+ Some("alternate"),
+ "href",
+ "target",
+ ));
+ assert!(dom.set_attribute_ns(element, Some("urn:custom"), Some("p"), "attr", "value",));
+ assert!(dom.append_child(container, element));
+
+ let expected = concat!(
+ "<div xml:lang=\"en-us\" xmlns:binding=\"urn:binding\" ",
+ "xmlns=\"urn:default\" xlink:href=\"target\" p:attr=\"value\"></div>"
+ );
+ assert_eq!(dom.inner_html(container).as_deref(), Some(expected));
+
+ let host = DomHost::from_dom(dom);
+ assert_eq!(
+ host.get_html(container, true, false, &[]).as_deref(),
+ Some(expected)
+ );
+}
+
+#[test]
+fn html_serializers_escape_adopted_cdata_as_text() {
+ const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";
+ const XMLNS_NAMESPACE: &str = "http://www.w3.org/2000/xmlns/";
+
+ let mut xml_dom = NativeDom::new_xml(test_url());
+ let xml_svg = xml_dom
+ .create_element_ns(Some(SVG_NAMESPACE), "svg")
+ .expect("XML SVG element");
+ assert!(
+ xml_dom.set_attribute_ns(xml_svg, Some(XMLNS_NAMESPACE), None, "xmlns", SVG_NAMESPACE,)
+ );
+ let xml_cdata = xml_dom.create_cdata_section("<img>&");
+ assert!(xml_dom.append_child(xml_svg, xml_cdata));
+ assert_eq!(
+ xml_dom.outer_html(xml_svg).as_deref(),
+ Some(r#"<svg xmlns="http://www.w3.org/2000/svg"><![CDATA[<img>&]]></svg>"#)
+ );
+
+ let mut html_dom = NativeDom::new_html(test_url());
+ let html_svg = html_dom
+ .create_element_ns(Some(SVG_NAMESPACE), "svg")
+ .expect("HTML-document SVG element");
+ assert!(html_dom.set_attribute_ns(
+ html_svg,
+ Some(XMLNS_NAMESPACE),
+ None,
+ "xmlns",
+ SVG_NAMESPACE,
+ ));
+ let adopted_cdata = html_dom.create_cdata_section("<img>&");
+ assert!(html_dom.append_child(html_svg, adopted_cdata));
+ assert_eq!(
+ html_dom.outer_html(html_svg).as_deref(),
+ Some(r#"<svg xmlns="http://www.w3.org/2000/svg">&lt;img&gt;&amp;</svg>"#)
+ );
+
+ let host = DomHost::from_dom(html_dom);
+ assert_eq!(
+ host.get_html(html_svg, true, false, &[]).as_deref(),
+ Some("&lt;img&gt;&amp;")
+ );
+}
diff --git a/moli-dom/src/native/mod.rs b/moli-dom/src/native/mod.rs
index 9a6d4eefb..21a0ab8a3 100644
--- a/moli-dom/src/native/mod.rs
+++ b/moli-dom/src/native/mod.rs
@@ -2,9 +2,10 @@ mod attributes;
mod document;
mod element;
mod host;
+mod html_serialization;
mod node;
mod queries;
-mod serialize;
+mod scripts;
use std::sync::Arc;
@@ -29,11 +30,11 @@ pub use host::{
HostElementSnapshot, ShadowRootBindingSnapshot, ShadowRootInclusion, ShadowRootInit,
ShadowRootRegistryAttributePolicy,
};
+pub use html_serialization::HtmlSerializationLimitExceeded;
pub use node::{
CDataSection, Comment, LiveDomNodeMetadata, NativeNodeId, Node, NodeData, NodeFlags, NodeType,
ProcessingInstruction, Text,
};
-pub use serialize::HtmlSerializationLimitExceeded;
// Node IDs remain dense indexes, while immutable page snapshots share complete
// chunks. A mutation detaches only its 256-node chunk, bounding copy-on-write
@@ -214,6 +215,10 @@ impl NativeDom {
&self.parse_errors
}
+ pub fn push_parse_error(&mut self, error: String) {
+ self.parse_errors.push(error);
+ }
+
pub fn node(&self, node_id: NativeNodeId) -> Option<&Node> {
self.nodes.get(node_id.index())
}
diff --git a/moli-dom/src/native/queries.rs b/moli-dom/src/native/queries.rs
index 7b767627e..84853716d 100644
--- a/moli-dom/src/native/queries.rs
+++ b/moli-dom/src/native/queries.rs
@@ -4,19 +4,17 @@ use super::node::{NativeNodeId, Node};
use crate::forms::parse_non_negative_integer_prefix;
impl NativeDom {
- pub fn connected_script_handles(&self, root: NativeNodeId) -> Vec<NativeNodeId> {
- let mut handles = Vec::new();
- let mut stack = vec![root];
- while let Some(handle) = stack.pop() {
- let Some(node) = self.node(handle) else {
- continue;
- };
- if node.flags().connected() && node.is_script_element() {
- handles.push(handle);
- }
- stack.extend(self.child_ids_reversed(handle));
+ pub fn is_html_element_named(&self, node_id: NativeNodeId, local_name: &str) -> bool {
+ self.node(node_id)
+ .is_some_and(|node| node.is_html_element_named(local_name))
+ }
+
+ pub fn option_value(&self, node_id: NativeNodeId) -> Option<String> {
+ let element = self.node(node_id).and_then(Node::as_element)?;
+ if !element.is_html_option() {
+ return None;
}
- handles
+ Some(element.option_value(self, node_id))
}
pub fn child_element_nodes(&self, root: NativeNodeId) -> Vec<NativeNodeId> {
diff --git a/moli-dom/src/native/scripts.rs b/moli-dom/src/native/scripts.rs
new file mode 100644
index 000000000..6d2118170
--- /dev/null
+++ b/moli-dom/src/native/scripts.rs
@@ -0,0 +1,73 @@
+use super::{NativeDom, NativeNodeId};
+
+impl NativeDom {
+ pub fn connected_script_handles(&self, root: NativeNodeId) -> Vec<NativeNodeId> {
+ let mut handles = Vec::new();
+ let mut stack = vec![root];
+ while let Some(handle) = stack.pop() {
+ let Some(node) = self.node(handle) else {
+ continue;
+ };
+ if node.flags().connected() && node.is_script_element() {
+ handles.push(handle);
+ }
+ stack.extend(self.child_ids_reversed(handle));
+ }
+ handles
+ }
+
+ pub fn script_handles(&self) -> Vec<NativeNodeId> {
+ self.nodes
+ .iter()
+ .filter_map(|node| node.is_script_element().then_some(node.id()))
+ .collect()
+ }
+
+ pub fn script_node_ids(&self) -> Vec<NativeNodeId> {
+ self.script_handles()
+ }
+
+ pub fn document_order_script_handles(&self) -> Vec<NativeNodeId> {
+ let mut script_handles = Vec::new();
+ let mut stack = vec![self.document_node_id];
+ while let Some(node_id) = stack.pop() {
+ let Some(node) = self.node(node_id) else {
+ continue;
+ };
+ if node.is_script_element() {
+ script_handles.push(node_id);
+ }
+ stack.extend(self.child_ids_reversed(node_id));
+ }
+ script_handles
+ }
+
+ pub fn document_order_script_node_ids(&self) -> Vec<NativeNodeId> {
+ self.document_order_script_handles()
+ }
+
+ pub fn script_src(&self, node_id: NativeNodeId) -> Option<&str> {
+ self.node(node_id)?.as_element()?.script_source_attribute()
+ }
+
+ pub fn script_text(&self, node_id: NativeNodeId) -> Option<String> {
+ let script_node = self.node(node_id)?;
+ let element = script_node.as_element()?;
+ if !element.is_script_element() {
+ return None;
+ }
+
+ let mut script_text = String::new();
+ for child_id in script_node.child_ids(self) {
+ let Some(child) = self.node(child_id) else {
+ continue;
+ };
+
+ if let Some(text) = child.as_text() {
+ script_text.push_str(text.data());
+ }
+ }
+
+ (!script_text.is_empty()).then_some(script_text)
+ }
+}
diff --git a/moli-dom/src/native/serialize.rs b/moli-dom/src/native/serialize.rs
deleted file mode 100644
index 098838377..000000000
--- a/moli-dom/src/native/serialize.rs
+++ /dev/null
@@ -1,864 +0,0 @@
-use super::NativeDom;
-use super::node::{NativeNodeId, Node, NodeData};
-
-/// A bounded serialization stopped before appending bytes past its limit.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct HtmlSerializationLimitExceeded {
- pub max_bytes: usize,
-}
-
-impl std::fmt::Display for HtmlSerializationLimitExceeded {
- fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(
- formatter,
- "serialized HTML exceeds the {}-byte output limit",
- self.max_bytes
- )
- }
-}
-
-impl std::error::Error for HtmlSerializationLimitExceeded {}
-
-pub(super) trait HtmlSerializationSink {
- fn push_str(&mut self, value: &str);
- fn push(&mut self, value: char);
- fn limit_exceeded(&self) -> bool;
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub(super) enum HtmlSerializationTarget {
- IncludeNode,
- ChildrenOnly,
-}
-
-pub(super) struct HtmlSerializedShadowRoot {
- root: NativeNodeId,
- template_start: String,
-}
-
-impl HtmlSerializedShadowRoot {
- pub(super) fn new(root: NativeNodeId, template_start: String) -> Self {
- Self {
- root,
- template_start,
- }
- }
-}
-
-pub(super) trait HtmlShadowRootProvider {
- fn serialized_shadow_root_for_host(
- &self,
- host: NativeNodeId,
- ) -> Option<HtmlSerializedShadowRoot>;
-}
-
-impl<F> HtmlShadowRootProvider for F
-where
- F: Fn(NativeNodeId) -> Option<HtmlSerializedShadowRoot>,
-{
- fn serialized_shadow_root_for_host(
- &self,
- host: NativeNodeId,
- ) -> Option<HtmlSerializedShadowRoot> {
- self(host)
- }
-}
-
-#[derive(Clone, Copy)]
-pub(super) struct HtmlSerializationOptions<'a> {
- target: HtmlSerializationTarget,
- scripting_enabled: bool,
- shadow_root_provider: Option<&'a dyn HtmlShadowRootProvider>,
-}
-
-impl<'a> HtmlSerializationOptions<'a> {
- pub(super) const fn new(target: HtmlSerializationTarget, scripting_enabled: bool) -> Self {
- Self {
- target,
- scripting_enabled,
- shadow_root_provider: None,
- }
- }
-
- pub(super) const fn with_shadow_root_provider(
- mut self,
- shadow_root_provider: &'a dyn HtmlShadowRootProvider,
- ) -> Self {
- self.shadow_root_provider = Some(shadow_root_provider);
- self
- }
-}
-
-impl HtmlSerializationSink for String {
- fn push_str(&mut self, value: &str) {
- String::push_str(self, value);
- }
-
- fn push(&mut self, value: char) {
- String::push(self, value);
- }
-
- fn limit_exceeded(&self) -> bool {
- false
- }
-}
-
-pub(super) fn escape_html_text<S>(value: &str, out: &mut S)
-where
- S: HtmlSerializationSink + ?Sized,
-{
- for ch in value.chars() {
- if out.limit_exceeded() {
- return;
- }
- match ch {
- '&' => out.push_str("&amp;"),
- '<' => out.push_str("&lt;"),
- '>' => out.push_str("&gt;"),
- '\u{00A0}' => out.push_str("&nbsp;"),
- _ => out.push(ch),
- }
- }
-}
-
-pub(super) fn escape_html_attribute<S>(value: &str, out: &mut S)
-where
- S: HtmlSerializationSink + ?Sized,
-{
- for ch in value.chars() {
- if out.limit_exceeded() {
- return;
- }
- match ch {
- '&' => out.push_str("&amp;"),
- '"' => out.push_str("&quot;"),
- '<' => out.push_str("&lt;"),
- '>' => out.push_str("&gt;"),
- _ => out.push(ch),
- }
- }
-}
-
-pub(super) fn serialize_cdata_section<S>(
- value: &str,
- out: &mut S,
- raw_text_parent: bool,
- html_document: bool,
-) where
- S: HtmlSerializationSink + ?Sized,
-{
- if html_document {
- if raw_text_parent {
- out.push_str(value);
- } else {
- escape_html_text(value, out);
- }
- } else {
- out.push_str("<![CDATA[");
- out.push_str(value);
- out.push_str("]]>");
- }
-}
-
-pub(super) fn is_void_html_element(namespace: &str, local_name: &str) -> bool {
- namespace == "http://www.w3.org/1999/xhtml"
- && matches!(
- local_name,
- "area"
- | "base"
- | "basefont"
- | "bgsound"
- | "br"
- | "col"
- | "embed"
- | "frame"
- | "hr"
- | "img"
- | "input"
- | "keygen"
- | "link"
- | "meta"
- | "param"
- | "source"
- | "track"
- | "wbr"
- )
-}
-
-enum HtmlSerializationFrame<'a> {
- Node(NativeNodeId),
- Children(NativeNodeId),
- ShadowRootTemplate(HtmlSerializedShadowRoot),
- CloseElement(&'a str),
- CloseShadowRootTemplate,
-}
-
-pub(super) fn serialize_html_into_sink<S>(
- dom: &NativeDom,
- node_id: NativeNodeId,
- options: HtmlSerializationOptions<'_>,
- out: &mut S,
-) -> bool
-where
- S: HtmlSerializationSink,
-{
- let Some(node) = dom.node(node_id) else {
- return false;
- };
- if options.target == HtmlSerializationTarget::ChildrenOnly
- && node
- .as_element()
- .is_some_and(|element| is_void_html_element(element.namespace(), element.local_name()))
- {
- return true;
- }
-
- let mut stack = match options.target {
- HtmlSerializationTarget::IncludeNode => vec![HtmlSerializationFrame::Node(node_id)],
- HtmlSerializationTarget::ChildrenOnly => {
- let mut stack = vec![HtmlSerializationFrame::Children(node_id)];
- push_shadow_root_frame(node_id, options.shadow_root_provider, &mut stack);
- stack
- }
- };
-
- while !out.limit_exceeded() {
- let Some(frame) = stack.pop() else {
- break;
- };
- match frame {
- HtmlSerializationFrame::Node(node_id) => {
- serialize_html_node_frame(dom, node_id, options, out, &mut stack);
- }
- HtmlSerializationFrame::Children(node_id) => {
- push_child_html_serialization_frames(dom, node_id, &mut stack);
- }
- HtmlSerializationFrame::ShadowRootTemplate(shadow_root) => {
- out.push_str(&shadow_root.template_start);
- stack.push(HtmlSerializationFrame::CloseShadowRootTemplate);
- stack.push(HtmlSerializationFrame::Children(shadow_root.root));
- }
- HtmlSerializationFrame::CloseElement(local_name) => {
- out.push_str("</");
- out.push_str(local_name);
- out.push('>');
- }
- HtmlSerializationFrame::CloseShadowRootTemplate => {
- out.push_str("</template>");
- }
- }
- }
- true
-}
-
-fn serialize_html_node_frame<'a, S>(
- dom: &'a NativeDom,
- node_id: NativeNodeId,
- options: HtmlSerializationOptions<'_>,
- out: &mut S,
- stack: &mut Vec<HtmlSerializationFrame<'a>>,
-) where
- S: HtmlSerializationSink,
-{
- let Some(node) = dom.node(node_id) else {
- return;
- };
- match node.data() {
- NodeData::Document(_) | NodeData::DocumentFragment(_) => {
- stack.push(HtmlSerializationFrame::Children(node_id));
- }
- NodeData::DocumentType(document_type) => {
- out.push_str("<!DOCTYPE ");
- out.push_str(document_type.name());
- if !document_type.public_id().is_empty() || !document_type.system_id().is_empty() {
- out.push_str(" PUBLIC \"");
- out.push_str(document_type.public_id());
- out.push_str("\" \"");
- out.push_str(document_type.system_id());
- out.push('"');
- }
- out.push('>');
- }
- NodeData::Element(element) => {
- out.push('<');
- out.push_str(element.local_name());
- if let Some(is_name) = element.custom_element_is_name()
- && !element.has_attribute("is")
- {
- out.push_str(" is=\"");
- escape_html_attribute(is_name, out);
- out.push('"');
- }
- for attribute in element.attributes() {
- if out.limit_exceeded() {
- return;
- }
- out.push(' ');
- attribute.push_html_serialized_name(|part| out.push_str(part));
- out.push_str("=\"");
- escape_html_attribute(attribute.value(), out);
- out.push('"');
- }
- out.push('>');
-
- if !is_void_html_element(element.namespace(), element.local_name()) {
- stack.push(HtmlSerializationFrame::CloseElement(element.local_name()));
- stack.push(HtmlSerializationFrame::Children(node_id));
- push_shadow_root_frame(node_id, options.shadow_root_provider, stack);
- }
- }
- NodeData::Text(text) => {
- if text_data_serializes_literally(dom, node_id, options.scripting_enabled) {
- out.push_str(text.data());
- } else {
- escape_html_text(text.data(), out);
- }
- }
- NodeData::CDataSection(cdata) => {
- serialize_cdata_section(
- cdata.data(),
- out,
- text_data_serializes_literally(dom, node_id, options.scripting_enabled),
- dom.node_document_is_html_document(node_id).unwrap_or(false),
- );
- }
- NodeData::Comment(comment) => {
- out.push_str("<!--");
- out.push_str(comment.data());
- out.push_str("-->");
- }
- NodeData::ProcessingInstruction(processing_instruction) => {
- out.push_str("<?");
- out.push_str(processing_instruction.target());
- if !processing_instruction.data().is_empty() {
- out.push(' ');
- out.push_str(processing_instruction.data());
- }
- out.push_str("?>");
- }
- }
-}
-
-fn push_child_html_serialization_frames<'a>(
- dom: &NativeDom,
- node_id: NativeNodeId,
- stack: &mut Vec<HtmlSerializationFrame<'a>>,
-) {
- let children_root = dom
- .node(node_id)
- .and_then(Node::as_element)
- .and_then(|element| element.template_contents())
- .unwrap_or(node_id);
- stack.extend(
- dom.child_ids_reversed(children_root)
- .map(HtmlSerializationFrame::Node),
- );
-}
-
-fn push_shadow_root_frame(
- host: NativeNodeId,
- shadow_root_provider: Option<&dyn HtmlShadowRootProvider>,
- stack: &mut Vec<HtmlSerializationFrame<'_>>,
-) {
- let Some(shadow_root) =
- shadow_root_provider.and_then(|provider| provider.serialized_shadow_root_for_host(host))
- else {
- return;
- };
- stack.push(HtmlSerializationFrame::ShadowRootTemplate(shadow_root));
-}
-
-fn text_data_serializes_literally(
- dom: &NativeDom,
- node_id: NativeNodeId,
- scripting_enabled: bool,
-) -> bool {
- if !dom.node_document_is_html_document(node_id).unwrap_or(false) {
- return false;
- }
- let Some(parent) = dom
- .parent_node(node_id)
- .and_then(|parent| dom.node(parent))
- .and_then(Node::as_element)
- else {
- return false;
- };
- if parent.namespace() != "http://www.w3.org/1999/xhtml" {
- return false;
- }
- matches!(
- parent.local_name(),
- "style" | "script" | "xmp" | "iframe" | "noembed" | "noframes" | "plaintext"
- ) || scripting_enabled && parent.local_name() == "noscript"
-}
-
-struct BoundedHtmlSerialization {
- output: String,
- max_bytes: usize,
- exceeded: bool,
-}
-
-impl BoundedHtmlSerialization {
- fn new(max_bytes: usize) -> Self {
- Self {
- output: String::new(),
- max_bytes,
- exceeded: false,
- }
- }
-
- fn finish(self) -> Result<String, HtmlSerializationLimitExceeded> {
- if self.exceeded {
- Err(HtmlSerializationLimitExceeded {
- max_bytes: self.max_bytes,
- })
- } else {
- Ok(self.output)
- }
- }
-}
-
-impl HtmlSerializationSink for BoundedHtmlSerialization {
- fn push_str(&mut self, value: &str) {
- if self.exceeded {
- return;
- }
- if self
- .output
- .len()
- .checked_add(value.len())
- .is_none_or(|length| length > self.max_bytes)
- {
- self.exceeded = true;
- return;
- }
- self.output.push_str(value);
- }
-
- fn push(&mut self, value: char) {
- if self.exceeded {
- return;
- }
- if self
- .output
- .len()
- .checked_add(value.len_utf8())
- .is_none_or(|length| length > self.max_bytes)
- {
- self.exceeded = true;
- return;
- }
- self.output.push(value);
- }
-
- fn limit_exceeded(&self) -> bool {
- self.exceeded
- }
-}
-
-impl NativeDom {
- pub fn serialize_document(&self) -> String {
- self.serialize_document_with_scripting_enabled(true)
- }
-
- pub fn serialize_document_with_scripting_enabled(&self, scripting_enabled: bool) -> String {
- let mut html = String::new();
- serialize_html_into_sink(
- self,
- self.document_node_id,
- HtmlSerializationOptions::new(HtmlSerializationTarget::ChildrenOnly, scripting_enabled),
- &mut html,
- );
- html
- }
-
- pub fn is_html_element_named(&self, node_id: NativeNodeId, local_name: &str) -> bool {
- self.node(node_id)
- .is_some_and(|node| node.is_html_element_named(local_name))
- }
-
- pub fn option_value(&self, node_id: NativeNodeId) -> Option<String> {
- let element = self.node(node_id).and_then(Node::as_element)?;
- if !element.is_html_option() {
- return None;
- }
- Some(element.option_value(self, node_id))
- }
-
- pub fn outer_html(&self, node_id: NativeNodeId) -> Option<String> {
- self.outer_html_with_scripting_enabled(node_id, true)
- }
-
- pub fn outer_html_with_scripting_enabled(
- &self,
- node_id: NativeNodeId,
- scripting_enabled: bool,
- ) -> Option<String> {
- let mut html = String::new();
- serialize_html_into_sink(
- self,
- node_id,
- HtmlSerializationOptions::new(HtmlSerializationTarget::IncludeNode, scripting_enabled),
- &mut html,
- )
- .then_some(html)
- }
-
- /// Serializes one subtree without ever growing the output beyond `max_bytes`.
- ///
- /// This is intended for bounded derived consumers such as a fresh inline
- /// SVG image parse. Web-exposed `outerHTML` continues to use the unbounded
- /// serializer because truncation would violate its string contract.
- pub fn outer_html_with_limit(
- &self,
- node_id: NativeNodeId,
- max_bytes: usize,
- ) -> Result<Option<String>, HtmlSerializationLimitExceeded> {
- let mut out = BoundedHtmlSerialization::new(max_bytes);
- if !serialize_html_into_sink(
- self,
- node_id,
- HtmlSerializationOptions::new(HtmlSerializationTarget::IncludeNode, true),
- &mut out,
- ) {
- return Ok(None);
- }
- out.finish().map(Some)
- }
-
- pub fn inner_html(&self, node_id: NativeNodeId) -> Option<String> {
- self.inner_html_with_scripting_enabled(node_id, true)
- }
-
- pub fn inner_html_with_scripting_enabled(
- &self,
- node_id: NativeNodeId,
- scripting_enabled: bool,
- ) -> Option<String> {
- let mut html = String::new();
- serialize_html_into_sink(
- self,
- node_id,
- HtmlSerializationOptions::new(HtmlSerializationTarget::ChildrenOnly, scripting_enabled),
- &mut html,
- )
- .then_some(html)
- }
-
- pub fn script_handles(&self) -> Vec<NativeNodeId> {
- self.nodes
- .iter()
- .filter_map(|node| node.is_script_element().then_some(node.id()))
- .collect()
- }
-
- pub fn script_node_ids(&self) -> Vec<NativeNodeId> {
- self.script_handles()
- }
-
- pub fn document_order_script_handles(&self) -> Vec<NativeNodeId> {
- let mut script_handles = Vec::new();
- let mut stack = vec![self.document_node_id];
- while let Some(node_id) = stack.pop() {
- let Some(node) = self.node(node_id) else {
- continue;
- };
- if node.is_script_element() {
- script_handles.push(node_id);
- }
- stack.extend(self.child_ids_reversed(node_id));
- }
- script_handles
- }
-
- pub fn document_order_script_node_ids(&self) -> Vec<NativeNodeId> {
- self.document_order_script_handles()
- }
-
- pub fn script_src(&self, node_id: NativeNodeId) -> Option<&str> {
- self.node(node_id)?.as_element()?.script_source_attribute()
- }
-
- pub fn script_text(&self, node_id: NativeNodeId) -> Option<String> {
- let script_node = self.node(node_id)?;
- let element = script_node.as_element()?;
- if !element.is_script_element() {
- return None;
- }
-
- let mut script_text = String::new();
- for child_id in script_node.child_ids(self) {
- let Some(child) = self.node(child_id) else {
- continue;
- };
-
- if let Some(text) = child.as_text() {
- script_text.push_str(text.data());
- }
- }
-
- (!script_text.is_empty()).then_some(script_text)
- }
-
- pub fn push_parse_error(&mut self, error: String) {
- self.parse_errors.push(error);
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use crate::native::DomHost;
-
- fn test_url() -> url::Url {
- url::Url::parse("https://serialization.test/").expect("test URL")
- }
-
- #[test]
- fn html_serializers_share_the_complete_void_element_set() {
- let mut dom = NativeDom::new_html(test_url());
- let container = dom.create_element("div");
- let mut expected = String::new();
- let mut void_elements = Vec::new();
- for local_name in [
- "area", "base", "basefont", "bgsound", "br", "col", "embed", "frame", "hr", "img",
- "input", "keygen", "link", "meta", "param", "source", "track", "wbr",
- ] {
- let element = dom.create_element(local_name);
- let ignored_child = dom.create_element("span");
- assert!(dom.append_child(element, ignored_child));
- assert_eq!(dom.outer_html(element), Some(format!("<{local_name}>")));
- assert_eq!(dom.inner_html(element).as_deref(), Some(""));
- assert!(dom.append_child(container, element));
- expected.push_str(&format!("<{local_name}>"));
- void_elements.push(element);
- }
- assert_eq!(
- dom.inner_html(container).as_deref(),
- Some(expected.as_str())
- );
-
- let foreign_param = dom
- .create_element_ns(Some("http://www.w3.org/2000/svg"), "param")
- .expect("SVG element");
- assert_eq!(
- dom.outer_html(foreign_param).as_deref(),
- Some("<param></param>")
- );
-
- let mut host = DomHost::from_dom(dom);
- let param = host.create_element("param");
- assert!(host.append_child(container, param));
- assert_eq!(
- host.get_html(container, true, false, &[]).as_deref(),
- Some(format!("{expected}<param>").as_str())
- );
- for element in void_elements {
- assert_eq!(
- host.get_html(element, true, false, &[]).as_deref(),
- Some("")
- );
- }
- }
-
- #[test]
- fn html_text_serialization_uses_the_actual_parent_and_scripting_mode() {
- let mut dom = NativeDom::new_html(test_url());
- let container = dom.create_element("main");
- let mut cases = Vec::new();
- for (local_name, literal_with_scripting, literal_without_scripting) in [
- ("style", true, true),
- ("script", true, true),
- ("xmp", true, true),
- ("iframe", true, true),
- ("noembed", true, true),
- ("noframes", true, true),
- ("plaintext", true, true),
- ("noscript", true, false),
- ("textarea", false, false),
- ("title", false, false),
- ("div", false, false),
- ] {
- let element = dom.create_element(local_name);
- let text = dom.create_text_node("<&");
- assert!(dom.append_child(element, text));
- assert!(dom.append_child(container, element));
- cases.push((
- element,
- local_name,
- literal_with_scripting,
- literal_without_scripting,
- ));
- }
-
- for &(element, _, literal_with_scripting, literal_without_scripting) in &cases {
- assert_eq!(
- dom.inner_html_with_scripting_enabled(element, true)
- .as_deref(),
- Some(if literal_with_scripting {
- "<&"
- } else {
- "&lt;&amp;"
- })
- );
- assert_eq!(
- dom.inner_html_with_scripting_enabled(element, false)
- .as_deref(),
- Some(if literal_without_scripting {
- "<&"
- } else {
- "&lt;&amp;"
- })
- );
- }
-
- let host = DomHost::from_dom(dom);
- for &(element, local_name, literal_with_scripting, literal_without_scripting) in &cases {
- for (scripting_enabled, literal) in [
- (true, literal_with_scripting),
- (false, literal_without_scripting),
- ] {
- let contents = if literal { "<&" } else { "&lt;&amp;" };
- assert_eq!(
- host.get_html_with_shadow_root_registry_attribute_policy(
- element,
- scripting_enabled,
- false,
- &[],
- None,
- )
- .as_deref(),
- Some(contents),
- "children-only serialization diverged for {local_name}"
- );
- assert_eq!(
- host.outer_html_with_shadow_roots(
- element,
- scripting_enabled,
- crate::native::ShadowRootInclusion::None,
- None,
- ),
- Some(format!("<{local_name}>{contents}</{local_name}>")),
- "node serialization diverged for {local_name}"
- );
- }
- }
- }
-
- #[test]
- fn bounded_outer_html_stops_before_exceeding_the_output_limit() {
- let mut dom = NativeDom::new_html(test_url());
- let element = dom.create_element("div");
- let expected = "<div></div>";
-
- assert_eq!(
- dom.outer_html_with_limit(element, expected.len()),
- Ok(Some(expected.to_owned()))
- );
- assert_eq!(
- dom.outer_html_with_limit(element, expected.len() - 1),
- Err(HtmlSerializationLimitExceeded {
- max_bytes: expected.len() - 1,
- })
- );
- }
-
- #[test]
- fn html_serializers_apply_attribute_serialized_name_rules() {
- let mut dom = NativeDom::new_html(test_url());
- let container = dom.create_element("section");
- let element = dom
- .create_element_ns(Some("urn:element"), "div")
- .expect("namespaced element");
- assert!(dom.set_attribute_ns(
- element,
- Some("http://www.w3.org/XML/1998/namespace"),
- Some("alternate"),
- "lang",
- "en-us",
- ));
- assert!(dom.set_attribute_ns(
- element,
- Some("http://www.w3.org/2000/xmlns/"),
- None,
- "binding",
- "urn:binding",
- ));
- assert!(dom.set_attribute_ns(
- element,
- Some("http://www.w3.org/2000/xmlns/"),
- None,
- "xmlns",
- "urn:default",
- ));
- assert!(dom.set_attribute_ns(
- element,
- Some("http://www.w3.org/1999/xlink"),
- Some("alternate"),
- "href",
- "target",
- ));
- assert!(dom.set_attribute_ns(element, Some("urn:custom"), Some("p"), "attr", "value",));
- assert!(dom.append_child(container, element));
-
- let expected = concat!(
- "<div xml:lang=\"en-us\" xmlns:binding=\"urn:binding\" ",
- "xmlns=\"urn:default\" xlink:href=\"target\" p:attr=\"value\"></div>"
- );
- assert_eq!(dom.inner_html(container).as_deref(), Some(expected));
-
- let host = DomHost::from_dom(dom);
- assert_eq!(
- host.get_html(container, true, false, &[]).as_deref(),
- Some(expected)
- );
- }
-
- #[test]
- fn html_serializers_escape_adopted_cdata_as_text() {
- const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";
- const XMLNS_NAMESPACE: &str = "http://www.w3.org/2000/xmlns/";
-
- let mut xml_dom = NativeDom::new_xml(test_url());
- let xml_svg = xml_dom
- .create_element_ns(Some(SVG_NAMESPACE), "svg")
- .expect("XML SVG element");
- assert!(xml_dom.set_attribute_ns(
- xml_svg,
- Some(XMLNS_NAMESPACE),
- None,
- "xmlns",
- SVG_NAMESPACE,
- ));
- let xml_cdata = xml_dom.create_cdata_section("<img>&");
- assert!(xml_dom.append_child(xml_svg, xml_cdata));
- assert_eq!(
- xml_dom.outer_html(xml_svg).as_deref(),
- Some(r#"<svg xmlns="http://www.w3.org/2000/svg"><![CDATA[<img>&]]></svg>"#)
- );
-
- let mut html_dom = NativeDom::new_html(test_url());
- let html_svg = html_dom
- .create_element_ns(Some(SVG_NAMESPACE), "svg")
- .expect("HTML-document SVG element");
- assert!(html_dom.set_attribute_ns(
- html_svg,
- Some(XMLNS_NAMESPACE),
- None,
- "xmlns",
- SVG_NAMESPACE,
- ));
- let adopted_cdata = html_dom.create_cdata_section("<img>&");
- assert!(html_dom.append_child(html_svg, adopted_cdata));
- assert_eq!(
- html_dom.outer_html(html_svg).as_deref(),
- Some(r#"<svg xmlns="http://www.w3.org/2000/svg">&lt;img&gt;&amp;</svg>"#)
- );
-
- let host = DomHost::from_dom(html_dom);
- assert_eq!(
- host.get_html(html_svg, true, false, &[]).as_deref(),
- Some("&lt;img&gt;&amp;")
- );
- }
-}