Skip to content

Commit 0929bd2

Browse files
committed
load aboutblank doc
1 parent fc0281b commit 0929bd2

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/browser/page.zig

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/cdp/domains/target.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ fn createTarget(cmd: anytype) !void {
125125
bc.target_id = target_id;
126126

127127
var page = try bc.session.createPage();
128+
// Navigate to about:blank such that the window.document are created and set
129+
const url = @import("../../url.zig").URL.about_blank;
130+
try page.navigate(url, .{});
128131
{
129132
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
130133
bc.inspector.contextCreated(
@@ -501,6 +504,10 @@ test "cdp.target: createTarget" {
501504
// should create a browser context
502505
const bc = ctx.cdp().browser_context.?;
503506
try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = bc.id, .targetId = bc.target_id.? } }, .{});
507+
508+
// Even about:blank should set the window.document
509+
const page = ctx.cdp().browser_context.?.session.page.?;
510+
try testing.expect(page.window.document != null);
504511
}
505512

506513
{

src/url.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub const URL = struct {
99
raw: []const u8,
1010

1111
pub const empty = URL{ .uri = .{ .scheme = "" }, .raw = "" };
12+
pub const about_blank = URL{ .uri = .{ .scheme = "" }, .raw = "about:blank" };
1213

1314
// We assume str will last as long as the URL
1415
// In some cases, this is safe to do, because we know the URL is short lived.

0 commit comments

Comments
 (0)