Skip to content

Lightpanda: the headless browser designed for AI and automation

dump: serialize template contents

Jay Hemnanid113a38785d0
diff --git a/src/browser/dump.zig b/src/browser/dump.zig
index 30eca503e..95d33a8a0 100644
--- a/src/browser/dump.zig
+++ b/src/browser/dump.zig
@@ -24,6 +24,7 @@ const LimitedWriter = @import("../LimitedWriter.zig");
const Node = @import("webapi/Node.zig");
const Slot = @import("webapi/element/html/Slot.zig");
const IFrame = @import("webapi/element/html/IFrame.zig");
+const Template = @import("webapi/element/html/Template.zig");
pub const Opts = struct {
with_base: bool = false,
@@ -307,7 +308,9 @@ const Walk = struct {
if (opts.with_frames and el.is(IFrame) != null) {
return self.open(end_tag, .{ .document = el.as(IFrame).getContentDocument() });
}
- if (node.firstChild()) |first| {
+ // A template serializes its contents, not its own children.
+ const content_root = if (el.is(Template)) |template| template.getContent().asNode() else node;
+ if (content_root.firstChild()) |first| {
return self.open(end_tag, .{ .siblings = first });
}
// No children: skip the stack
diff --git a/src/browser/tests/element/html/template.html b/src/browser/tests/element/html/template.html
index 693749a55..fafacf4bf 100644
--- a/src/browser/tests/element/html/template.html
+++ b/src/browser/tests/element/html/template.html
@@ -284,3 +284,13 @@
testing.expectEqual('b', d.innerHTML);
}
</script>
+
+<div id="tpl_parent"><template><b>x</b></template></div>
+<script id="serialize_from_ancestor">
+{
+ const parent = $('#tpl_parent');
+ testing.expectEqual('<template><b>x</b></template>', parent.innerHTML);
+ testing.expectEqual('<div id="tpl_parent"><template><b>x</b></template></div>', parent.outerHTML);
+ testing.expectEqual('<template><b>x</b></template>', parent.firstChild.outerHTML);
+}
+</script>