Lightpanda: the headless browser designed for AI and automation
image: drop queued load and error events for superseded sources
Scott Taylor79f46875a457
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex f1f864735..5ab60d69c 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -2386,6 +2386,8 @@ pub fn trackFileList(self: *Frame, file_list: *FileList) !void {pub const QueuedEvent = struct {kind: Kind,element: *Element.Html,+ // An <img> event belongs to one source: dropped if the source changed.+ image_generation: ?u32 = null,pub const Kind = enum { load, @"error" };};@@ -2395,7 +2397,15 @@ pub fn queueLoad(self: *Frame, html: *Element.Html) !void {}pub fn queueElementEvent(self: *Frame, element: *Element.Html, kind: QueuedEvent.Kind) !void {- try self._queued_events.append(self.arena, .{ .element = element, .kind = kind });+ try self.queueEvent(.{ .element = element, .kind = kind });+}++pub fn queueImageEvent(self: *Frame, image: *Element.Html.Image, kind: QueuedEvent.Kind) !void {+ try self.queueEvent(.{ .element = Factory.protoOf(image), .kind = kind, .image_generation = image._generation });+}++fn queueEvent(self: *Frame, event: QueuedEvent) !void {+ try self._queued_events.append(self.arena, event);if (self._queued_events.items.len == 1) {try self.js.scheduler.add(self, struct {fn cleanup(ctx: *anyopaque) !?u32 {@@ -2616,6 +2626,12 @@ fn dispatchQueuedEvents(self: *Frame) !void {for (to_process.items) |queued| {const html_element = queued.element;const element = html_element.asElement();+ if (queued.image_generation) |generation| {+ const image = html_element.is(Element.Html.Image) orelse unreachable;+ if (image._generation != generation) {+ continue;+ }+ }switch (queued.kind) {// hasAttributeFunction only sees handlers compiled via property// access; a parsed `onload="..."` attribute is compiled lazily atdiff --git a/src/browser/frame/resource_load.zig b/src/browser/frame/resource_load.zigindex 04af69ab5..153f1f935 100644--- a/src/browser/frame/resource_load.zig+++ b/src/browser/frame/resource_load.zig@@ -43,7 +43,7 @@ pub fn image(frame: *Frame, img: *Element.Html.Image, src: []const u8) !void {// makes. They still get the synthetic load the no-fetch path would have// given them, so the two modes agree.if (frame._parse_mode == .fragment) {- return frame.queueLoad(Factory.protoOf(img));+ return frame.queueImageEvent(img, .load);}const arena = try session.getArena(.small, "resource_load.image");@@ -198,7 +198,7 @@ const ImageLoad = struct {// puts us inside html5ever parsing, and an event handler is free// to navigate. Queueing also puts these events ahead of window// load, which is where the spec wants them.- frame.queueElementEvent(Factory.protoOf(img), kind) catch |err| {+ frame.queueImageEvent(img, kind) catch |err| {log.warn(.frame, "image queue event", .{ .err = err, .kind = kind });};}diff --git a/src/browser/tests/element/html/image.html b/src/browser/tests/element/html/image.htmlindex 368de087b..91d770991 100644--- a/src/browser/tests/element/html/image.html+++ b/src/browser/tests/element/html/image.html@@ -163,6 +163,23 @@}</script>+<script id="superseded-src-fires-one-load">+{+ // Only the source the img ends up with fires load, as when a newer src+ // aborts the fetch of an older one.+ const img = document.createElement("img");+ const events = [];+ img.addEventListener("load", () => events.push(img.getAttribute("src")));+ document.body.appendChild(img);+ img.src = "a.png";+ img.removeAttribute("src");+ img.src = "b.png";+ img.src = "c.png";++ testing.onload(() => testing.expectEqual("c.png", events.join(",")));+}+</script>+<script id=url_encode>{let img = document.createElement('img');diff --git a/src/browser/webapi/element/html/Image.zig b/src/browser/webapi/element/html/Image.zigindex 62188c35b..a568bc65e 100644--- a/src/browser/webapi/element/html/Image.zig+++ b/src/browser/webapi/element/html/Image.zig@@ -147,12 +147,12 @@ fn imageAddedCallback(self: *Image, frame: *Frame) !void {// If image loading not desired, we just do fake "load" event.if (frame._session.load_resources.image == false) {- return frame.queueLoad(Factory.protoOf(self));+ return frame.queueImageEvent(self, .load);}Frame.resource_load.image(frame, self, src) catch |err| {log.debug(.http, "image fetch", .{ .err = err, .src = src });- return frame.queueElementEvent(Factory.protoOf(self), .@"error");+ return frame.queueImageEvent(self, .@"error");};}