Lightpanda: the headless browser designed for AI and automation
webapi: Add DOMPoint and DOMPointReadOnly
Karl Seguin6095d3a0e1d5
diff --git a/src/browser/js/Value.zig b/src/browser/js/Value.zigindex c9d4fb5e3..358561df8 100644--- a/src/browser/js/Value.zig+++ b/src/browser/js/Value.zig@@ -418,6 +418,8 @@ const cloneable_types = .{@import("../webapi/File.zig"),@import("../webapi/FileList.zig"),@import("../webapi/ImageData.zig"),+ @import("../webapi/DOMPointReadOnly.zig"),+ @import("../webapi/DOMPoint.zig"),};// Passed to a type's structuredSerialize hook to write its payload into thediff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zigindex 4a3adf1ac..0ea934e64 100644--- a/src/browser/js/bridge.zig+++ b/src/browser/js/bridge.zig@@ -822,6 +822,8 @@ pub const PageJsApis = flattenTypes(&.{@import("../webapi/DOMRect.zig"),@import("../webapi/DOMMatrixReadOnly.zig"),@import("../webapi/DOMMatrix.zig"),+ @import("../webapi/DOMPointReadOnly.zig"),+ @import("../webapi/DOMPoint.zig"),@import("../webapi/DOMParser.zig"),@import("../webapi/XMLSerializer.zig"),@import("../webapi/AbstractRange.zig"),@@ -1021,6 +1023,8 @@ pub const WorkerJsApis = flattenTypes(&.{@import("../webapi/DOMException.zig"),@import("../webapi/DOMMatrixReadOnly.zig"),@import("../webapi/DOMMatrix.zig"),+ @import("../webapi/DOMPointReadOnly.zig"),+ @import("../webapi/DOMPoint.zig"),@import("../webapi/net/URLSearchParams.zig"),@import("../webapi/encoding/TextEncoder.zig"),@import("../webapi/encoding/TextDecoder.zig"),diff --git a/src/browser/tests/dompoint.html b/src/browser/tests/dompoint.htmlnew file mode 100644index 000000000..e485cfa17--- /dev/null+++ b/src/browser/tests/dompoint.html@@ -0,0 +1,173 @@+<!DOCTYPE html>+<head>+ <title>DOMPoint Test</title>+ <script src="testing.js"></script>+</head>++<body>+</body>++<script id=default_construct>+ {+ const p = new DOMPoint();+ testing.expectEqual(0, p.x);+ testing.expectEqual(0, p.y);+ testing.expectEqual(0, p.z);+ testing.expectEqual(1, p.w);+ }+</script>++<script id=construct_with_args>+ {+ const p = new DOMPoint(1, 2, 3, 4);+ testing.expectEqual(1, p.x);+ testing.expectEqual(2, p.y);+ testing.expectEqual(3, p.z);+ testing.expectEqual(4, p.w);+ }+</script>++<script id=readonly_default>+ {+ const p = new DOMPointReadOnly(5, 6);+ testing.expectEqual(5, p.x);+ testing.expectEqual(6, p.y);+ testing.expectEqual(0, p.z);+ testing.expectEqual(1, p.w);+ }+</script>++<script id=mutable_setters>+ {+ const p = new DOMPoint(1, 1, 1, 1);+ p.x = 10;+ p.y = 20;+ p.z = 30;+ p.w = 40;+ testing.expectEqual(10, p.x);+ testing.expectEqual(20, p.y);+ testing.expectEqual(30, p.z);+ testing.expectEqual(40, p.w);+ }+</script>++<script id=readonly_is_not_writable>+ {+ const p = new DOMPointReadOnly(1, 2, 3, 4);+ // Non-strict assignment to an accessor without a setter is a silent no-op.+ p.x = 99;+ testing.expectEqual(1, p.x);+ }+</script>++<script id=prototype_chain>+ {+ const p = new DOMPoint(1, 2, 3, 4);+ testing.expectTrue(p instanceof DOMPoint);+ testing.expectTrue(p instanceof DOMPointReadOnly);++ const ro = new DOMPointReadOnly(1, 2, 3, 4);+ testing.expectTrue(ro instanceof DOMPointReadOnly);+ testing.expectFalse(ro instanceof DOMPoint);+ }+</script>++<script id=from_point>+ {+ const p = DOMPoint.fromPoint({ x: 7, y: 8, z: 9 });+ testing.expectTrue(p instanceof DOMPoint);+ testing.expectEqual(7, p.x);+ testing.expectEqual(8, p.y);+ testing.expectEqual(9, p.z);+ testing.expectEqual(1, p.w);++ const ro = DOMPointReadOnly.fromPoint({ x: 1, w: 5 });+ testing.expectTrue(ro instanceof DOMPointReadOnly);+ testing.expectFalse(ro instanceof DOMPoint);+ testing.expectEqual(1, ro.x);+ testing.expectEqual(5, ro.w);+ }+</script>++<script id=from_point_empty>+ {+ const p = DOMPoint.fromPoint();+ testing.expectEqual(0, p.x);+ testing.expectEqual(1, p.w);+ }+</script>++<script id=to_json>+ {+ const p = new DOMPoint(1, 2, 3, 4);+ const j = p.toJSON();+ testing.expectEqual(1, j.x);+ testing.expectEqual(2, j.y);+ testing.expectEqual(3, j.z);+ testing.expectEqual(4, j.w);+ }+</script>++<script id=matrix_transform_translate>+ {+ const p = new DOMPoint(1, 2, 0, 1);+ const t = p.matrixTransform(new DOMMatrix().translate(10, 20));+ testing.expectTrue(t instanceof DOMPoint);+ testing.expectEqual(11, t.x);+ testing.expectEqual(22, t.y);+ testing.expectEqual(0, t.z);+ testing.expectEqual(1, t.w);+ // Original is unchanged.+ testing.expectEqual(1, p.x);+ testing.expectEqual(2, p.y);+ }+</script>++<script id=matrix_transform_scale>+ {+ const p = new DOMPointReadOnly(2, 3, 4, 1);+ const t = p.matrixTransform(new DOMMatrix().scale(2, 3));+ testing.expectEqual(4, t.x);+ testing.expectEqual(9, t.y);+ }+</script>++<script id=matrix_transform_dict>+ {+ const p = new DOMPoint(1, 0, 0, 1);+ // matrix(a,b,c,d,e,f) = scaleX 2, translateX 5+ const t = p.matrixTransform({ a: 2, e: 5 });+ testing.expectEqual(7, t.x);+ }+</script>++<script id=structured_clone_readonly>+ {+ const p = new DOMPointReadOnly(1.5, 2.5, 3.5, 4.5);+ const c = structuredClone(p);+ testing.expectTrue(c instanceof DOMPointReadOnly);+ testing.expectFalse(c instanceof DOMPoint);+ testing.expectEqual(1.5, c.x);+ testing.expectEqual(2.5, c.y);+ testing.expectEqual(3.5, c.z);+ testing.expectEqual(4.5, c.w);+ testing.expectFalse(c === p);+ }+</script>++<script id=structured_clone_mutable>+ {+ const p = new DOMPoint(-1, -2, -3, -4);+ const c = structuredClone(p);+ testing.expectTrue(c instanceof DOMPoint);+ testing.expectTrue(c instanceof DOMPointReadOnly);+ testing.expectEqual(-1, c.x);+ testing.expectEqual(-2, c.y);+ testing.expectEqual(-3, c.z);+ testing.expectEqual(-4, c.w);+ // The clone is independent and writable.+ c.x = 100;+ testing.expectEqual(100, c.x);+ testing.expectEqual(-1, p.x);+ }+</script>diff --git a/src/browser/webapi/DOMMatrix.zig b/src/browser/webapi/DOMMatrix.zigindex 7e7c13846..d1661a090 100644--- a/src/browser/webapi/DOMMatrix.zig+++ b/src/browser/webapi/DOMMatrix.zig@@ -58,9 +58,12 @@ pub fn fromFloat64Array(array: js.TypedArray(f64), page: *Page) !*DOMMatrix {return create(parsed.m, parsed.is_2d, page);}-// The base already exposes read-only getters, but a redeclared accessor's-// getter must be typed to this (owner) struct, so we provide DOMMatrix-typed-// getters that read through `_proto`.+// DOMMatrix redeclares a/b/.../m44 as writable, so DOMMatrix.prototype needs+// its own read-write accessors, distinct from the read-only ones on+// DOMMatrixReadOnly.prototype. The setters are the point; each accessor bundles+// a getter too, so we pair them with DOMMatrix-typed getters that read through+// `_proto` (they could reuse the base getters — the receiver unwraps down the+// prototype chain either way — but keeping the pair symmetric reads cleaner).pub fn getA(self: *const DOMMatrix) f64 {return self._proto._m[0];diff --git a/src/browser/webapi/DOMPoint.zig b/src/browser/webapi/DOMPoint.zignew file mode 100644index 000000000..0df456604--- /dev/null+++ b/src/browser/webapi/DOMPoint.zig@@ -0,0 +1,108 @@+// 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 Page = @import("../Page.zig");+const RO = @import("DOMPointReadOnly.zig");++const DOMPoint = @This();++_proto: *RO,++pub fn init(x_: ?f64, y_: ?f64, z_: ?f64, w_: ?f64, exec: *const js.Execution) !*DOMPoint {+ return create(x_ orelse 0, y_ orelse 0, z_ orelse 0, w_ orelse 1, exec.page);+}++pub fn create(x: f64, y: f64, z: f64, w: f64, page: *Page) !*DOMPoint {+ const proto = try RO.createBare(x, y, z, w, page);+ errdefer proto.deinit(page);++ const self = try proto._arena.create(DOMPoint);+ self.* = .{ ._proto = proto };+ proto._type = .{ .mutable = self };+ return self;+}++pub fn fromPoint(other_: ?RO.DOMPointInit, page: *Page) !*DOMPoint {+ const other: RO.DOMPointInit = other_ orelse .{};+ return create(other.x, other.y, other.z, other.w, page);+}++pub fn structuredSerialize(self: *const DOMPoint, writer: *js.StructuredWriter) !void {+ try self._proto.structuredSerialize(writer);+}++pub fn structuredDeserialize(reader: *js.StructuredReader, page: *Page) !*DOMPoint {+ const proto = try RO.structuredDeserialize(reader, page);+ errdefer proto.deinit(page);++ const self = try proto._arena.create(DOMPoint);+ self.* = .{ ._proto = proto };+ proto._type = .{ .mutable = self };+ return self;+}++// DOMPoint redeclares x/y/z/w as writable (`inherit attribute` in the IDL), so+// DOMPoint.prototype needs its own read-write accessors, distinct from the+// read-only ones on DOMPointReadOnly.prototype. The setters are the point; each+// accessor bundles a getter too, so we pair them with DOMPoint-typed getters+// that read through `_proto` (they could reuse the base getters — the receiver+// unwraps down the prototype chain either way — but keeping the pair symmetric+// mirrors DOMMatrix).+pub fn getX(self: *const DOMPoint) f64 {+ return self._proto._x;+}+pub fn getY(self: *const DOMPoint) f64 {+ return self._proto._y;+}+pub fn getZ(self: *const DOMPoint) f64 {+ return self._proto._z;+}+pub fn getW(self: *const DOMPoint) f64 {+ return self._proto._w;+}++pub fn setX(self: *DOMPoint, v: f64) void {+ self._proto._x = v;+}+pub fn setY(self: *DOMPoint, v: f64) void {+ self._proto._y = v;+}+pub fn setZ(self: *DOMPoint, v: f64) void {+ self._proto._z = v;+}+pub fn setW(self: *DOMPoint, v: f64) void {+ self._proto._w = v;+}++pub const JsApi = struct {+ pub const bridge = js.Bridge(DOMPoint);++ pub const Meta = struct {+ pub const name = "DOMPoint";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ };++ pub const constructor = bridge.constructor(DOMPoint.init, .{});+ pub const fromPoint = bridge.function(DOMPoint.fromPoint, .{ .static = true });+ pub const x = bridge.accessor(DOMPoint.getX, DOMPoint.setX, .{});+ pub const y = bridge.accessor(DOMPoint.getY, DOMPoint.setY, .{});+ pub const z = bridge.accessor(DOMPoint.getZ, DOMPoint.setZ, .{});+ pub const w = bridge.accessor(DOMPoint.getW, DOMPoint.setW, .{});+};diff --git a/src/browser/webapi/DOMPointReadOnly.zig b/src/browser/webapi/DOMPointReadOnly.zignew file mode 100644index 000000000..dccb9770a--- /dev/null+++ b/src/browser/webapi/DOMPointReadOnly.zig@@ -0,0 +1,167 @@+// 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 std = @import("std");+const lp = @import("lightpanda");++const js = @import("../js/js.zig");+const Page = @import("../Page.zig");+const DOMPoint = @import("DOMPoint.zig");+const Matrix = @import("DOMMatrixReadOnly.zig");++const Allocator = std.mem.Allocator;++const DOMPointReadOnly = @This();++pub const _prototype_root = true;++_type: Type,+_rc: lp.RC(u8),+_arena: Allocator,++_x: f64,+_y: f64,+_z: f64,+_w: f64,++pub const Type = union(enum) {+ generic,+ mutable: *DOMPoint,+};++pub const DOMPointInit = struct {+ x: f64 = 0,+ y: f64 = 0,+ z: f64 = 0,+ w: f64 = 1,+};++pub fn init(x_: ?f64, y_: ?f64, z_: ?f64, w_: ?f64, exec: *const js.Execution) !*DOMPointReadOnly {+ return createBare(x_ orelse 0, y_ orelse 0, z_ orelse 0, w_ orelse 1, exec.page);+}++pub fn deinit(self: *DOMPointReadOnly, page: *Page) void {+ page.releaseArena(self._arena);+}++pub fn acquireRef(self: *DOMPointReadOnly) void {+ self._rc.acquire();+}++pub fn releaseRef(self: *DOMPointReadOnly, page: *Page) void {+ self._rc.release(self, page);+}++pub fn createBare(x: f64, y: f64, z: f64, w: f64, page: *Page) !*DOMPointReadOnly {+ const arena = try page.getArena(.tiny, "DOMPoint");+ errdefer page.releaseArena(arena);++ const self = try arena.create(DOMPointReadOnly);+ self.* = .{+ ._rc = .{},+ ._arena = arena,+ ._type = .generic,+ ._x = x,+ ._y = y,+ ._z = z,+ ._w = w,+ };+ return self;+}++pub fn fromPoint(other_: ?DOMPointInit, page: *Page) !*DOMPointReadOnly {+ const other: DOMPointInit = other_ orelse .{};+ return createBare(other.x, other.y, other.z, other.w, page);+}++pub fn structuredSerialize(self: *const DOMPointReadOnly, writer: *js.StructuredWriter) !void {+ writer.writeUint64(@bitCast(self._x));+ writer.writeUint64(@bitCast(self._y));+ writer.writeUint64(@bitCast(self._z));+ writer.writeUint64(@bitCast(self._w));+}++pub fn structuredDeserialize(reader: *js.StructuredReader, page: *Page) !*DOMPointReadOnly {+ const x: f64 = @bitCast(try reader.readUint64());+ const y: f64 = @bitCast(try reader.readUint64());+ const z: f64 = @bitCast(try reader.readUint64());+ const w: f64 = @bitCast(try reader.readUint64());+ return createBare(x, y, z, w, page);+}++pub fn matrixTransform(self: *const DOMPointReadOnly, matrix_: ?Matrix.DOMMatrixInit, page: *Page) !*DOMPoint {+ const m = (try Matrix.fixupDict(matrix_ orelse .{})).m;+ const x = self._x;+ const y = self._y;+ const z = self._z;+ const w = self._w;+ return DOMPoint.create(+ m[0] * x + m[4] * y + m[8] * z + m[12] * w,+ m[1] * x + m[5] * y + m[9] * z + m[13] * w,+ m[2] * x + m[6] * y + m[10] * z + m[14] * w,+ m[3] * x + m[7] * y + m[11] * z + m[15] * w,+ page,+ );+}++pub fn getX(self: *const DOMPointReadOnly) f64 {+ return self._x;+}+pub fn getY(self: *const DOMPointReadOnly) f64 {+ return self._y;+}+pub fn getZ(self: *const DOMPointReadOnly) f64 {+ return self._z;+}+pub fn getW(self: *const DOMPointReadOnly) f64 {+ return self._w;+}++pub fn toJSON(self: *const DOMPointReadOnly) struct {+ x: f64,+ y: f64,+ z: f64,+ w: f64,+} {+ return .{ .x = self._x, .y = self._y, .z = self._z, .w = self._w };+}++pub const JsApi = struct {+ pub const bridge = js.Bridge(DOMPointReadOnly);++ pub const Meta = struct {+ pub const name = "DOMPointReadOnly";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ };++ pub const constructor = bridge.constructor(DOMPointReadOnly.init, .{});+ pub const fromPoint = bridge.function(DOMPointReadOnly.fromPoint, .{ .static = true });+ pub const x = bridge.accessor(DOMPointReadOnly.getX, null, .{});+ pub const y = bridge.accessor(DOMPointReadOnly.getY, null, .{});+ pub const z = bridge.accessor(DOMPointReadOnly.getZ, null, .{});+ pub const w = bridge.accessor(DOMPointReadOnly.getW, null, .{});++ pub const matrixTransform = bridge.function(DOMPointReadOnly.matrixTransform, .{});+ pub const toJSON = bridge.function(DOMPointReadOnly.toJSON, .{});+};++const testing = @import("../../testing.zig");+test "WebApi: DOMPoint" {+ try testing.htmlRunner("dompoint.html", .{});+}