mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 14:43:28 +00:00
Merge pull request #1101 from lightpanda-io/nikneym/window-onload
Add `window.onload` getter and setter
This commit is contained in:
@@ -35,6 +35,7 @@ const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
|
||||
const Screen = @import("screen.zig").Screen;
|
||||
const domcss = @import("../dom/css.zig");
|
||||
const Css = @import("../css/css.zig").Css;
|
||||
const EventHandler = @import("../events/event.zig").EventHandler;
|
||||
|
||||
const Function = Env.Function;
|
||||
|
||||
@@ -67,6 +68,7 @@ pub const Window = struct {
|
||||
performance: Performance,
|
||||
screen: Screen = .{},
|
||||
css: Css = .{},
|
||||
onload_callback: ?Function = null,
|
||||
|
||||
pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window {
|
||||
var fbs = std.io.fixedBufferStream("");
|
||||
@@ -101,6 +103,37 @@ pub const Window = struct {
|
||||
return fetchFn(input, options, page);
|
||||
}
|
||||
|
||||
/// Returns `onload_callback`.
|
||||
pub fn get_onload(self: *const Window) ?Function {
|
||||
return self.onload_callback;
|
||||
}
|
||||
|
||||
/// Sets `onload_callback`.
|
||||
pub fn set_onload(self: *Window, maybe_listener: ?EventHandler.Listener, page: *Page) !void {
|
||||
const event_target = parser.toEventTarget(Window, self);
|
||||
|
||||
if (self.onload_callback) |callback| {
|
||||
// If we got here, it means `onload_callback` has been set before
|
||||
// so listener cannot be null.
|
||||
const listener = try parser.eventTargetHasListener(event_target, "load", false, callback.id);
|
||||
std.debug.assert(listener != null);
|
||||
|
||||
try parser.eventTargetRemoveEventListener(event_target, "load", listener.?, false);
|
||||
}
|
||||
|
||||
if (maybe_listener) |listener| {
|
||||
// The only time this can return null if the listener is already
|
||||
// registered. But before calling `register`, all of our functions
|
||||
// remove any existing listener, so it should be impossible to get null
|
||||
// from this function call.
|
||||
_ = try EventHandler.register(page.arena, event_target, "load", listener, null) orelse unreachable;
|
||||
self.onload_callback = listener.function;
|
||||
} else {
|
||||
// Just unset the listener.
|
||||
self.onload_callback = null;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_window(self: *Window) *Window {
|
||||
return self;
|
||||
}
|
||||
|
||||
@@ -104,3 +104,16 @@
|
||||
});
|
||||
testing.eventually(() => testing.expectEqual(true, dcl));
|
||||
</script>
|
||||
|
||||
<script id=window.onload>
|
||||
let isWindowTarget = false;
|
||||
|
||||
const callback = (e) => isWindowTarget = e.target === window;
|
||||
// Callback is not set yet.
|
||||
testing.expectEqual(null, window.onload);
|
||||
// Callback is set.
|
||||
window.onload = callback;
|
||||
testing.expectEqual(callback, window.onload);
|
||||
|
||||
testing.eventually(() => testing.expectEqual(true, isWindowTarget));
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user