Skip to content

add defaultView getter to HTMLDocument #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/browser/env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/browser/html/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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" },
}, .{});
}
1 change: 1 addition & 0 deletions src/browser/page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions src/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.window = try Window.create(null, null);
try self.window.replaceDocument(document);
try self.window.replaceLocation(.{
.url = try self.url.toWebApi(arena),
});

self.storage_shelf = storage.Shelf.init(arena);
self.window.setStorageShelf(&self.storage_shelf);

Expand Down