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): serialize HTML void elements without children

ldm08704569963c0
diff --git a/moli-dom/src/native/host/serialize.rs b/moli-dom/src/native/host/serialize.rs
index c46d818d0..9aa2a50e1 100644
--- a/moli-dom/src/native/host/serialize.rs
+++ b/moli-dom/src/native/host/serialize.rs
@@ -117,6 +117,11 @@ impl DomHost {
raw_text_parent: false,
}],
HtmlSerializationTarget::ChildrenOnly => {
+ if node.as_element().is_some_and(|element| {
+ is_void_html_element(element.namespace(), element.local_name())
+ }) {
+ return Some(String::new());
+ }
let mut stack = vec![HostHtmlSerializationFrame::Children {
handle,
raw_text_parent: false,
@@ -281,19 +286,19 @@ impl DomHost {
stack.push(HostHtmlSerializationFrame::CloseElement(
element.local_name(),
));
- }
- stack.push(HostHtmlSerializationFrame::Children {
- handle,
- raw_text_parent: raw_text_child,
- });
- if let Some((shadow_root, init)) =
- self.serialized_shadow_root_for_host(handle, shadow_root_inclusion)
- {
- stack.push(HostHtmlSerializationFrame::ShadowRootTemplate {
- host: handle,
- shadow_root,
- init,
+ stack.push(HostHtmlSerializationFrame::Children {
+ handle,
+ raw_text_parent: raw_text_child,
});
+ if let Some((shadow_root, init)) =
+ self.serialized_shadow_root_for_host(handle, shadow_root_inclusion)
+ {
+ stack.push(HostHtmlSerializationFrame::ShadowRootTemplate {
+ host: handle,
+ shadow_root,
+ init,
+ });
+ }
}
}
NodeData::Text(text) => {
diff --git a/moli-dom/src/native/node/mod.rs b/moli-dom/src/native/node/mod.rs
index dac4ae122..98ff0bca1 100644
--- a/moli-dom/src/native/node/mod.rs
+++ b/moli-dom/src/native/node/mod.rs
@@ -845,8 +845,8 @@ fn serialize_html_node_frame<'a, S>(
let children_root = element.template_contents().unwrap_or(node_id);
if !is_void_html_element(element.namespace(), element.local_name()) {
stack.push(HtmlSerializationFrame::CloseElement(element.local_name()));
+ push_child_html_serialization_frames(dom, children_root, raw_text_child, stack);
}
- push_child_html_serialization_frames(dom, children_root, raw_text_child, stack);
}
NodeData::Text(text) => {
if raw_text_parent {
diff --git a/moli-dom/src/native/serialize.rs b/moli-dom/src/native/serialize.rs
index d93c3a215..121640c8a 100644
--- a/moli-dom/src/native/serialize.rs
+++ b/moli-dom/src/native/serialize.rs
@@ -102,12 +102,16 @@ pub(super) fn is_void_html_element(namespace: &str, local_name: &str) -> bool {
local_name,
"area"
| "base"
+ | "basefont"
+ | "bgsound"
| "br"
| "col"
| "embed"
+ | "frame"
| "hr"
| "img"
| "input"
+ | "keygen"
| "link"
| "meta"
| "param"
@@ -166,6 +170,12 @@ impl NativeDom {
pub fn inner_html(&self, node_id: NativeNodeId) -> Option<String> {
let node = self.node(node_id)?;
+ if node
+ .as_element()
+ .is_some_and(|element| is_void_html_element(element.namespace(), element.local_name()))
+ {
+ return Some(String::new());
+ }
let mut html = String::new();
let raw_text_child = node.as_element().is_some_and(|element| {
element.namespace() == "http://www.w3.org/1999/xhtml"
@@ -264,13 +274,26 @@ mod tests {
#[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", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param",
- "source", "track", "wbr",
+ "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")
@@ -281,13 +304,15 @@ mod tests {
);
let mut host = DomHost::from_dom(dom);
- let container = host.create_element("div");
let param = host.create_element("param");
assert!(host.append_child(container, param));
assert_eq!(
host.get_html(container, false, &[]).as_deref(),
- Some("<param>")
+ Some(format!("{expected}<param>").as_str())
);
+ for element in void_elements {
+ assert_eq!(host.get_html(element, false, &[]).as_deref(), Some(""));
+ }
}
#[test]