Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(domparser): preserve adopted XML serialization

ldm039d474864b39
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt
index fce28aede..3c87454ab 100644
--- a/moli-benchmark/wpt-cross-current/failed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt
@@ -3463,7 +3463,6 @@ html/syntax/parsing/template/additions-to-the-in-body-insertion-mode/ignore-head
html/syntax/parsing/template/creating-an-element-for-the-token/template-owner-document.html
html/syntax/parsing/the-end.html
html/syntax/serializing-html-fragments/escaping.html
-html/syntax/serializing-html-fragments/serializing-cdata-in-html-document.html
html/syntax/serializing-html-fragments/serializing.html
html/syntax/serializing-xml-fragments/outerHTML.html
html/syntax/xmldecl/xmldecl-1.html
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index 7c96645e4..b3c90fd45 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -6295,6 +6295,7 @@ html/syntax/parsing/unclosed-svg-script.html
html/syntax/parsing/zero.html
html/syntax/serializing-html-fragments/initial-linefeed-pre.html
html/syntax/serializing-html-fragments/outerHTML.html
+html/syntax/serializing-html-fragments/serializing-cdata-in-html-document.html
html/syntax/serializing-html-fragments/serializing-lt-gt.html
html/syntax/serializing-html-fragments/template.html
html/webappapis/animation-frames/callback-cross-realm-report-exception.html
diff --git a/moli-dom/src/native/host/serialize.rs b/moli-dom/src/native/host/serialize.rs
index 67f486343..c46d818d0 100644
--- a/moli-dom/src/native/host/serialize.rs
+++ b/moli-dom/src/native/host/serialize.rs
@@ -1,5 +1,7 @@
use super::*;
-use crate::native::serialize::is_void_html_element;
+use crate::native::serialize::{
+ escape_html_attribute, escape_html_text, is_void_html_element, serialize_cdata_section,
+};
pub type ShadowRootRegistryAttributePolicy<'a> =
dyn Fn(DomHandle, DomHandle, &ShadowRootInit) -> bool + 'a;
@@ -302,9 +304,12 @@ impl DomHost {
}
}
NodeData::CDataSection(cdata) => {
- out.push_str("<![CDATA[");
- out.push_str(cdata.data());
- out.push_str("]]>");
+ serialize_cdata_section(
+ cdata.data(),
+ out,
+ raw_text_parent,
+ self.node_document_is_html_document(handle).unwrap_or(false),
+ );
}
NodeData::Comment(comment) => {
out.push_str("<!--");
@@ -401,30 +406,6 @@ fn is_raw_text_element(namespace: &str, local_name: &str) -> bool {
&& matches!(local_name, "script" | "style" | "textarea" | "title")
}
-fn escape_html_text(value: &str, out: &mut String) {
- for ch in value.chars() {
- 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(value: &str, out: &mut String) {
- for ch in value.chars() {
- match ch {
- '&' => out.push_str("&amp;"),
- '"' => out.push_str("&quot;"),
- '<' => out.push_str("&lt;"),
- '>' => out.push_str("&gt;"),
- _ => out.push(ch),
- }
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;
diff --git a/moli-dom/src/native/node/mod.rs b/moli-dom/src/native/node/mod.rs
index d5a718efc..dac4ae122 100644
--- a/moli-dom/src/native/node/mod.rs
+++ b/moli-dom/src/native/node/mod.rs
@@ -9,7 +9,10 @@ use std::num::NonZeroU32;
use super::NativeDom;
use super::element::Element;
-use super::serialize::is_void_html_element;
+use super::serialize::{
+ HtmlSerializationSink, escape_html_attribute, escape_html_text, is_void_html_element,
+ serialize_cdata_section,
+};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct NativeNodeId(NonZeroU32);
@@ -716,26 +719,6 @@ impl Node {
}
}
-trait HtmlSerializationSink {
- fn push_str(&mut self, value: &str);
- fn push(&mut self, value: char);
- fn limit_exceeded(&self) -> bool;
-}
-
-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
- }
-}
-
struct BoundedHtmlSerialization {
output: String,
max_bytes: usize,
@@ -873,9 +856,12 @@ fn serialize_html_node_frame<'a, S>(
}
}
NodeData::CDataSection(cdata) => {
- out.push_str("<![CDATA[");
- out.push_str(cdata.data());
- out.push_str("]]>");
+ serialize_cdata_section(
+ cdata.data(),
+ out,
+ raw_text_parent,
+ dom.node_document_is_html_document(node_id).unwrap_or(false),
+ );
}
NodeData::Comment(comment) => {
out.push_str("<!--");
@@ -996,39 +982,3 @@ fn is_raw_text_element(namespace: &str, local_name: &str) -> bool {
namespace == "http://www.w3.org/1999/xhtml"
&& matches!(local_name, "script" | "style" | "noscript")
}
-
-fn escape_html_text<S>(value: &str, out: &mut S)
-where
- S: HtmlSerializationSink,
-{
- 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,
-{
- 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),
- }
- }
-}
diff --git a/moli-dom/src/native/serialize.rs b/moli-dom/src/native/serialize.rs
index f23231e48..d93c3a215 100644
--- a/moli-dom/src/native/serialize.rs
+++ b/moli-dom/src/native/serialize.rs
@@ -19,6 +19,83 @@ impl std::fmt::Display for HtmlSerializationLimitExceeded {
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;
+}
+
+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!(
@@ -281,4 +358,52 @@ mod tests {
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, false, &[]).as_deref(),
+ Some("&lt;img&gt;&amp;")
+ );
+ }
}
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 e00860dac..73b176b29 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
@@ -73,6 +73,41 @@ fn detached_domparser_query_and_element_collections_use_native_handles() {
);
}
+#[test]
+fn adopted_xml_cdata_uses_html_fragment_serialization() {
+ let mut vm = new_storage_test_vm("https://adopted-xml-cdata.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const parsed = new DOMParser().parseFromString(
+ '<svg xmlns="http://www.w3.org/2000/svg"><![CDATA[<img>]]></svg>',
+ 'application/xml'
+ );
+ const element = parsed.documentElement;
+ const before = element.outerHTML;
+ const adopted = document.adoptNode(element);
+ return JSON.stringify({
+ before,
+ after: adopted.outerHTML,
+ declarationValue: adopted.getAttributeNS(
+ 'http://www.w3.org/2000/xmlns/',
+ 'xmlns'
+ ),
+ cdataNodeType: adopted.firstChild && adopted.firstChild.nodeType
+ });
+})()
+"#,
+ )
+ .expect("adopted XML CDATA serialization probe should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"before":"<svg xmlns=\"http://www.w3.org/2000/svg\"><![CDATA[<img>]]></svg>","after":"<svg xmlns=\"http://www.w3.org/2000/svg\">&lt;img&gt;</svg>","declarationValue":"http://www.w3.org/2000/svg","cdataNodeType":4}"#
+ );
+}
+
#[test]
fn detached_query_brand_checks_accept_standard_prototype_methods() {
let mut vm = new_storage_test_vm("https://detached-query-brand-check.test/");