Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: reprocess connected stylesheet attributes consistently

Aaron Wong3ec7a0c7a0ae
diff --git a/src/browser/tests/css/stylesheet_event_order.html b/src/browser/tests/css/stylesheet_event_order.html
index 021362bdc..2c95b57e3 100644
--- a/src/browser/tests/css/stylesheet_event_order.html
+++ b/src/browser/tests/css/stylesheet_event_order.html
@@ -32,6 +32,32 @@
})();
</script>
+<script id="reflected_and_generic_attribute_updates_keep_completion">
+(async () => {
+ const state = await testing.async();
+ const observed = [];
+ for (const update of [
+ link => { link.rel = 'stylesheet'; },
+ link => { link.setAttribute('href', '/styles/visibility.css'); },
+ link => { link.setAttribute('href', '/styles/404.css'); },
+ ]) {
+ const link = document.createElement('link');
+ link.rel = 'stylesheet';
+ link.href = '/styles/visibility.css';
+ document.head.appendChild(link);
+ update(link);
+ const events = [];
+ link.addEventListener('load', () => events.push('load'));
+ link.addEventListener('error', () => events.push('error'));
+ await new Promise(resolve => setTimeout(resolve, 200));
+ observed.push(events);
+ link.remove();
+ }
+ state.resolve();
+ await state.done(() => testing.expectEqual([['load'], ['load'], ['error']], observed));
+})();
+</script>
+
<script id="stylesheet_error_after_append">
(async () => {
const state = await testing.async();
@@ -66,7 +92,7 @@
const observed = [];
for (const [first, second] of [
['/styles/404.css', '/styles/visibility.css'],
- ['/styles/visibility.css?first', '/styles/visibility.css?second'],
+ ['/styles/visibility.css', '/styles/visibility2.css'],
['/styles/visibility.css', '/styles/404.css'],
]) {
const link = document.createElement('link');
diff --git a/src/browser/webapi/element/html/Link.zig b/src/browser/webapi/element/html/Link.zig
index 79f63deca..7c18459f2 100644
--- a/src/browser/webapi/element/html/Link.zig
+++ b/src/browser/webapi/element/html/Link.zig
@@ -40,6 +40,7 @@ _proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,
// Mirrors `Style._sheet`.
_sheet: ?*@import("../../css/CSSStyleSheet.zig") = null,
_stylesheet_generation: u32 = 0,
+_processed_rel: []const u8 = "",
pub fn asElement(self: *Link) *Element {
return Factory.protoOf(self).asElement();
@@ -63,10 +64,6 @@ pub fn getHref(self: *Link, frame: *Frame) ![]const u8 {
pub fn setHref(self: *Link, value: []const u8, frame: *Frame) !void {
const element = self.asElement();
try element.setAttributeSafe(comptime .wrap("href"), .wrap(value), frame);
-
- if (element.asNode().isConnected()) {
- try self.linkAddedCallback(frame);
- }
}
fn getRel(self: *Link) []const u8 {
@@ -120,6 +117,7 @@ pub fn linkAddedCallback(self: *Link, frame: *Frame) !void {
}
const element = self.asElement();
+ self._processed_rel = element.getAttributeInterned("rel") orelse "";
// A document without a browsing context (DOMParser et al.) loads nothing.
if (element.getDocument(frame)._frame == null) {
@@ -197,9 +195,14 @@ pub const JsApi = struct {
// links in HTML go through `linkAddedCallback` at element-create time,
// with attributes already populated by `populateElementAttributes`.
pub const Build = struct {
- pub fn attributeChange(element: *Element, name: String, _: String, _: *Frame) !void {
- if (name.eql(comptime .wrap("href")) or name.eql(comptime .wrap("rel"))) {
- element.as(Link)._stylesheet_generation +%= 1;
+ pub fn attributeChange(element: *Element, name: String, value: String, frame: *Frame) !void {
+ const link = element.as(Link);
+ 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);
}
}