Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(dom): resolve scripting state per serialized node

ldm01f6dcd575781
diff --git a/moli-dom/src/native/host/html_serialization/mod.rs b/moli-dom/src/native/host/html_serialization/mod.rs
index 454bce6ad..c4dc59506 100644
--- a/moli-dom/src/native/host/html_serialization/mod.rs
+++ b/moli-dom/src/native/host/html_serialization/mod.rs
@@ -36,13 +36,13 @@ impl DomHost {
pub fn get_html(
&self,
handle: DomHandle,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool,
serializable_shadow_roots: bool,
explicit_shadow_roots: &[DomHandle],
) -> Option<String> {
self.get_html_with_shadow_root_registry_attribute_policy(
handle,
- scripting_enabled,
+ scripting_enabled_for_node,
serializable_shadow_roots,
explicit_shadow_roots,
None,
@@ -52,7 +52,7 @@ impl DomHost {
pub fn get_html_with_shadow_root_registry_attribute_policy(
&self,
handle: DomHandle,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool,
serializable_shadow_roots: bool,
explicit_shadow_roots: &[DomHandle],
registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
@@ -60,7 +60,7 @@ impl DomHost {
self.serialize_html_with_shadow_roots(
handle,
HtmlSerializationTarget::ChildrenOnly,
- scripting_enabled,
+ scripting_enabled_for_node,
ShadowRootInclusion::SerializableOrExplicit {
serializable: serializable_shadow_roots,
explicit: explicit_shadow_roots,
@@ -72,14 +72,14 @@ impl DomHost {
pub fn outer_html_with_shadow_roots(
&self,
handle: DomHandle,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool,
shadow_root_inclusion: ShadowRootInclusion<'_>,
registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
) -> Option<String> {
self.serialize_html_with_shadow_roots(
handle,
HtmlSerializationTarget::IncludeNode,
- scripting_enabled,
+ scripting_enabled_for_node,
shadow_root_inclusion,
registry_attribute_policy,
)
@@ -89,7 +89,7 @@ impl DomHost {
&self,
handle: DomHandle,
target: HtmlSerializationTarget,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(DomHandle) -> bool,
shadow_root_inclusion: ShadowRootInclusion<'_>,
registry_attribute_policy: Option<&ShadowRootRegistryAttributePolicy<'_>>,
) -> Option<String> {
@@ -111,7 +111,7 @@ impl DomHost {
&self.dom,
handle,
target,
- scripting_enabled,
+ scripting_enabled_for_node,
&shadow_root_provider,
)
}
diff --git a/moli-dom/src/native/host/html_serialization/tests.rs b/moli-dom/src/native/host/html_serialization/tests.rs
index 5aba50820..d0395fa18 100644
--- a/moli-dom/src/native/host/html_serialization/tests.rs
+++ b/moli-dom/src/native/host/html_serialization/tests.rs
@@ -4,6 +4,10 @@ fn test_url() -> url::Url {
url::Url::parse("https://inspector-shadow-serialization.test/").expect("test URL")
}
+fn scripting_enabled(_: DomHandle) -> bool {
+ true
+}
+
#[test]
fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
@@ -29,8 +33,13 @@ fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
assert!(host.append_child(element, light_text));
assert_eq!(
- host.outer_html_with_shadow_roots(element, true, ShadowRootInclusion::None, None)
- .as_deref(),
+ host.outer_html_with_shadow_roots(
+ element,
+ &scripting_enabled,
+ ShadowRootInclusion::None,
+ None,
+ )
+ .as_deref(),
Some("<section id=\"host\">light &amp; more</section>")
);
@@ -38,7 +47,7 @@ fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
assert_eq!(
host.outer_html_with_shadow_roots(
element,
- true,
+ &scripting_enabled,
ShadowRootInclusion::AllAuthorForInspector,
Some(&include_registry),
)
@@ -54,7 +63,7 @@ fn inspector_outer_html_uses_chromium_shadow_template_attributes() {
assert_eq!(
host.get_html_with_shadow_root_registry_attribute_policy(
element,
- true,
+ &scripting_enabled,
false,
&[shadow_root],
Some(&include_registry),
@@ -94,14 +103,19 @@ fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
assert!(host.append_child(outer_host, outer_light));
assert_eq!(
- host.outer_html_with_shadow_roots(outer_host, true, ShadowRootInclusion::None, None)
- .as_deref(),
+ host.outer_html_with_shadow_roots(
+ outer_host,
+ &scripting_enabled,
+ ShadowRootInclusion::None,
+ None,
+ )
+ .as_deref(),
Some("<x-outer>outer-light</x-outer>")
);
assert_eq!(
host.outer_html_with_shadow_roots(
outer_host,
- true,
+ &scripting_enabled,
ShadowRootInclusion::SerializableOrExplicit {
serializable: false,
explicit: &[outer_root],
@@ -122,7 +136,7 @@ fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
assert_eq!(
host.outer_html_with_shadow_roots(
outer_host,
- true,
+ &scripting_enabled,
ShadowRootInclusion::AllAuthorForInspector,
None,
)
@@ -132,7 +146,7 @@ fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
assert_eq!(
host.outer_html_with_shadow_roots(
host.document_node_id(),
- true,
+ &scripting_enabled,
ShadowRootInclusion::AllAuthorForInspector,
None,
)
@@ -142,7 +156,7 @@ fn inspector_outer_html_includes_nested_open_and_closed_author_roots() {
assert_eq!(
host.outer_html_with_shadow_roots(
outer_root,
- true,
+ &scripting_enabled,
ShadowRootInclusion::AllAuthorForInspector,
None,
)
@@ -175,12 +189,22 @@ fn inspector_outer_html_walks_deep_shadow_trees_iteratively() {
assert!(host.append_child(shadow_host, leaf));
assert_eq!(
- host.outer_html_with_shadow_roots(root, true, ShadowRootInclusion::None, None)
- .as_deref(),
+ host.outer_html_with_shadow_roots(
+ root,
+ &scripting_enabled,
+ ShadowRootInclusion::None,
+ None,
+ )
+ .as_deref(),
Some("<div></div>")
);
let html = host
- .outer_html_with_shadow_roots(root, true, ShadowRootInclusion::AllAuthorForInspector, None)
+ .outer_html_with_shadow_roots(
+ root,
+ &scripting_enabled,
+ 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>"));
@@ -247,7 +271,12 @@ fn shadow_excluding_outer_html_matches_native_serializer() {
fragment_child,
] {
assert_eq!(
- host.outer_html_with_shadow_roots(handle, true, ShadowRootInclusion::None, None),
+ host.outer_html_with_shadow_roots(
+ handle,
+ &scripting_enabled,
+ ShadowRootInclusion::None,
+ None,
+ ),
host.dom().outer_html(handle),
"shadow-excluding host serializer diverged for {handle:?}"
);
diff --git a/moli-dom/src/native/host/mutation/tree.rs b/moli-dom/src/native/host/mutation/tree.rs
index 4d2f9217d..bd360d3fa 100644
--- a/moli-dom/src/native/host/mutation/tree.rs
+++ b/moli-dom/src/native/host/mutation/tree.rs
@@ -548,15 +548,6 @@ impl DomHost {
self.dom.inner_html(handle)
}
- pub fn inner_html_with_scripting_enabled(
- &self,
- handle: DomHandle,
- scripting_enabled: bool,
- ) -> Option<String> {
- self.dom
- .inner_html_with_scripting_enabled(handle, scripting_enabled)
- }
-
pub fn node_metadata(&self, handle: DomHandle) -> Option<LiveDomNodeMetadata> {
self.dom.node_metadata(handle)
}
diff --git a/moli-dom/src/native/html_serialization/engine.rs b/moli-dom/src/native/html_serialization/engine.rs
index 9cc377a2c..52bada56d 100644
--- a/moli-dom/src/native/html_serialization/engine.rs
+++ b/moli-dom/src/native/html_serialization/engine.rs
@@ -50,15 +50,20 @@ where
#[derive(Clone, Copy)]
struct HtmlSerializationOptions<'a> {
target: HtmlSerializationTarget,
- scripting_enabled: bool,
+ // This is node-scoped: one traversal can enter inert template contents
+ // whose owner Document has different scripting state from the outer tree.
+ scripting_enabled_for_node: &'a dyn Fn(NativeNodeId) -> bool,
shadow_root_provider: Option<&'a dyn HtmlShadowRootProvider>,
}
impl<'a> HtmlSerializationOptions<'a> {
- const fn new(target: HtmlSerializationTarget, scripting_enabled: bool) -> Self {
+ const fn new(
+ target: HtmlSerializationTarget,
+ scripting_enabled_for_node: &'a dyn Fn(NativeNodeId) -> bool,
+ ) -> Self {
Self {
target,
- scripting_enabled,
+ scripting_enabled_for_node,
shadow_root_provider: None,
}
}
@@ -287,7 +292,7 @@ fn serialize_html_node_frame<'a, S>(
}
}
NodeData::Text(text) => {
- if text_data_serializes_literally(dom, node_id, options.scripting_enabled) {
+ if text_data_serializes_literally(dom, node_id, options.scripting_enabled_for_node) {
out.push_str(text.data());
} else {
escape_html_text(text.data(), out);
@@ -297,7 +302,7 @@ fn serialize_html_node_frame<'a, S>(
serialize_cdata_section(
cdata.data(),
out,
- text_data_serializes_literally(dom, node_id, options.scripting_enabled),
+ text_data_serializes_literally(dom, node_id, options.scripting_enabled_for_node),
dom.node_document_is_html_document(node_id).unwrap_or(false),
);
}
@@ -350,7 +355,7 @@ fn push_shadow_root_frame(
fn text_data_serializes_literally(
dom: &NativeDom,
node_id: NativeNodeId,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(NativeNodeId) -> bool,
) -> bool {
if !dom.node_document_is_html_document(node_id).unwrap_or(false) {
return false;
@@ -368,7 +373,7 @@ fn text_data_serializes_literally(
matches!(
parent.local_name(),
"style" | "script" | "xmp" | "iframe" | "noembed" | "noframes" | "plaintext"
- ) || scripting_enabled && parent.local_name() == "noscript"
+ ) || parent.local_name() == "noscript" && scripting_enabled_for_node(node_id)
}
struct BoundedHtmlSerialization {
@@ -441,11 +446,12 @@ pub(super) fn serialize_html(
target: HtmlSerializationTarget,
scripting_enabled: bool,
) -> Option<String> {
+ let scripting_enabled_for_node = |_: NativeNodeId| scripting_enabled;
let mut html = String::new();
serialize_html_into_sink(
dom,
node_id,
- HtmlSerializationOptions::new(target, scripting_enabled),
+ HtmlSerializationOptions::new(target, &scripting_enabled_for_node),
&mut html,
)
.then_some(html)
@@ -455,13 +461,13 @@ pub(in crate::native) fn serialize_html_with_shadow_root_provider<F>(
dom: &NativeDom,
node_id: NativeNodeId,
target: HtmlSerializationTarget,
- scripting_enabled: bool,
+ scripting_enabled_for_node: &dyn Fn(NativeNodeId) -> bool,
shadow_root_provider: &F,
) -> Option<String>
where
F: Fn(NativeNodeId) -> Option<HtmlSerializedShadowRoot>,
{
- let options = HtmlSerializationOptions::new(target, scripting_enabled)
+ let options = HtmlSerializationOptions::new(target, scripting_enabled_for_node)
.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)
@@ -476,11 +482,15 @@ pub(super) fn serialize_html_with_limit(
node_id: NativeNodeId,
max_bytes: usize,
) -> Result<Option<String>, HtmlSerializationLimitExceeded> {
+ let scripting_enabled_for_node = |_: NativeNodeId| true;
let mut out = BoundedHtmlSerialization::new(max_bytes);
if !serialize_html_into_sink(
dom,
node_id,
- HtmlSerializationOptions::new(HtmlSerializationTarget::IncludeNode, true),
+ HtmlSerializationOptions::new(
+ HtmlSerializationTarget::IncludeNode,
+ &scripting_enabled_for_node,
+ ),
&mut out,
) {
return Ok(None);
diff --git a/moli-dom/src/native/html_serialization/tests.rs b/moli-dom/src/native/html_serialization/tests.rs
index e822072d9..4750721ae 100644
--- a/moli-dom/src/native/html_serialization/tests.rs
+++ b/moli-dom/src/native/html_serialization/tests.rs
@@ -5,6 +5,10 @@ fn test_url() -> url::Url {
url::Url::parse("https://serialization.test/").expect("test URL")
}
+fn scripting_enabled(_: NativeNodeId) -> bool {
+ true
+}
+
#[test]
fn html_serializers_share_the_complete_void_element_set() {
let mut dom = NativeDom::new_html(test_url());
@@ -41,12 +45,14 @@ fn html_serializers_share_the_complete_void_element_set() {
let param = host.create_element("param");
assert!(host.append_child(container, param));
assert_eq!(
- host.get_html(container, true, false, &[]).as_deref(),
+ host.get_html(container, &scripting_enabled, false, &[])
+ .as_deref(),
Some(format!("{expected}<param>").as_str())
);
for element in void_elements {
assert_eq!(
- host.get_html(element, true, false, &[]).as_deref(),
+ host.get_html(element, &scripting_enabled, false, &[])
+ .as_deref(),
Some("")
);
}
@@ -109,11 +115,12 @@ fn html_text_serialization_uses_the_actual_parent_and_scripting_mode() {
(true, literal_with_scripting),
(false, literal_without_scripting),
] {
+ let scripting_enabled_for_node = |_: NativeNodeId| scripting_enabled;
let contents = if literal { "<&" } else { "&lt;&amp;" };
assert_eq!(
host.get_html_with_shadow_root_registry_attribute_policy(
element,
- scripting_enabled,
+ &scripting_enabled_for_node,
false,
&[],
None,
@@ -125,7 +132,7 @@ fn html_text_serialization_uses_the_actual_parent_and_scripting_mode() {
assert_eq!(
host.outer_html_with_shadow_roots(
element,
- scripting_enabled,
+ &scripting_enabled_for_node,
crate::native::ShadowRootInclusion::None,
None,
),
@@ -136,6 +143,46 @@ fn html_text_serialization_uses_the_actual_parent_and_scripting_mode() {
}
}
+#[test]
+fn html_serialization_queries_scripting_state_for_each_noscript_text_node() {
+ let mut host = DomHost::from_dom(NativeDom::new_html(test_url()));
+ let container = host.create_element("main");
+ let enabled_noscript = host.create_element("noscript");
+ let enabled_text = host.create_text_node("<enabled&>");
+ let disabled_noscript = host.create_element("noscript");
+ let disabled_text = host.create_text_node("<disabled&>");
+ assert!(host.append_child(enabled_noscript, enabled_text));
+ assert!(host.append_child(disabled_noscript, disabled_text));
+ assert!(host.append_child(container, enabled_noscript));
+ assert!(host.append_child(container, disabled_noscript));
+
+ let scripting_enabled_for_node = |node| node == enabled_text;
+ let expected = concat!(
+ "<noscript><enabled&></noscript>",
+ "<noscript>&lt;disabled&amp;&gt;</noscript>"
+ );
+ assert_eq!(
+ host.get_html_with_shadow_root_registry_attribute_policy(
+ container,
+ &scripting_enabled_for_node,
+ false,
+ &[],
+ None,
+ )
+ .as_deref(),
+ Some(expected)
+ );
+ assert_eq!(
+ host.outer_html_with_shadow_roots(
+ container,
+ &scripting_enabled_for_node,
+ crate::native::ShadowRootInclusion::None,
+ None,
+ ),
+ Some(format!("<main>{expected}</main>"))
+ );
+}
+
#[test]
fn bounded_outer_html_stops_before_exceeding_the_output_limit() {
let mut dom = NativeDom::new_html(test_url());
@@ -200,7 +247,8 @@ fn html_serializers_apply_attribute_serialized_name_rules() {
let host = DomHost::from_dom(dom);
assert_eq!(
- host.get_html(container, true, false, &[]).as_deref(),
+ host.get_html(container, &scripting_enabled, false, &[])
+ .as_deref(),
Some(expected)
);
}
@@ -244,7 +292,8 @@ fn html_serializers_escape_adopted_cdata_as_text() {
let host = DomHost::from_dom(html_dom);
assert_eq!(
- host.get_html(html_svg, true, false, &[]).as_deref(),
+ host.get_html(html_svg, &scripting_enabled, 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 21a0ab8a3..bd30e0fd9 100644
--- a/moli-dom/src/native/mod.rs
+++ b/moli-dom/src/native/mod.rs
@@ -3203,7 +3203,7 @@ mod tests {
);
let html = host
- .get_html(root, true, false, &[shadow_root])
+ .get_html(root, &|_| true, false, &[shadow_root])
.expect("shadow-including HTML");
assert!(html.contains("<template shadowrootmode=\"open\">"));
assert!(html.contains("</template></article><script></script>"));
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs
index 837837247..0f5e8be53 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_snapshots/document_lookup.rs
@@ -66,9 +66,10 @@ impl JsContextHost {
.then_some(binding.root)
})
.collect::<Vec<_>>();
+ let scripting_enabled_for_node = |node| self.node_document_scripting_enabled(node);
let markup = self.dom_host().get_html(
document_handle,
- self.child_browsing_context_scripting_enabled(handle),
+ &scripting_enabled_for_node,
false,
&explicit_shadow_roots,
)?;
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
index fa047b6c4..ee5c53e4f 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/request_scope.rs
@@ -736,12 +736,13 @@ impl JsContextHost {
.unwrap_or(false)
}
+ /// Returns the scripting state of the node's actual node Document.
+ ///
+ /// Associated-tree identity is deliberately not used here: template
+ /// contents map to their outer host for CDP registries, but remain owned by
+ /// a separate inert Document for HTML parsing and serialization semantics.
pub(crate) fn node_document_scripting_enabled(&self, handle: DomHandle) -> bool {
- let Some(document_handle) = self
- .dom_host()
- .document_identity_handle(handle)
- .and_then(|identity| self.dom_host().owner_document_handle(identity))
- else {
+ let Some(document_handle) = self.dom_host().owner_document_handle(handle) else {
return false;
};
self.document_scripting_enabled(document_handle)
diff --git a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
index 3173466a0..61828c22a 100644
--- a/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
+++ b/moli-renderer-v8/src/native_bridge/document/lifecycle.rs
@@ -338,7 +338,11 @@ pub(in crate::native_bridge) fn append_detached_html_document_body_html(
let Some(body) = detached_html_document_body_handle(runtime, document_handle) else {
return false;
};
- let mut next = runtime.dom_host().inner_html(body).unwrap_or_default();
+ let scripting_enabled_for_node = |node| runtime.node_document_scripting_enabled(node);
+ let mut next = runtime
+ .dom_host()
+ .get_html(body, &scripting_enabled_for_node, false, &[])
+ .unwrap_or_default();
next.push_str(html);
runtime.set_inner_html(scope, runtime_ptr, body, &next)
})
diff --git a/moli-renderer-v8/src/native_bridge/element/content.rs b/moli-renderer-v8/src/native_bridge/element/content.rs
index 0e90fb7c7..5c92b7762 100644
--- a/moli-renderer-v8/src/native_bridge/element/content.rs
+++ b/moli-renderer-v8/src/native_bridge/element/content.rs
@@ -833,9 +833,10 @@ fn node_inner_text(
}
fn node_inner_html(runtime: &JsContextHost, handle: DomHandle) -> Option<String> {
+ let scripting_enabled_for_node = |node| runtime.node_document_scripting_enabled(node);
runtime
.dom_host()
- .inner_html_with_scripting_enabled(handle, runtime.node_document_scripting_enabled(handle))
+ .get_html(handle, &scripting_enabled_for_node, false, &[])
}
fn is_element_or_shadow_root_receiver(runtime: &JsContextHost, handle: DomHandle) -> bool {
@@ -897,10 +898,15 @@ pub(in crate::native_bridge) fn node_outer_html_getter_function<'s>(
rv.set_undefined();
return;
}
+ let scripting_enabled_for_node = |node| runtime.node_document_scripting_enabled(node);
let value = runtime
.dom_host()
- .dom()
- .outer_html_with_scripting_enabled(handle, runtime.node_document_scripting_enabled(handle))
+ .outer_html_with_shadow_roots(
+ handle,
+ &scripting_enabled_for_node,
+ crate::dom::native::ShadowRootInclusion::None,
+ None,
+ )
.unwrap_or_default();
let Some(value) = v8_string(scope, &value) else {
rv.set_null();
@@ -1053,11 +1059,12 @@ pub(in crate::native_bridge) fn node_get_html_callback<'s>(
|_: DomHandle, shadow_root: DomHandle, _: &crate::dom::native::ShadowRootInit| {
runtime.should_serialize_shadow_root_registry_attribute(shadow_root)
};
+ let scripting_enabled_for_node = |node| runtime.node_document_scripting_enabled(node);
let Some(html) = runtime
.dom_host()
.get_html_with_shadow_root_registry_attribute_policy(
handle,
- runtime.node_document_scripting_enabled(handle),
+ &scripting_enabled_for_node,
serializable_shadow_roots,
&explicit_shadow_roots,
Some(&should_serialize_registry_attribute),
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index 817eb0556..7cd4dfd5f 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -1331,9 +1331,10 @@ impl ScriptVm {
|_: DomHandle, shadow_root: DomHandle, _: &crate::dom::native::ShadowRootInit| {
host.should_serialize_shadow_root_registry_attribute(shadow_root)
};
+ let scripting_enabled_for_node = |node| host.node_document_scripting_enabled(node);
host.dom_host().outer_html_with_shadow_roots(
handle,
- host.node_document_scripting_enabled(handle),
+ &scripting_enabled_for_node,
shadow_root_inclusion,
Some(&should_serialize_registry_attribute),
)
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
index 204060e1b..c876ef97e 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/detached.rs
@@ -49,6 +49,80 @@ fn detached_domparser_parses_noscript_with_scripting_disabled() {
);
}
+#[test]
+fn template_contents_serialize_noscript_with_the_inert_node_document() {
+ let mut vm = new_storage_test_vm("https://template-noscript-serialization.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const template = document.createElement("template");
+ const noscript = document.createElement("noscript");
+ noscript.textContent = "<em>fallback&</em>";
+ template.content.append(noscript);
+
+ return [
+ template.innerHTML,
+ template.outerHTML,
+ template.getHTML(),
+ noscript.innerHTML,
+ noscript.outerHTML,
+ noscript.getHTML(),
+ noscript.ownerDocument === template.content.ownerDocument,
+ noscript.ownerDocument === document
+ ].join("|");
+})()
+"#,
+ )
+ .expect("template noscript serialization probe should evaluate");
+
+ assert_eq!(
+ result,
+ concat!(
+ "<noscript>&lt;em&gt;fallback&amp;&lt;/em&gt;</noscript>|",
+ "<template><noscript>&lt;em&gt;fallback&amp;&lt;/em&gt;</noscript></template>|",
+ "<noscript>&lt;em&gt;fallback&amp;&lt;/em&gt;</noscript>|",
+ "&lt;em&gt;fallback&amp;&lt;/em&gt;|",
+ "<noscript>&lt;em&gt;fallback&amp;&lt;/em&gt;</noscript>|",
+ "&lt;em&gt;fallback&amp;&lt;/em&gt;|true|false"
+ )
+ );
+}
+
+#[test]
+fn detached_document_write_preserves_existing_noscript_text() {
+ let mut vm = new_storage_test_vm("https://detached-write-noscript-serialization.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const doc = document.implementation.createHTMLDocument("");
+ const noscript = doc.createElement("noscript");
+ noscript.textContent = "<em>fallback&</em>";
+ doc.body.append(noscript);
+ doc.write("<span>tail</span>");
+ return [
+ doc.body.innerHTML,
+ noscript.innerHTML,
+ noscript.firstElementChild === null,
+ doc.body.lastElementChild?.localName
+ ].join("|");
+})()
+"#,
+ )
+ .expect("detached document.write noscript probe should evaluate");
+
+ assert_eq!(
+ result,
+ concat!(
+ "<noscript>&lt;em&gt;fallback&amp;&lt;/em&gt;</noscript><span>tail</span>|",
+ "&lt;em&gt;fallback&amp;&lt;/em&gt;|true|span"
+ )
+ );
+}
+
#[test]
fn detached_domparser_query_and_element_collections_use_native_handles() {
let mut vm = new_storage_test_vm("https://detached-domparser-query.test/");