Skip to content

Lightpanda: the headless browser designed for AI and automation

`Image`: fix generation update order

Halil Durakf07f818d2f32
diff --git a/src/browser/webapi/element/html/Image.zig b/src/browser/webapi/element/html/Image.zig
index 83e096009..205af94c9 100644
--- a/src/browser/webapi/element/html/Image.zig
+++ b/src/browser/webapi/element/html/Image.zig
@@ -1,6 +1,5 @@
const lp = @import("lightpanda");
const std = @import("std");
-const log = lp.log;
const js = @import("../../../js/js.zig");
const Factory = @import("../../../Factory.zig");
const Frame = @import("../../../Frame.zig");
@@ -8,18 +7,15 @@ const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig");
const HtmlElement = @import("../Html.zig");
+const log = lp.log;
const String = lp.String;
const Image = @This();
pub const Proto = HtmlElement;
-// Bumped on every src assignment. An in-flight fetch carries the value it
-// was issued under; when they no longer match, a newer src has taken over
-// and the older response must not fire events on this element.
_generation: u32 = 0,
-// Per spec, false only while a fetch is in flight. Without
-// `--load-resources image` there is never a fetch, so it never leaves true.
+// Per spec, false only while a fetch is in flight.
_complete: bool = true,
_proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,
@@ -59,11 +55,6 @@ pub fn getSrc(self: *const Image, frame: *Frame) ![]const u8 {
}
pub fn setSrc(self: *Image, value: []const u8, frame: *Frame) !void {
- // Setting the attribute is enough: `_put` dispatches to
- // `Build.attributeChange`, which starts the load. Calling
- // `imageAddedCallback` here too would issue the request twice (the first
- // one immediately superseded, so it costs a request and shows nothing).
- // Connectivity still isn't checked — a detached `new Image()` loads.
return self.asElement().setAttributeSafe(comptime .wrap("src"), .wrap(value), frame);
}
@@ -88,17 +79,12 @@ pub fn getNaturalHeight(_: *const Image) u32 {
}
pub fn getComplete(self: *const Image) bool {
- // Per spec, complete is true when: no src/srcset, src is empty, the
- // image is fully available, or the image is broken with no pending
- // request. Every one of those is "no fetch in flight", which is exactly
- // what `_complete` tracks. Without `--load-resources image` nothing ever
- // clears it.
return self._complete;
}
/// The one funnel for "this element's src became current": parser-created
-/// images (`Build.created`), `img.src = ...` (`setSrc`) and
-/// `setAttribute("src", ...)` (`Build.attributeChange`) all land here.
+/// images, `img.src = ...` and `setAttribute`/`removeAttribute("src")` all
+/// land here.
pub fn imageAddedCallback(self: *Image, frame: *Frame) !void {
// if we're planning on navigating to another frame, don't trigger a load event
// or start fetching a resource.
@@ -106,6 +92,9 @@ pub fn imageAddedCallback(self: *Image, frame: *Frame) !void {
return;
}
+ self._generation +%= 1;
+ self._complete = true;
+
const element = self.asElement();
// Exit if src not set.
const src = element.getAttributeSafe(comptime .wrap("src")) orelse return;
@@ -116,21 +105,8 @@ pub fn imageAddedCallback(self: *Image, frame: *Frame) !void {
return frame.queueLoad(Factory.protoOf(self));
}
- // A fetch still in flight for the previous src is stale as of right now,
- // and this is the only place that knows it. Wrapping is fine: colliding
- // needs a request to still be in flight 2^32 src assignments later, which
- // outlives any transfer timeout by several orders of magnitude.
- self._generation +%= 1;
-
- // Deliberately not propagated. `newRequest` is declared `anyerror`, and
- // this runs on DOM mutation paths with narrow declared error sets
- // (`Node.cloneNode`'s `CloneError`, for one). Failing to even issue the
- // request is the same observable outcome as the request failing, so
- // report it the same way — `loadImage` has already unwound `_complete`
- // and the pending-load slot by the time it returns an error.
- frame.loadImage(self, src) catch |err| {
+ Frame.resource_load.image(frame, self, src) catch |err| {
log.warn(.http, "image fetch", .{ .err = err, .src = src });
- // On failure, queue "error" event.
return frame.queueElementEvent(Factory.protoOf(self), .@"error");
};
}