add defaultView getter to HTMLDocument

This commit is contained in:
Karl Seguin
2025-05-16 20:33:28 +08:00
parent afd29fef81
commit f165131da8
5 changed files with 34 additions and 13 deletions

View File

@@ -44,11 +44,14 @@ pub const JsThis = Env.JsThis;
pub const JsObject = Env.JsObject;
pub const Callback = Env.Callback;
pub const Env = js.Env(*SessionState, WebApis);
pub const Global = @import("html/window.zig").Window;
const Window = @import("html/window.zig").Window;
pub const Global = Window;
pub const SessionState = struct {
loop: *Loop,
url: *const URL,
window: *Window,
renderer: *Renderer,
arena: std.mem.Allocator,
http_client: *HttpClient,

View File

@@ -21,6 +21,7 @@ const std = @import("std");
const parser = @import("../netsurf.zig");
const SessionState = @import("../env.zig").SessionState;
const Window = @import("window.zig").Window;
const Document = @import("../dom/document.zig").Document;
const NodeList = @import("../dom/nodelist.zig").NodeList;
const Location = @import("location.zig").Location;
@@ -171,6 +172,10 @@ pub const HTMLDocument = struct {
return "off";
}
pub fn get_defaultView(_: *parser.DocumentHTML, state: *const SessionState) *Window {
return state.window;
}
// noop legacy functions
// https://html.spec.whatwg.org/#Document-partial
pub fn _clear(_: *parser.DocumentHTML) void {}
@@ -267,4 +272,8 @@ test "Browser.HTML.Document" {
.{ "document.all(5)", "[object HTMLParagraphElement]" },
.{ "document.all('content')", "[object HTMLDivElement]" },
}, .{});
try runner.testCases(&.{
.{ "document.defaultView.document == document", "true" },
}, .{});
}

View File

@@ -97,6 +97,7 @@ pub const Page = struct {
.arena = arena,
.document = null,
.url = &self.url,
.window = &self.window,
.renderer = &self.renderer,
.loop = browser.app.loop,
.cookie_jar = &session.cookie_jar,

View File

@@ -1952,9 +1952,16 @@ fn Caller(comptime E: type, comptime State: type) type {
const params = @typeInfo(F).@"fn".params;
const param = params[index].type.?;
if (param != State) {
@compileError(std.fmt.comptimePrint("The {d} parameter to {s} must be a {s}. Got: {s}", .{ index, named_function.full_name, @typeName(State), @typeName(param) }));
if (param == State) {
return;
}
if (@typeInfo(State) == .pointer) {
if (param == *const @typeInfo(State).pointer.child) {
return;
}
}
@compileError(std.fmt.comptimePrint("The {d} parameter to {s} must be a {s}. Got: {s}", .{ index, named_function.full_name, @typeName(State), @typeName(param) }));
}
fn handleError(self: *Self, comptime Struct: type, comptime named_function: NamedFunction, err: anyerror, info: anytype) void {

View File

@@ -412,22 +412,23 @@ pub const JsRunner = struct {
var html = std.io.fixedBufferStream(opts.html);
const document = try parser.documentHTMLParse(html.reader(), "UTF-8");
self.state = .{
.arena = arena,
.loop = &self.loop,
.document = document,
.url = &self.url,
.renderer = &self.renderer,
.cookie_jar = &self.cookie_jar,
.http_client = &self.http_client,
};
self.window = try Window.create(null, null);
try self.window.replaceDocument(document);
try self.window.replaceLocation(.{
.url = try self.url.toWebApi(arena),
});
self.state = .{
.arena = arena,
.loop = &self.loop,
.document = document,
.url = &self.url,
.window = &self.window,
.renderer = &self.renderer,
.cookie_jar = &self.cookie_jar,
.http_client = &self.http_client,
};
self.storage_shelf = storage.Shelf.init(arena);
self.window.setStorageShelf(&self.storage_shelf);