Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Add dummy Path2D

Karl Seguinc29f58c6206c
diff --git a/src/browser/PageStatus.zig b/src/browser/PageStatus.zig
index e72cb27a5..e440b6c49 100644
--- a/src/browser/PageStatus.zig
+++ b/src/browser/PageStatus.zig
@@ -59,22 +59,21 @@ blocked: Counts(Loaded) = .{},
// The first recognized bot challenge on a sub-resource.
resource_challenge: ?HttpClient.BotChallenge = null,
// Globals that uncaught ReferenceErrors named. Most of what a page needs and
-// we lack shows up here first, before any call we could count: TradingView
-// dies at `new Path2D()` and never reaches getContext. Deduped and capped;
+// we lack shows up here first, before any call we could count: a page that
+// dies at `createImageBitmap()` never reaches getContext. Deduped and capped;
// the names are whatever V8 put in the message, site bugs included.
missing_globals: MissingGlobals = .{},
// Missing globals that stand for a feature we already count, so the common
// cases stay a one-field check. Only globals we don't register.
const global_features = std.StaticStringMap(Unsupported).initComptime(.{
- .{ "Path2D", .canvas2d },
.{ "ImageBitmap", .canvas2d },
.{ "createImageBitmap", .canvas2d },
.{ "WebGL2RenderingContext", .webgl },
});
// A bit of a hack. Say we want to report that canvas was used, but some sites
-// will try to create a Path2D first and fail there. We do have a
+// will try to create an ImageBitmap first and fail there. We do have a
// unknownObjectPropertyCallback in bridge, but that gets called on the global
// before v8 tries to resolve it itself, so it's very noisy and full of site
// and framework-specific globals. Instead, we try to parse an uncaught error.
@@ -256,7 +255,7 @@ test "PageStatus: missing globals from uncaught ReferenceErrors" {
const json = try std.json.Stringify.valueAlloc(testing.allocator, status.missing_globals, .{});
defer testing.allocator.free(json);
- try testing.expectEqual("[\"Path2D\",\"someUndefinedThing\",\"WebGL2RenderingContext\"]", json);
+ try testing.expectEqual("[\"createImageBitmap\",\"someUndefinedThing\",\"WebGL2RenderingContext\"]", json);
}
test "PageStatus: failed and blocked sub-resources" {
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zig
index 032714dd2..bdc6728df 100644
--- a/src/browser/js/bridge.zig
+++ b/src/browser/js/bridge.zig
@@ -1292,6 +1292,7 @@ pub const PageJsApis = flattenTypes(&.{
@import("../webapi/canvas/TextMetrics.zig"),
@import("../webapi/canvas/CanvasGradient.zig"),
@import("../webapi/canvas/CanvasPattern.zig"),
+ @import("../webapi/canvas/Path2D.zig"),
@import("../webapi/SubtleCrypto.zig"),
@import("../webapi/CryptoKey.zig"),
@import("../webapi/Selection.zig"),
@@ -1404,6 +1405,7 @@ const worker_common_apis = [_]type{
@import("../webapi/canvas/TextMetrics.zig"),
@import("../webapi/canvas/CanvasGradient.zig"),
@import("../webapi/canvas/CanvasPattern.zig"),
+ @import("../webapi/canvas/Path2D.zig"),
@import("../webapi/net/WebSocket.zig"),
@import("../webapi/net/EventSource.zig"),
@import("../webapi/FileReader.zig"),
diff --git a/src/browser/tests/canvas/path2d.html b/src/browser/tests/canvas/path2d.html
new file mode 100644
index 000000000..43ab6ecb1
--- /dev/null
+++ b/src/browser/tests/canvas/path2d.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<script id=constructor>
+{
+ testing.expectEqual(true, new Path2D() instanceof Path2D);
+ testing.expectEqual(true, new Path2D("M10 10 h 80 v 80 h -80 Z") instanceof Path2D);
+ testing.expectEqual(true, new Path2D(new Path2D()) instanceof Path2D);
+ // An invalid SVG path builds an empty path rather than throwing.
+ testing.expectEqual(true, new Path2D("not a path") instanceof Path2D);
+ testing.expectError('TypeError', () => Path2D());
+}
+</script>
+
+<script id=methods>
+{
+ const p = new Path2D();
+ p.moveTo(0, 0);
+ p.lineTo(10, 10);
+ p.quadraticCurveTo(1, 2, 3, 4);
+ p.bezierCurveTo(1, 2, 3, 4, 5, 6);
+ p.arc(5, 5, 5, 0, Math.PI, true);
+ p.arcTo(0, 0, 10, 10, 5);
+ p.ellipse(5, 5, 2, 3, 0, 0, Math.PI);
+ p.rect(0, 0, 10, 10);
+ p.roundRect(0, 0, 10, 10, [2, 4]);
+ p.closePath();
+ p.addPath(new Path2D("M0 0"));
+ p.addPath(new Path2D(), { a: 2, d: 2 });
+ testing.expectError('TypeError', () => p.addPath({}));
+}
+</script>
+
+<script id=context>
+{
+ const path = new Path2D("M0 0 L10 10");
+ const contexts = [
+ document.createElement("canvas").getContext("2d"),
+ new OffscreenCanvas(10, 10).getContext("2d"),
+ ];
+ for (const ctx of contexts) {
+ ctx.fill(path);
+ ctx.fill(path, "evenodd");
+ ctx.stroke(path);
+ ctx.clip(path);
+ ctx.clip(path, "nonzero");
+ testing.expectEqual(false, ctx.isPointInPath(path, 5, 5));
+ testing.expectEqual(false, ctx.isPointInStroke(path, 5, 5));
+ }
+}
+</script>
diff --git a/src/browser/tests/page_missing_global.html b/src/browser/tests/page_missing_global.html
index c5ea27a31..c92cc98b1 100644
--- a/src/browser/tests/page_missing_global.html
+++ b/src/browser/tests/page_missing_global.html
@@ -2,11 +2,11 @@
<html>
<body>
<!-- Fixture for PageStatus.missing_globals: each script is its own
- uncaught error. Path2D twice (deduped), a lowercase site-style
+ uncaught error. createImageBitmap twice (deduped), a lowercase site-style
name, a TypeError (ignored), and a ReferenceError that arrives as
an unhandled rejection. -->
- <script>new Path2D();</script>
- <script>Path2D.prototype;</script>
+ <script>createImageBitmap();</script>
+ <script>createImageBitmap.length;</script>
<script>someUndefinedThing();</script>
<script>null.x;</script>
<script>(async () => { new WebGL2RenderingContext(); })();</script>
diff --git a/src/browser/webapi/canvas/Path2D.zig b/src/browser/webapi/canvas/Path2D.zig
new file mode 100644
index 000000000..496e700c7
--- /dev/null
+++ b/src/browser/webapi/canvas/Path2D.zig
@@ -0,0 +1,75 @@
+// 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 Execution = js.Execution;
+
+
+const Path2D = @This();
+
+const Source = union(enum) {
+ path: *Path2D, // must be first, anything can be coerced to a string
+ svg: []const u8,
+};
+
+// Padding to avoid zero-size struct, which causes identity_map pointer collisions.
+_pad: bool = false,
+
+fn init(_: ?Source, exec: *const Execution) !*Path2D {
+ return exec._factory.create(Path2D{});
+}
+
+fn addPath(_: *Path2D, _: *Path2D, _: ?js.Value) void {}
+fn closePath(_: *Path2D) void {}
+fn moveTo(_: *Path2D, _: f64, _: f64) void {}
+fn lineTo(_: *Path2D, _: f64, _: f64) void {}
+fn quadraticCurveTo(_: *Path2D, _: f64, _: f64, _: f64, _: f64) void {}
+fn bezierCurveTo(_: *Path2D, _: f64, _: f64, _: f64, _: f64, _: f64, _: f64) void {}
+fn arc(_: *Path2D, _: f64, _: f64, _: f64, _: f64, _: f64, _: ?bool) void {}
+fn arcTo(_: *Path2D, _: f64, _: f64, _: f64, _: f64, _: f64) void {}
+fn ellipse(_: *Path2D, _: f64, _: f64, _: f64, _: f64, _: f64, _: f64, _: f64, _: ?bool) void {}
+fn rect(_: *Path2D, _: f64, _: f64, _: f64, _: f64) void {}
+fn roundRect(_: *Path2D, _: f64, _: f64, _: f64, _: f64, _: ?js.Value) void {}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(Path2D);
+
+ pub const Meta = struct {
+ pub const name = "Path2D";
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(Path2D.init, .{});
+ pub const addPath = bridge.function(Path2D.addPath, .{});
+ pub const closePath = bridge.function(Path2D.closePath, .{ .noop = true });
+ pub const moveTo = bridge.function(Path2D.moveTo, .{ .noop = true });
+ pub const lineTo = bridge.function(Path2D.lineTo, .{ .noop = true });
+ pub const quadraticCurveTo = bridge.function(Path2D.quadraticCurveTo, .{ .noop = true });
+ pub const bezierCurveTo = bridge.function(Path2D.bezierCurveTo, .{ .noop = true });
+ pub const arc = bridge.function(Path2D.arc, .{ .noop = true });
+ pub const arcTo = bridge.function(Path2D.arcTo, .{ .noop = true });
+ pub const ellipse = bridge.function(Path2D.ellipse, .{ .noop = true });
+ pub const rect = bridge.function(Path2D.rect, .{ .noop = true });
+ pub const roundRect = bridge.function(Path2D.roundRect, .{ .noop = true });
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: Path2D" {
+ try testing.htmlRunner("canvas/path2d.html", .{});
+}