@@ -141,16 +141,48 @@ pub const Page = struct {
141141 repeat_delay .* = 100 * std .time .ns_per_ms ;
142142 }
143143
144+ pub const DumpOpts = struct {
145+ exclude_scripts : bool = false ,
146+ with_base : bool = false ,
147+ };
148+
144149 // dump writes the page content into the given file.
145- pub fn dump (self : * const Page , opts : Dump.Opts , out : std.fs.File ) ! void {
150+ pub fn dump (self : * const Page , opts : DumpOpts , out : std.fs.File ) ! void {
146151 if (self .raw_data ) | raw_data | {
147152 // raw_data was set if the document was not HTML, dump the data content only.
148153 return try out .writeAll (raw_data );
149154 }
150155
151156 // if the page has a pointer to a document, dumps the HTML.
152157 const doc = parser .documentHTMLToDocument (self .window .document );
153- try Dump .writeHTML (doc , opts , out );
158+
159+ // if the base si requested, add the base's node in the document's headers.
160+ if (opts .with_base ) {
161+ try self .addDOMTreeBase ();
162+ }
163+
164+ try Dump .writeHTML (doc , .{
165+ .exclude_scripts = opts .exclude_scripts ,
166+ }, out );
167+ }
168+
169+ // addDOMTreeBase modifies the page's document to add a <base> tag after
170+ // <head>.
171+ // If <head> is missing, the function return silently.
172+ fn addDOMTreeBase (self : * const Page ) ! void {
173+ const doc = parser .documentHTMLToDocument (self .window .document );
174+ std .debug .assert (doc .is_html );
175+
176+ // find <head> tag
177+ // It should be the firstChild of the firstElement
178+ const list = try parser .documentGetElementsByTagName (doc , "head" );
179+ const head = try parser .nodeListItem (list , 0 ) orelse return ;
180+
181+ const base = try parser .documentCreateElement (doc , "base" );
182+ try parser .elementSetAttribute (base , "href" , self .url .raw );
183+
184+ const Node = @import ("dom/node.zig" ).Node ;
185+ try Node .prepend (head , &[_ ]Node.NodeOrText {.{ .node = parser .elementToNode (base ) }});
154186 }
155187
156188 pub fn fetchModuleSource (ctx : * anyopaque , src : []const u8 ) ! ? []const u8 {
0 commit comments