Skip to content

Commit 002d9c1

Browse files
authored
Merge pull request #841 from lightpanda-io/scroll_events
make window.scrollTo triggers scroll and scrollend events
2 parents 22a644b + 3d17c53 commit 002d9c1

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/browser/html/window.zig

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,31 @@ pub const Window = struct {
298298
behavior: []const u8,
299299
};
300300
};
301-
pub fn _scrollTo(_: *const Window, opts: ScrollToOpts, y: ?u32) void {
301+
pub fn _scrollTo(self: *Window, opts: ScrollToOpts, y: ?u32) !void {
302302
_ = opts;
303303
_ = y;
304+
305+
{
306+
const scroll_event = try parser.eventCreate();
307+
defer parser.eventDestroy(scroll_event);
308+
309+
try parser.eventInit(scroll_event, "scroll", .{});
310+
_ = try parser.eventTargetDispatchEvent(
311+
parser.toEventTarget(Window, self),
312+
scroll_event,
313+
);
314+
}
315+
316+
{
317+
const scroll_end = try parser.eventCreate();
318+
defer parser.eventDestroy(scroll_end);
319+
320+
try parser.eventInit(scroll_end, "scrollend", .{});
321+
_ = try parser.eventTargetDispatchEvent(
322+
parser.toEventTarget(parser.DocumentHTML, self.document),
323+
scroll_end,
324+
);
325+
}
304326
}
305327
};
306328

@@ -437,4 +459,13 @@ test "Browser.HTML.Window" {
437459
.{ "str", "https://ziglang.org/documentation/master/std/#std.base64.Base64Decoder" },
438460
.{ "try { atob('b') } catch (e) { e } ", "Error: InvalidCharacterError" },
439461
}, .{});
462+
463+
try runner.testCases(&.{
464+
.{ "let scroll = false; let scrolend = false", null },
465+
.{ "window.addEventListener('scroll', () => {scroll = true});", null },
466+
.{ "document.addEventListener('scrollend', () => {scrollend = true});", null },
467+
.{ "window.scrollTo(0)", null },
468+
.{ "scroll", "true" },
469+
.{ "scrollend", "true" },
470+
}, .{});
440471
}

0 commit comments

Comments
 (0)