Skip to content

Commit 2a15122

Browse files
authored
Merge pull request #1101 from lightpanda-io/nikneym/window-onload
Add `window.onload` getter and setter
2 parents 54f9bfb + 1d50e09 commit 2a15122

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/browser/html/window.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
3535
const Screen = @import("screen.zig").Screen;
3636
const domcss = @import("../dom/css.zig");
3737
const Css = @import("../css/css.zig").Css;
38+
const EventHandler = @import("../events/event.zig").EventHandler;
3839

3940
const Function = Env.Function;
4041

@@ -67,6 +68,7 @@ pub const Window = struct {
6768
performance: Performance,
6869
screen: Screen = .{},
6970
css: Css = .{},
71+
onload_callback: ?Function = null,
7072

7173
pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window {
7274
var fbs = std.io.fixedBufferStream("");
@@ -101,6 +103,37 @@ pub const Window = struct {
101103
return fetchFn(input, options, page);
102104
}
103105

106+
/// Returns `onload_callback`.
107+
pub fn get_onload(self: *const Window) ?Function {
108+
return self.onload_callback;
109+
}
110+
111+
/// Sets `onload_callback`.
112+
pub fn set_onload(self: *Window, maybe_listener: ?EventHandler.Listener, page: *Page) !void {
113+
const event_target = parser.toEventTarget(Window, self);
114+
115+
if (self.onload_callback) |callback| {
116+
// If we got here, it means `onload_callback` has been set before
117+
// so listener cannot be null.
118+
const listener = try parser.eventTargetHasListener(event_target, "load", false, callback.id);
119+
std.debug.assert(listener != null);
120+
121+
try parser.eventTargetRemoveEventListener(event_target, "load", listener.?, false);
122+
}
123+
124+
if (maybe_listener) |listener| {
125+
// The only time this can return null if the listener is already
126+
// registered. But before calling `register`, all of our functions
127+
// remove any existing listener, so it should be impossible to get null
128+
// from this function call.
129+
_ = try EventHandler.register(page.arena, event_target, "load", listener, null) orelse unreachable;
130+
self.onload_callback = listener.function;
131+
} else {
132+
// Just unset the listener.
133+
self.onload_callback = null;
134+
}
135+
}
136+
104137
pub fn get_window(self: *Window) *Window {
105138
return self;
106139
}

src/tests/window/window.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@
104104
});
105105
testing.eventually(() => testing.expectEqual(true, dcl));
106106
</script>
107+
108+
<script id=window.onload>
109+
let isWindowTarget = false;
110+
111+
const callback = (e) => isWindowTarget = e.target === window;
112+
// Callback is not set yet.
113+
testing.expectEqual(null, window.onload);
114+
// Callback is set.
115+
window.onload = callback;
116+
testing.expectEqual(callback, window.onload);
117+
118+
testing.eventually(() => testing.expectEqual(true, isWindowTarget));
119+
</script>

0 commit comments

Comments
 (0)