Skip to content

Commit 01ce086

Browse files
committed
load aboutblank doc to set window
1 parent 884ec05 commit 01ce086

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/browser/browser.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ pub const Page = struct {
335335

336336
// if the url is about:blank, nothing to do.
337337
if (std.mem.eql(u8, "about:blank", request_url.raw)) {
338+
var about_blank = AboutBlank{};
339+
try self.loadHTMLDoc(&about_blank, "utf-8");
338340
return;
339341
}
340342

@@ -393,6 +395,26 @@ pub const Page = struct {
393395
});
394396
}
395397

398+
// A minimal reader for about:blank such that it can be parsed by loadHTMLDoc.
399+
pub const AboutBlank = struct {
400+
const content =
401+
\\ <!DOCTYPE html>
402+
\\ <html>
403+
\\ <head>
404+
\\ <title></title>
405+
\\ </head>
406+
\\ <body>
407+
\\ </body>
408+
\\ </html>
409+
;
410+
done: bool = false,
411+
pub fn next(self: *AboutBlank) !?[]const u8 {
412+
if (self.done) return null;
413+
self.done = true;
414+
return AboutBlank.content;
415+
}
416+
};
417+
396418
// https://html.spec.whatwg.org/#read-html
397419
fn loadHTMLDoc(self: *Page, reader: anytype, charset: []const u8) !void {
398420
const arena = self.arena;

src/cdp/domains/target.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ 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 / state.document are created and set
129+
// Playwright uses these before navigating to a real URL to detect when they are removed.
130+
const url = try @import("../../url.zig").URL.parse("about:blank", "");
131+
try page.navigate(url, .{});
128132
{
129133
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
130134
bc.inspector.contextCreated(
@@ -501,6 +505,11 @@ test "cdp.target: createTarget" {
501505
// should create a browser context
502506
const bc = ctx.cdp().browser_context.?;
503507
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.? } }, .{});
508+
509+
// Even about:blank should set the window and document
510+
const page = ctx.cdp().browser_context.?.session.page.?;
511+
try testing.expect(page.state.document != null);
512+
try testing.expect(page.window.document != null);
504513
}
505514

506515
{

0 commit comments

Comments
 (0)