Skip to content

Commit a99d193

Browse files
Merge pull request #653 from lightpanda-io/document_default_view
add defaultView getter to HTMLDocument
2 parents a3b576a + f165131 commit a99d193

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/browser/env.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ pub const JsThis = Env.JsThis;
4444
pub const JsObject = Env.JsObject;
4545
pub const Callback = Env.Callback;
4646
pub const Env = js.Env(*SessionState, WebApis);
47-
pub const Global = @import("html/window.zig").Window;
47+
48+
const Window = @import("html/window.zig").Window;
49+
pub const Global = Window;
4850

4951
pub const SessionState = struct {
5052
loop: *Loop,
5153
url: *const URL,
54+
window: *Window,
5255
renderer: *Renderer,
5356
arena: std.mem.Allocator,
5457
http_client: *HttpClient,

src/browser/html/document.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const std = @import("std");
2121
const parser = @import("../netsurf.zig");
2222
const SessionState = @import("../env.zig").SessionState;
2323

24+
const Window = @import("window.zig").Window;
2425
const Document = @import("../dom/document.zig").Document;
2526
const NodeList = @import("../dom/nodelist.zig").NodeList;
2627
const Location = @import("location.zig").Location;
@@ -171,6 +172,10 @@ pub const HTMLDocument = struct {
171172
return "off";
172173
}
173174

175+
pub fn get_defaultView(_: *parser.DocumentHTML, state: *const SessionState) *Window {
176+
return state.window;
177+
}
178+
174179
// noop legacy functions
175180
// https://html.spec.whatwg.org/#Document-partial
176181
pub fn _clear(_: *parser.DocumentHTML) void {}
@@ -267,4 +272,8 @@ test "Browser.HTML.Document" {
267272
.{ "document.all(5)", "[object HTMLParagraphElement]" },
268273
.{ "document.all('content')", "[object HTMLDivElement]" },
269274
}, .{});
275+
276+
try runner.testCases(&.{
277+
.{ "document.defaultView.document == document", "true" },
278+
}, .{});
270279
}

src/browser/page.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub const Page = struct {
9797
.arena = arena,
9898
.document = null,
9999
.url = &self.url,
100+
.window = &self.window,
100101
.renderer = &self.renderer,
101102
.loop = browser.app.loop,
102103
.cookie_jar = &session.cookie_jar,

src/runtime/js.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,9 +1972,16 @@ fn Caller(comptime E: type, comptime State: type) type {
19721972
const params = @typeInfo(F).@"fn".params;
19731973

19741974
const param = params[index].type.?;
1975-
if (param != State) {
1976-
@compileError(std.fmt.comptimePrint("The {d} parameter to {s} must be a {s}. Got: {s}", .{ index, named_function.full_name, @typeName(State), @typeName(param) }));
1975+
if (param == State) {
1976+
return;
1977+
}
1978+
1979+
if (@typeInfo(State) == .pointer) {
1980+
if (param == *const @typeInfo(State).pointer.child) {
1981+
return;
1982+
}
19771983
}
1984+
@compileError(std.fmt.comptimePrint("The {d} parameter to {s} must be a {s}. Got: {s}", .{ index, named_function.full_name, @typeName(State), @typeName(param) }));
19781985
}
19791986

19801987
fn handleError(self: *Self, comptime Struct: type, comptime named_function: NamedFunction, err: anyerror, info: anytype) void {

src/testing.zig

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,23 @@ pub const JsRunner = struct {
412412
var html = std.io.fixedBufferStream(opts.html);
413413
const document = try parser.documentHTMLParse(html.reader(), "UTF-8");
414414

415+
self.window = try Window.create(null, null);
416+
try self.window.replaceDocument(document);
417+
try self.window.replaceLocation(.{
418+
.url = try self.url.toWebApi(arena),
419+
});
420+
415421
self.state = .{
416422
.arena = arena,
417423
.loop = &self.loop,
418424
.document = document,
419425
.url = &self.url,
426+
.window = &self.window,
420427
.renderer = &self.renderer,
421428
.cookie_jar = &self.cookie_jar,
422429
.http_client = &self.http_client,
423430
};
424431

425-
self.window = try Window.create(null, null);
426-
try self.window.replaceDocument(document);
427-
try self.window.replaceLocation(.{
428-
.url = try self.url.toWebApi(arena),
429-
});
430-
431432
self.storage_shelf = storage.Shelf.init(arena);
432433
self.window.setStorageShelf(&self.storage_shelf);
433434

0 commit comments

Comments
 (0)