Skip to content

Lightpanda: the headless browser designed for AI and automation

fix foster parenting

Karl Seguin24c907402282
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index a8773c0ca..4e8101122 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -2938,12 +2938,12 @@ fn _insertNodeRelative(self: *Frame, comptime from_parser: bool, parent: *Node,
child._parent = parent;
Element.Html.Select.childInserted(parent, child);
- if (comptime from_parser) {
- if (child.is(Element)) |el| {
- _ = try Element.Build.call(el, "parserInserted", .{ el, self });
- }
+ if (child.is(Element.Html.Image)) |img| {
+ // noop if it didn't actually change
+ try img.sourceChanged(self);
+ } else {
+ try Element.Html.Picture.childInserted(parent, child, self);
}
- try Element.Html.Picture.childInserted(parent, child, self);
// Update live ranges for insertion (DOM spec insert step 6).
// For .before/.after the child was inserted at a specific position;
diff --git a/src/browser/frame/node_factory.zig b/src/browser/frame/node_factory.zig
index a91752140..edd716983 100644
--- a/src/browser/frame/node_factory.zig
+++ b/src/browser/frame/node_factory.zig
@@ -1070,9 +1070,8 @@ fn createHtmlElementT(document: *const Node.Document, comptime E: type, namespac
const node = element.asNode();
if (@hasDecl(E, "Build") and @hasDecl(E.Build, "created")) {
- if (comptime @TypeOf(attribute_iterator) == Parser.AttributeIterator and @hasDecl(E.Build, "parserInserted")) {
- // When from the parer, prefer parserInserted if present, as then
- // the parent is available
+ if (comptime @TypeOf(attribute_iterator) == Parser.AttributeIterator and @hasDecl(E.Build, "parser_created_on_insert")) {
+ // The element wants its parent, it'll do this work when inserted.
return node;
}
@call(.auto, @field(E.Build, "created"), .{ node, frame }) catch |err| {
diff --git a/src/browser/tests/element/html/image_srcset.html b/src/browser/tests/element/html/image_srcset.html
index fa226252b..e7555a35b 100644
--- a/src/browser/tests/element/html/image_srcset.html
+++ b/src/browser/tests/element/html/image_srcset.html
@@ -11,6 +11,21 @@
<img id="parsed-srcset" srcset="/one.png 1x, /two.png 2x">
+<script>
+ // load doesn't bubble, but it can be captured before the elements exist
+ const fostered_loads = {};
+ document.addEventListener("load", (e) => {
+ fostered_loads[e.target.id] = (fostered_loads[e.target.id] ?? 0) + 1;
+ }, true);
+</script>
+
+<!-- foster parented: inserted before the table, not through the parser's append -->
+<table><img id="fostered" src="/fostered.png"></table>
+<picture>
+ <source srcset="/fostered-source.png">
+ <table><img id="fostered-picture" src="/fostered-src.png"></table>
+</picture>
+
<script id="parsed">
{
testing.expectEqual(testing.ORIGIN + "/from-source.png", document.getElementById("parsed-img").currentSrc);
@@ -19,6 +34,19 @@
}
</script>
+<script id="fostered" type=module>
+ const state = await testing.async();
+ await new Promise(resolve => setTimeout(resolve, 20));
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("TABLE", document.getElementById("fostered").nextElementSibling.tagName);
+ testing.expectEqual(1, fostered_loads["fostered"]);
+ testing.expectEqual(1, fostered_loads["fostered-picture"]);
+ testing.expectEqual(testing.ORIGIN + "/fostered-source.png", document.getElementById("fostered-picture").currentSrc);
+ });
+</script>
+
<script id="img_srcset" type=module>
// Browsers run the source selection, which updates currentSrc, after the
// current script. Lightpanda does it synchronously.
@@ -57,7 +85,7 @@
await check(testing.ORIGIN + "/a,b.png");
// invalid descriptors drop the candidate
- img.srcset = "/bad.png 1q, /bad2.png 1x 2x, /good.png";
+ img.srcset = "/bad.png 1q, /bad2.png 1x 2x, /bad3.png 1_0w, /bad4.png +10w, /good.png";
await check(testing.ORIGIN + "/good.png");
// width descriptors are measured against the viewport width
diff --git a/src/browser/webapi/element/html/Image.zig b/src/browser/webapi/element/html/Image.zig
index 86e0f8084..946428f37 100644
--- a/src/browser/webapi/element/html/Image.zig
+++ b/src/browser/webapi/element/html/Image.zig
@@ -156,9 +156,10 @@ fn imageAddedCallback(self: *Image, frame: *Frame) !void {
};
}
-/// A <picture> mutation: only restart the load if it changed the selection.
+/// An insertion or <picture> mutation: only restart the load if it changed the
+/// selection. A parser-created image has selected nothing until its insertion.
pub fn sourceChanged(self: *Image, frame: *Frame) !void {
- // Yes, we can get collisions, but the even if it happens, the impact is low.
+ // Yes, we can get collisions, but even if it happens, the impact is low.
if (std.hash.Wyhash.hash(0, self.selectSource(frame)) == self._selected_hash) {
return;
}
@@ -196,7 +197,8 @@ fn selectSource(self: *Image, frame: *Frame) []const u8 {
}
// Picks the candidate with the smallest density that is still >= 1, falling
-// back to the densest one.
+// back to the densest one. `sizes` isn't parsed: a width descriptor is always
+// measured against the full viewport width (the 100vw default).
fn pickCandidate(srcset: []const u8, src: []const u8, viewport_width: u32) ?[]const u8 {
var best: ?Candidate = null;
var densest: ?Candidate = null;
@@ -224,9 +226,8 @@ fn pickCandidate(srcset: []const u8, src: []const u8, viewport_width: u32) ?[]co
}
if (src.len > 0 and !has_1x and !has_width) {
- if (best == null or best.?.density > 1) {
- return src;
- }
+ // src is the 1x candidate, nothing in srcset can beat it.
+ return src;
}
const chosen = best orelse densest orelse return null;
return chosen.url;
@@ -315,8 +316,8 @@ const SrcsetIterator = struct {
if (density != null or width != null) {
return null;
}
- const w = std.fmt.parseInt(u32, value, 10) catch return null;
- if (w == 0 or value[0] == '+') {
+ const w = parseNonNegativeInt(value) orelse return null;
+ if (w == 0) {
return null;
}
width = w;
@@ -325,7 +326,7 @@ const SrcsetIterator = struct {
if (density != null or has_height) {
return null;
}
- _ = std.fmt.parseInt(u32, value, 10) catch return null;
+ _ = parseNonNegativeInt(value) orelse return null;
has_height = true;
},
else => return null,
@@ -341,6 +342,16 @@ const SrcsetIterator = struct {
return .{ .density = density orelse 1 };
}
+ // parseInt also accepts a sign and '_' separators.
+ fn parseNonNegativeInt(value: []const u8) ?u32 {
+ for (value) |c| {
+ if (!std.ascii.isDigit(c)) {
+ return null;
+ }
+ }
+ return std.fmt.parseInt(u32, value, 10) catch null;
+ }
+
// parseFloat also accepts "inf", "nan", hex and a leading '+'.
fn isFloatSyntax(value: []const u8) bool {
if (value.len == 0 or value[0] == '+') {
@@ -396,17 +407,15 @@ pub const JsApi = struct {
};
pub const Build = struct {
+ // The parser's images wait until they're inserted (sourceChanged), so a
+ // <picture> parent's <source> elements take part in the selection.
+ pub const parser_created_on_insert = true;
+
pub fn created(node: *Node, frame: *Frame) !void {
const self = node.as(Image);
return self.imageAddedCallback(frame);
}
- // The parser's images wait until they're linked, so a <picture> parent's
- // <source> elements take part in the selection.
- pub fn parserInserted(element: *Element, frame: *Frame) !void {
- return element.as(Image).imageAddedCallback(frame);
- }
-
pub fn attributeChange(element: *Element, name: String, _: String, frame: *Frame) !void {
if (!isSourceAttribute(name)) {
return;
diff --git a/src/browser/webapi/element/html/Picture.zig b/src/browser/webapi/element/html/Picture.zig
index c5d4f22e4..7ca25daeb 100644
--- a/src/browser/webapi/element/html/Picture.zig
+++ b/src/browser/webapi/element/html/Picture.zig
@@ -45,14 +45,11 @@ pub fn asNode(self: *Picture) *Node {
return self.asElement().asNode();
}
-// An <img> entering or leaving the picture, or a <source> changing before one.
+// A <source> entering the picture. Frame handles an inserted <img> itself.
pub fn childInserted(parent: *Node, child: *Node, frame: *Frame) !void {
if (parent.is(Picture) == null) {
return;
}
- if (child.is(Image)) |img| {
- return img.sourceChanged(frame);
- }
if (child.is(Source) != null) {
return imagesFrom(child.nextSibling(), frame);
}