Lightpanda: the headless browser designed for AI and automation
Move logic out of JsAPI
Karl Seguin872580a70851
diff --git a/src/browser/webapi/AbortSignal.zig b/src/browser/webapi/AbortSignal.zigindex 9b679cbb2..5ad2f9758 100644--- a/src/browser/webapi/AbortSignal.zig+++ b/src/browser/webapi/AbortSignal.zig@@ -168,9 +168,9 @@ pub fn createAborted(reason_: ?js.Value, exec: *const Execution) !*AbortSignal {}pub fn createAny(signals_value: js.Value, exec: *const Execution) !*AbortSignal {- // A js.Value parameter (not a slice) so the argument is required — the- // bridge would treat a trailing slice as variadic and default it to- // empty, but AbortSignal.any() must throw when called with no argument.+ // The parameter isn't optional. If we declared it as a slice directy, the+ // bridge would treat it as a variadic and map it to empty rather than throwing+ // a TypeError as it should.const signals = try signals_value.toZig([]const *AbortSignal);const result = try init(exec);@@ -284,9 +284,6 @@ pub const JsApi = struct {pub const Prototype = EventTarget;- // Per spec AbortSignal has no constructor; instances are created via the- // static methods and AbortController. (AbortSignal.init stays for- // internal use.)pub const aborted = bridge.accessor(AbortSignal.getAborted, null, .{});pub const reason = bridge.accessor(AbortSignal.getReason, null, .{});pub const onabort = bridge.accessor(AbortSignal.getOnAbort, AbortSignal.setOnAbort, .{});diff --git a/src/browser/webapi/element/html/Area.zig b/src/browser/webapi/element/html/Area.zigindex 4ce875fb4..02b01a708 100644--- a/src/browser/webapi/element/html/Area.zig+++ b/src/browser/webapi/element/html/Area.zig@@ -16,10 +16,12 @@const std = @import("std");const js = @import("../../../js/js.zig");const Frame = @import("../../../Frame.zig");-const URL = @import("../../../URL.zig");+const Node = @import("../../Node.zig");const Element = @import("../../Element.zig");+const DOMTokenList = @import("../../collections.zig").DOMTokenList;+const HtmlElement = @import("../Html.zig");const Area = @This();@@ -248,6 +250,15 @@ pub fn setProtocol(self: *Area, value: []const u8, frame: *Frame) !void {try setHref(self, new_href, frame);}+pub fn getRelList(self: *Area, frame: *Frame) !?*DOMTokenList {+ const element = self.asElement();+ // relList is only valid for HTML <area> elements+ if (element._namespace != .html) {+ return null;+ }+ return element.getRelList(frame);+}+fn getResolvedHref(self: *Area, frame: *Frame) !?[:0]const u8 {const href = self.asElement().getAttributeSafe(comptime .wrap("href")) orelse return null;if (href.len == 0) {@@ -289,16 +300,7 @@ pub const JsApi = struct {pub const rel = bridge.accessor(Area.getRel, Area.setRel, .{ .ce_reactions = true });pub const referrerPolicy = bridge.accessor(Area.getReferrerPolicy, Area.setReferrerPolicy, .{ .ce_reactions = true });pub const toString = bridge.function(Area.getHref, .{});- pub const relList = bridge.accessor(struct {- fn wrap(self: *Area, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {- const element = self.asElement();- // relList is only valid for HTML <area> elements- if (element._namespace != .html) {- return null;- }- return element.getRelList(frame);- }- }.wrap, null, .{ .null_as_undefined = true });+ pub const relList = bridge.accessor(Area.getRelList, null, .{ .null_as_undefined = true });};const testing = @import("../../../../testing.zig");diff --git a/src/browser/webapi/element/html/IFrame.zig b/src/browser/webapi/element/html/IFrame.zigindex 5e2153b76..e2e139049 100644--- a/src/browser/webapi/element/html/IFrame.zig+++ b/src/browser/webapi/element/html/IFrame.zig@@ -20,10 +20,13 @@ const std = @import("std");const js = @import("../../../js/js.zig");const Frame = @import("../../../Frame.zig");-const Window = @import("../../Window.zig");-const Document = @import("../../Document.zig");+const Node = @import("../../Node.zig");+const Window = @import("../../Window.zig");const Element = @import("../../Element.zig");+const Document = @import("../../Document.zig");+const DOMTokenList = @import("../../collections.zig").DOMTokenList;+const HtmlElement = @import("../Html.zig");const IFrame = @This();@@ -80,6 +83,14 @@ pub fn setName(self: *IFrame, value: []const u8, frame: *Frame) !void {try self.asElement().setAttributeSafe(comptime .wrap("name"), .wrap(value), frame);}+pub fn getSandbox(self: *IFrame, frame: *Frame) !?*DOMTokenList {+ const element = self.asElement();+ if (element._namespace != .html) {+ return null;+ }+ return element.getTokenList(.sandbox, frame);+}+pub const JsApi = struct {pub const bridge = js.Bridge(IFrame);@@ -93,15 +104,7 @@ pub const JsApi = struct {pub const name = bridge.accessor(IFrame.getName, IFrame.setName, .{ .ce_reactions = true });pub const contentWindow = bridge.accessor(IFrame.getContentWindow, null, .{});pub const contentDocument = bridge.accessor(IFrame.getContentDocument, null, .{});- pub const sandbox = bridge.accessor(_getSandbox, null, .{ .null_as_undefined = true });-- fn _getSandbox(self: *IFrame, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {- const element = self.asElement();- if (element._namespace != .html) {- return null;- }- return element.getTokenList(.sandbox, frame);- }+ pub const sandbox = bridge.accessor(IFrame.getSandbox, null, .{ .null_as_undefined = true });};pub const Build = struct {diff --git a/src/browser/webapi/element/html/Link.zig b/src/browser/webapi/element/html/Link.zigindex 06eee926d..2c06d3d8b 100644--- a/src/browser/webapi/element/html/Link.zig+++ b/src/browser/webapi/element/html/Link.zig@@ -22,6 +22,8 @@ const Frame = @import("../../../Frame.zig");const Node = @import("../../Node.zig");const Element = @import("../../Element.zig");+const DOMTokenList = @import("../../collections.zig").DOMTokenList;+const HtmlElement = @import("../Html.zig");const Link = @This();@@ -187,6 +189,23 @@ pub fn setTarget(self: *Link, value: []const u8, frame: *Frame) !void {return self.asElement().setAttributeSafe(comptime .wrap("target"), .wrap(value), frame);}+pub fn getSizes(self: *Link, frame: *Frame) !?*DOMTokenList {+ const element = self.asElement();+ if (element._namespace != .html) {+ return null;+ }+ return element.getTokenList(.sizes, frame);+}++pub fn getRelList(self: *Link, frame: *Frame) !?*DOMTokenList {+ const element = self.asElement();+ // relList is only valid for HTML <link> elements, not SVG or MathML+ if (element._namespace != .html) {+ return null;+ }+ return element.getRelList(frame);+}+pub fn linkAddedCallback(self: *Link, frame: *Frame) !void {// if we're planning on navigating to another frame, don't trigger load event.if (frame.isGoingAway()) {@@ -255,25 +274,8 @@ pub const JsApi = struct {pub const @"type" = bridge.accessor(Link.getType, Link.setType, .{ .ce_reactions = true });pub const rev = bridge.accessor(Link.getRev, Link.setRev, .{ .ce_reactions = true });pub const target = bridge.accessor(Link.getTarget, Link.setTarget, .{ .ce_reactions = true });- pub const relList = bridge.accessor(_getRelList, null, .{ .null_as_undefined = true });- pub const sizes = bridge.accessor(_getSizes, null, .{ .null_as_undefined = true });-- fn _getSizes(self: *Link, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {- const element = self.asElement();- if (element._namespace != .html) {- return null;- }- return element.getTokenList(.sizes, frame);- }-- fn _getRelList(self: *Link, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {- const element = self.asElement();- // relList is only valid for HTML <link> elements, not SVG or MathML- if (element._namespace != .html) {- return null;- }- return element.getRelList(frame);- }+ pub const relList = bridge.accessor(Link.getRelList, null, .{ .null_as_undefined = true });+ pub const sizes = bridge.accessor(Link.getSizes, null, .{ .null_as_undefined = true });};// Parser-created <link> elements are void (no closing tag) so they neverdiff --git a/src/browser/webapi/element/html/Output.zig b/src/browser/webapi/element/html/Output.zigindex 96240077e..449f84ec6 100644--- a/src/browser/webapi/element/html/Output.zig+++ b/src/browser/webapi/element/html/Output.zig@@ -1,7 +1,28 @@+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)+//+// Francis Bouvier <[email protected]>+// Pierre Tachoire <[email protected]>+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.+const js = @import("../../../js/js.zig");const Frame = @import("../../../Frame.zig");+const Node = @import("../../Node.zig");const Element = @import("../../Element.zig");+const DOMTokenList = @import("../../collections.zig").DOMTokenList;+const HtmlElement = @import("../Html.zig");const Output = @This();@@ -19,6 +40,14 @@ pub fn getLabels(self: *Output, frame: *Frame) !js.Array {return @import("Label.zig").getControlLabels(self.asElement(), frame);}+pub fn getHtmlFor(self: *Output, frame: *Frame) !?*DOMTokenList {+ const element = self.asElement();+ if (element._namespace != .html) {+ return null;+ }+ return element.getTokenList(.@"for", frame);+}+pub const JsApi = struct {pub const bridge = js.Bridge(Output);@@ -29,13 +58,5 @@ pub const JsApi = struct {};pub const labels = bridge.accessor(Output.getLabels, null, .{});- pub const htmlFor = bridge.accessor(_getHtmlFor, null, .{ .null_as_undefined = true });-- fn _getHtmlFor(self: *Output, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {- const element = self.asElement();- if (element._namespace != .html) {- return null;- }- return element.getTokenList(.@"for", frame);- }+ pub const htmlFor = bridge.accessor(Output.getHtmlFor, null, .{ .null_as_undefined = true });};