@@ -60,8 +60,6 @@ pub const Page = struct {
6060 // Serves are the root object of our JavaScript environment
6161 window : Window ,
6262
63- doc : ? * parser.Document ,
64-
6563 // The URL of the page
6664 url : URL ,
6765
@@ -87,7 +85,6 @@ pub const Page = struct {
8785 self .* = .{
8886 .window = try Window .create (null , null ),
8987 .arena = arena ,
90- .doc = null ,
9188 .raw_data = null ,
9289 .url = URL .empty ,
9390 .session = session ,
@@ -121,15 +118,14 @@ pub const Page = struct {
121118
122119 // dump writes the page content into the given file.
123120 pub fn dump (self : * const Page , out : std.fs.File ) ! void {
124- // if no HTML document pointer available, dump the data content only.
125- if (self .doc == null ) {
126- // no data loaded, nothing to do.
127- if (self .raw_data == null ) return ;
128- return try out .writeAll (self .raw_data .? );
121+ if (self .raw_data ) | raw_data | {
122+ // raw_data was set if the document was not HTML, dump the data content only.
123+ return try out .writeAll (raw_data );
129124 }
130125
131126 // if the page has a pointer to a document, dumps the HTML.
132- try Dump .writeHTML (self .doc .? , out );
127+ const doc = parser .documentHTMLToDocument (self .window .document );
128+ try Dump .writeHTML (doc , out );
133129 }
134130
135131 pub fn fetchModuleSource (ctx : * anyopaque , specifier : []const u8 ) ! ? []const u8 {
@@ -187,7 +183,7 @@ pub const Page = struct {
187183 try self .loadHTMLDoc (fbs .reader (), "utf-8" );
188184 // We do not processHTMLDoc here as we know we don't have any scripts
189185 // This assumption may be false when CDP Page.addScriptToEvaluateOnNewDocument is implemented
190- try HTMLDocument .documentIsComplete (self .window .document .? , & self .state );
186+ try HTMLDocument .documentIsComplete (self .window .document , & self .state );
191187 return ;
192188 }
193189
@@ -229,6 +225,7 @@ pub const Page = struct {
229225 } orelse .unknown ;
230226
231227 if (mime .isHTML ()) {
228+ self .raw_data = null ;
232229 try self .loadHTMLDoc (& response , mime .charset orelse "utf-8" );
233230 try self .processHTMLDoc ();
234231 } else {
@@ -256,9 +253,6 @@ pub const Page = struct {
256253 const html_doc = try parser .documentHTMLParse (reader , ccharset );
257254 const doc = parser .documentHTMLToDocument (html_doc );
258255
259- // save a document's pointer in the page.
260- self .doc = doc ;
261-
262256 // inject the URL to the document including the fragment.
263257 try parser .documentSetDocumentURI (doc , self .url .raw );
264258
@@ -270,8 +264,8 @@ pub const Page = struct {
270264 }
271265
272266 fn processHTMLDoc (self : * Page ) ! void {
273- const doc = self .doc .? ;
274- const html_doc = self . window . document .? ;
267+ const html_doc = self .window . document ;
268+ const doc = parser . documentHTMLToDocument ( html_doc ) ;
275269
276270 const document_element = (try parser .documentGetDocumentElement (doc )) orelse return error .DocumentElementError ;
277271 try parser .eventTargetAddEventListener (
0 commit comments