Lightpanda: the headless browser designed for AI and automation
Re-use existing QueueEvent.generation
Karl Seguinacebd13c8937
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 0f90f4a00..f829289bc 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -2625,8 +2625,7 @@ pub fn trackFileList(self: *Frame, file_list: *FileList) !void {pub const QueuedEvent = struct {kind: Kind,element: *Element.Html,- generation: u32 = 0, // Image request generation- stylesheet_generation: ?u32 = null,+ generation: u32 = 0, // Image/Link request generationpub const Kind = enum { load, @"error" };};@@ -2636,6 +2635,9 @@ pub fn queueLoad(self: *Frame, html: *Element.Html) !void {}pub fn queueElementEvent(self: *Frame, element: *Element.Html, kind: QueuedEvent.Kind) !void {+ if (element.is(Element.Html.Link)) |link| {+ return self.queueLinkEvent(link, kind, link._generation);+ }try self.queueEvent(.{ .element = element, .kind = kind });}@@ -2643,8 +2645,8 @@ pub fn queueImageEvent(self: *Frame, image: *Element.Html.Image, kind: QueuedEvetry self.queueEvent(.{ .element = Factory.protoOf(image), .kind = kind, .generation = image._generation });}-fn queueStylesheetEvent(self: *Frame, link: *Element.Html.Link, kind: QueuedEvent.Kind, generation: u32) !void {- try self.queueEvent(.{ .element = Factory.protoOf(link), .kind = kind, .stylesheet_generation = generation });+fn queueLinkEvent(self: *Frame, link: *Element.Html.Link, kind: QueuedEvent.Kind, generation: u32) !void {+ try self.queueEvent(.{ .element = Factory.protoOf(link), .kind = kind, .generation = generation });}fn queueEvent(self: *Frame, event: QueuedEvent) !void {@@ -2748,8 +2750,9 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []coif (self._parse_mode == .fragment) {return;}- link._stylesheet_generation +%= 1;- const generation = link._stylesheet_generation;+ // Captured up front: if the link is reprocessed while the sync fetch is+ // in flight, this pass's outcome is stale and must not be delivered.+ const generation = link._generation;const element = link.asElement();// HttpClient will take out a larger arena for the body, if necessary@@ -2758,7 +2761,7 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []coconst resolved = URL.resolve(arena.allocator(), self.base(), href, .{ .encoding = self.charset }) catch |err| {log.debug(.http, "external stylesheet resolve", .{ .err = err, .href = href });- try self.queueStylesheetEvent(link, .@"error", generation);+ try self.queueLinkEvent(link, .@"error", generation);return;};@@ -2779,7 +2782,7 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []co.shutdown_callback = HttpClient.noopShutdown, // syncRequest installs its own}, &self._http_owner) catch |err| {log.debug(.http, "external stylesheet fetch", .{ .err = err, .url = resolved });- return self.queueStylesheetEvent(link, .@"error", generation);+ return self.queueLinkEvent(link, .@"error", generation);};{errdefer transfer.deinit();@@ -2801,13 +2804,13 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []covar response = transfer.submitSync(.{}) catch |err| {log.debug(.http, "external stylesheet fetch", .{ .err = err, .url = resolved });- return self.queueStylesheetEvent(link, .@"error", generation);+ return self.queueLinkEvent(link, .@"error", generation);};defer response.deinit();if (response.status < 200 or response.status >= 300) {log.info(.http, "external stylesheet status", .{ .status = response.status, .url = resolved });- return self.queueStylesheetEvent(link, .@"error", generation);+ return self.queueLinkEvent(link, .@"error", generation);}if (response.body.items.len > MAX_STYLESHEET_BYTES) {@@ -2816,7 +2819,7 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []co.max = MAX_STYLESHEET_BYTES,.url = resolved,});- return self.queueStylesheetEvent(link, .@"error", generation);+ return self.queueLinkEvent(link, .@"error", generation);}// Reuse the cached sheet on re-fetch (href mutation on a connected@@ -2851,11 +2854,11 @@ pub fn loadExternalStylesheet(self: *Frame, link: *Element.Html.Link, href: []coconst text = try css_charset.decode(arena.allocator(), response.body.items, protocol_charset, self.charset);sheet.replaceSync(text, self) catch |err| {log.debug(.http, "external stylesheet parse", .{ .err = err, .url = resolved });- return self.queueStylesheetEvent(link, .@"error", generation);+ return self.queueLinkEvent(link, .@"error", generation);};sheet._href = try self.page_arena.dupe(u8, resolved);- try self.queueStylesheetEvent(link, .load, generation);+ try self.queueLinkEvent(link, .load, generation);}// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps@@ -2895,9 +2898,10 @@ fn dispatchQueuedEvents(self: *Frame) !void {}}- if (queued.stylesheet_generation) |generation| {- const link = html_element.asElement().as(Element.Html.Link);- if (link._stylesheet_generation != generation) continue;+ if (html_element.is(Element.Html.Link)) |link| {+ if (link._generation != queued.generation) {+ continue;+ }}const element = html_element.asElement();diff --git a/src/browser/tests/element/html/link.html b/src/browser/tests/element/html/link.htmlindex 2831400b9..9e7243655 100644--- a/src/browser/tests/element/html/link.html+++ b/src/browser/tests/element/html/link.html@@ -138,6 +138,33 @@}</script>+<script id="superseded-synthetic-load">+{+ // With fetching off, the synthetic load still follows the real one: a+ // removed link gets nothing, a re-pointed one gets a single load.+ const removed = [];+ const link1 = document.createElement('link');+ link1.rel = 'stylesheet';+ link1.href = '/nope';+ link1.addEventListener('load', () => removed.push('load'));+ document.head.appendChild(link1);+ link1.remove();++ const repointed = [];+ const link2 = document.createElement('link');+ link2.rel = 'stylesheet';+ link2.href = '/nope';+ link2.addEventListener('load', () => repointed.push('load'));+ document.head.appendChild(link2);+ link2.href = '/nope2';++ testing.onload(() => {+ testing.expectEqual([], removed);+ testing.expectEqual(['load'], repointed);+ });+}+</script>+<script id="ext_stylesheet_disabled_default">{// Flag defaults off — a <link rel=stylesheet> must NOT register a sheetdiff --git a/src/browser/webapi/element/html/Link.zig b/src/browser/webapi/element/html/Link.zigindex 7c18459f2..4ab309885 100644--- a/src/browser/webapi/element/html/Link.zig+++ b/src/browser/webapi/element/html/Link.zig@@ -39,7 +39,9 @@ _proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,// old rules are dropped instead of accumulating in `document.styleSheets`.// Mirrors `Style._sheet`._sheet: ?*@import("../../css/CSSStyleSheet.zig") = null,-_stylesheet_generation: u32 = 0,+// Bumped each time the link is (re)processed or disconnected. A queued+// load/error from an earlier pass no longer matches and is dropped.+_generation: u32 = 0,_processed_rel: []const u8 = "",pub fn asElement(self: *Link) *Element {@@ -110,7 +112,7 @@ fn setRelList(self: *Link, value: String, frame: *Frame) !void {}pub fn linkAddedCallback(self: *Link, frame: *Frame) !void {- self._stylesheet_generation +%= 1;+ self._generation +%= 1;// if we're planning on navigating to another frame, don't trigger load event.if (frame.isGoingAway()) {return;@@ -200,7 +202,6 @@ pub const Build = struct {const is_rel = name.eql(comptime .wrap("rel"));if (!is_rel and !name.eql(comptime .wrap("href"))) return;if (is_rel and std.mem.eql(u8, value.str(), link._processed_rel)) return;- link._stylesheet_generation +%= 1;if (element.asNode().isConnected()) {try link.linkAddedCallback(element.ownerFrame(frame) orelse return);}@@ -211,7 +212,7 @@ pub const Build = struct {}pub fn disconnected(element: *Element, _: *Frame) !void {- element.as(Link)._stylesheet_generation +%= 1;+ element.as(Link)._generation +%= 1;}pub fn created(node: *Node, frame: *Frame) !void {