@@ -183,6 +183,11 @@ pub const Page = struct {
183183
184184 // if the url is about:blank, nothing to do.
185185 if (std .mem .eql (u8 , "about:blank" , request_url .raw )) {
186+ var fbs = std .io .fixedBufferStream ("" );
187+ try self .loadHTMLDoc (fbs .reader (), "utf-8" );
188+ // We do not processHTMLDoc here as we know we don't have any scripts
189+ // This assumption may be false when CDP Page.addScriptToEvaluateOnNewDocument is implemented
190+ try HTMLDocument .documentIsComplete (self .window .document .? , & self .state );
186191 return ;
187192 }
188193
@@ -225,6 +230,7 @@ pub const Page = struct {
225230
226231 if (mime .isHTML ()) {
227232 try self .loadHTMLDoc (& response , mime .charset orelse "utf-8" );
233+ try self .processHTMLDoc ();
228234 } else {
229235 log .info ("non-HTML document: {s}" , .{content_type orelse "null" });
230236 var arr : std .ArrayListUnmanaged (u8 ) = .{};
@@ -243,29 +249,16 @@ pub const Page = struct {
243249
244250 // https://html.spec.whatwg.org/#read-html
245251 fn loadHTMLDoc (self : * Page , reader : anytype , charset : []const u8 ) ! void {
246- const arena = self .arena ;
247-
248252 log .debug ("parse html with charset {s}" , .{charset });
249253
250- const ccharset = try arena .dupeZ (u8 , charset );
254+ const ccharset = try self . arena .dupeZ (u8 , charset );
251255
252256 const html_doc = try parser .documentHTMLParse (reader , ccharset );
253257 const doc = parser .documentHTMLToDocument (html_doc );
254258
255259 // save a document's pointer in the page.
256260 self .doc = doc ;
257261
258- const document_element = (try parser .documentGetDocumentElement (doc )) orelse return error .DocumentElementError ;
259- try parser .eventTargetAddEventListener (
260- parser .toEventTarget (parser .Element , document_element ),
261- "click" ,
262- & self .window_clicked_event_node ,
263- false ,
264- );
265-
266- // TODO set document.readyState to interactive
267- // https://html.spec.whatwg.org/#reporting-document-loading-status
268-
269262 // inject the URL to the document including the fragment.
270263 try parser .documentSetDocumentURI (doc , self .url .raw );
271264
@@ -274,6 +267,19 @@ pub const Page = struct {
274267 self .window .setStorageShelf (
275268 try self .session .storage_shed .getOrPut (try self .origin (self .arena )),
276269 );
270+ }
271+
272+ fn processHTMLDoc (self : * Page ) ! void {
273+ const doc = self .doc .? ;
274+ const html_doc = self .window .document .? ;
275+
276+ const document_element = (try parser .documentGetDocumentElement (doc )) orelse return error .DocumentElementError ;
277+ try parser .eventTargetAddEventListener (
278+ parser .toEventTarget (parser .Element , document_element ),
279+ "click" ,
280+ & self .window_clicked_event_node ,
281+ false ,
282+ );
277283
278284 // https://html.spec.whatwg.org/#read-html
279285
@@ -320,12 +326,12 @@ pub const Page = struct {
320326 // > parsing and evaluated as soon as it is available.
321327 // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#async
322328 if (script .is_async ) {
323- try async_scripts .append (arena , script );
329+ try async_scripts .append (self . arena , script );
324330 continue ;
325331 }
326332
327333 if (script .is_defer ) {
328- try defer_scripts .append (arena , script );
334+ try defer_scripts .append (self . arena , script );
329335 continue ;
330336 }
331337
0 commit comments