Skip to content

The headless browser for AI agents and web scraping

perf(dom): avoid per-element node clone when serializing template contents

SGavrlbecb3d941f28
diff --git a/crates/obscura-dom/src/serialize.rs b/crates/obscura-dom/src/serialize.rs
index 38509457..8cb2abfe 100644
--- a/crates/obscura-dom/src/serialize.rs
+++ b/crates/obscura-dom/src/serialize.rs
@@ -30,10 +30,12 @@ impl DomTree {
// there rather than under the element (issue #463); for everything else it
// is the node itself.
fn content_source(&self, node_id: NodeId) -> NodeId {
- match self.get_node(node_id).map(|n| n.data) {
- Some(NodeData::Element { template_contents: Some(contents), .. }) => contents,
- _ => node_id,
- }
+ self.with_node(node_id, |n| match &n.data {
+ NodeData::Element { template_contents: Some(contents), .. } => Some(*contents),
+ _ => None,
+ })
+ .flatten()
+ .unwrap_or(node_id)
}
// Children as work items in document order (top of the LIFO stack first).
@@ -90,7 +92,7 @@ impl DomTree {
buf.push_str(name);
buf.push('>');
}
- NodeData::Element { name, attrs, .. } => {
+ NodeData::Element { name, attrs, template_contents, .. } => {
let tag = name.local.as_ref();
if include_self {
buf.push('<');
@@ -113,8 +115,11 @@ impl DomTree {
stack.push(SerializeWork::CloseTag(tag.to_string()));
}
// A <template> serializes its contents document, not its
- // own (always empty) children — issue #463.
- for w in self.child_work(self.content_source(node_id)) {
+ // own (always empty) children (issue #463). template_contents
+ // is already in hand from this node, so read it directly
+ // rather than re-fetching and cloning the node.
+ let source = template_contents.unwrap_or(node_id);
+ for w in self.child_work(source) {
stack.push(w);
}
}