Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add [mostly dummy] element.requestFullscreen

Karl Seguin2bdd7a0c08d1
diff --git a/src/browser/EventManager.zig b/src/browser/EventManager.zig
index 02aae6236..f392b9df6 100644
--- a/src/browser/EventManager.zig
+++ b/src/browser/EventManager.zig
@@ -29,6 +29,7 @@ const Window = @import("webapi/Window.zig");
const Element = @import("webapi/Element.zig");
const ShadowRoot = @import("webapi/ShadowRoot.zig");
const Performance = @import("webapi/Performance.zig");
+const Screen = @import("webapi/Screen.zig");
const EventTarget = @import("webapi/EventTarget.zig");
const MediaQueryList = @import("webapi/css/MediaQueryList.zig");
const XMLHttpRequestEventTarget = @import("webapi/net/XMLHttpRequestEventTarget.zig");
@@ -91,6 +92,7 @@ pub fn dispatch(self: *EventManager, target: *EventTarget, event: *Event) Dispat
.xhr => try self.dispatchDirect(target, event, target.subtype(XMLHttpRequestEventTarget).inlineHandler(event._type_string), .{ .context = "dispatch" }),
.media_query_list => try self.dispatchDirect(target, event, target.subtype(MediaQueryList).inlineHandler(event._type_string), .{ .context = "dispatch" }),
.performance => try self.dispatchDirect(target, event, target.subtype(Performance).inlineHandler(event._type_string), .{ .context = "dispatch" }),
+ .screen_orientation => try self.dispatchDirect(target, event, target.subtype(Screen.Orientation).inlineHandler(event._type_string), .{ .context = "dispatch" }),
.window => try self.dispatchDirect(target, event, windowInlineHandler(target.subtype(Window), event._type_string), .{ .context = "dispatch" }),
else => try self.dispatchDirect(target, event, null, .{ .context = "dispatch" }),
}
diff --git a/src/browser/tests/window/screen.html b/src/browser/tests/window/screen.html
index 616490336..37f866753 100644
--- a/src/browser/tests/window/screen.html
+++ b/src/browser/tests/window/screen.html
@@ -21,3 +21,63 @@
// above shouldn't crash (it used to)
testing.expectEqual(true, true);
</script>
+
+<script id=orientation_accessors>
+ testing.expectEqual('function', typeof screen.orientation.lock);
+ testing.expectEqual('function', typeof screen.orientation.unlock);
+ testing.expectError('TypeError', () => ScreenOrientation.prototype.type);
+ testing.expectError('TypeError', () => ScreenOrientation.prototype.angle);
+
+ testing.expectEqual(null, screen.orientation.onchange);
+ let changed = 0;
+ screen.orientation.onchange = () => changed += 1;
+ screen.orientation.dispatchEvent(new Event('change'));
+ testing.expectEqual(1, changed);
+ screen.orientation.onchange = null;
+
+ testing.expectEqual(undefined, screen.orientation.unlock());
+</script>
+
+<script id=orientation_lock type=module>
+ const state = await testing.async();
+ const errorName = (p) => p.then(() => 'resolved', (e) => e.name);
+
+ const unsupported = await errorName(screen.orientation.lock('portrait'));
+ const invalid = await Promise.all([
+ screen.orientation.lock('nope'),
+ screen.orientation.lock(null),
+ screen.orientation.lock(),
+ ].map(errorName));
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual('NotSupportedError', unsupported);
+ testing.expectEqual(['TypeError', 'TypeError', 'TypeError'], invalid);
+ });
+</script>
+
+<script id=fullscreen type=module>
+ const state = await testing.async();
+
+ let requestError = null;
+ try {
+ await document.documentElement.requestFullscreen();
+ } catch (e) {
+ requestError = e;
+ }
+ let exitError = null;
+ try {
+ await document.exitFullscreen();
+ } catch (e) {
+ exitError = e;
+ }
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual(false, document.fullscreenEnabled);
+ testing.expectEqual(false, document.fullscreen);
+ testing.expectEqual(null, document.fullscreenElement);
+ testing.expectEqual(true, requestError instanceof TypeError);
+ testing.expectEqual(true, exitError instanceof TypeError);
+ });
+</script>
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zig
index bdc09cca3..3b9250d2c 100644
--- a/src/browser/webapi/Document.zig
+++ b/src/browser/webapi/Document.zig
@@ -1368,6 +1368,15 @@ fn getAdoptedStyleSheets(self: *Document, frame: *Frame) !js.Object.Global {
return self._adopted_style_sheets.?;
}
+fn getFullscreenElement(_: *const Document) ?*Element {
+ // see Element.requestFullscreen, nothing is ever fullscreen
+ return null;
+}
+
+fn exitFullscreen(_: *Document, frame: *Frame) js.Promise {
+ return frame.js.local.?.rejectPromise(.{ .type_error = "Document not in fullscreen" });
+}
+
pub fn hasFocus(_: *Document) bool {
log.debug(.not_implemented, "Document.hasFocus", .{});
return true;
@@ -1724,6 +1733,10 @@ pub const JsApi = struct {
}
}.defaultView, null, .{});
pub const hasFocus = bridge.function(Document.hasFocus, .{});
+ pub const fullscreenEnabled = bridge.property(false, .{ .template = false, .readonly = true });
+ pub const fullscreen = bridge.property(false, .{ .template = false, .readonly = true });
+ pub const fullscreenElement = bridge.accessor(Document.getFullscreenElement, null, .{});
+ pub const exitFullscreen = bridge.function(Document.exitFullscreen, .{});
pub const prerendering = bridge.property(false, .{ .template = false });
pub const characterSet = bridge.accessor(Document.getCharset, null, .{});
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 7b963a227..580131add 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -553,6 +553,10 @@ pub fn insertAdjacentHTML(
return he.insertAdjacentHTML(position, html_or_xml, frame);
}
+pub fn requestFullscreen(_: *const Element, frame: *Frame) js.Promise {
+ return frame.js.local.?.rejectPromise(.{ .type_error = "Fullscreen is not supported" });
+}
+
pub fn getOuterHTML(self: *Element, writer: *std.Io.Writer, frame: *Frame) !void {
return dump.deep(self.asNode(), .{ .shadow = .skip }, writer, frame);
}
@@ -1478,6 +1482,7 @@ const CheckVisibilityOpts = struct {
opacityProperty: bool = false,
visibilityProperty: bool = false,
};
+
pub fn checkVisibility(self: *Element, opts_: ?CheckVisibilityOpts, frame: *Frame) bool {
const opts = opts_ orelse CheckVisibilityOpts{};
const owner = self.ownerFrame(frame) orelse return false;
@@ -2746,6 +2751,7 @@ pub const JsApi = struct {
pub const getAnimations = bridge.function(Element.getAnimations, .{});
pub const animate = bridge.function(Element.animate, .{});
pub const checkVisibility = bridge.function(Element.checkVisibility, .{});
+ pub const requestFullscreen = bridge.function(Element.requestFullscreen, .{});
pub const clientWidth = bridge.accessor(Element.getClientWidth, null, .{});
pub const clientHeight = bridge.accessor(Element.getClientHeight, null, .{});
pub const clientTop = bridge.accessor(Element.getClientTop, null, .{});
diff --git a/src/browser/webapi/Screen.zig b/src/browser/webapi/Screen.zig
index 09788874a..6b9352894 100644
--- a/src/browser/webapi/Screen.zig
+++ b/src/browser/webapi/Screen.zig
@@ -16,6 +16,8 @@
// 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 lp = @import("lightpanda");
+
const js = @import("../js/js.zig");
const Frame = @import("../Frame.zig");
const EventTarget = @import("EventTarget.zig");
@@ -79,6 +81,7 @@ pub const Orientation = struct {
pub const Proto = EventTarget;
_proto: *EventTarget,
+ _on_change: ?js.Function.Global = null,
pub fn init(frame: *Frame) !*Orientation {
return frame._factory.eventTarget(Orientation{
@@ -90,16 +93,57 @@ pub const Orientation = struct {
return self._proto;
}
- pub fn getType(_: *const Orientation, frame: *Frame) []const u8 {
- const orientation = frame.page.getViewport().orientation orelse return "landscape-primary";
- return @tagName(orientation.type);
+ pub fn inlineHandler(self: *const Orientation, typ: lp.String) ?js.Function.Global {
+ if (typ.eql(comptime .wrap("change"))) {
+ return self._on_change;
+ }
+ return null;
}
- pub fn getAngle(_: *const Orientation, frame: *Frame) u16 {
+ const LockType = enum {
+ any,
+ natural,
+ landscape,
+ portrait,
+ @"portrait-primary",
+ @"portrait-secondary",
+ @"landscape-primary",
+ @"landscape-secondary",
+ pub const js_enum_from_string = true;
+ };
+
+ fn getAngle(_: *const Orientation, frame: *Frame) u16 {
const orientation = frame.page.getViewport().orientation orelse return 0;
return orientation.angle;
}
+ // Without an emulated orientation, the screen never rotates, so its
+ // orientation follows its dimensions.
+ fn getType(_: *const Orientation, frame: *Frame) []const u8 {
+ const viewport = frame.page.getViewport();
+ if (viewport.orientation) |orientation| {
+ return @tagName(orientation.type);
+ }
+ const width = viewport.screen_width orelse viewport.width;
+ const height = viewport.screen_height orelse viewport.height;
+ return if (height > width) "portrait-primary" else "landscape-primary";
+ }
+
+ fn lock(_: *Orientation, _: LockType) !js.Promise {
+ return error.NotSupported;
+ }
+
+ // Nothing is ever locked.
+ fn unlock(_: *Orientation) void {}
+
+ fn getOnChange(self: *const Orientation) ?js.Function.Global {
+ return self._on_change;
+ }
+
+ fn setOnChange(self: *Orientation, cb: ?js.Function.Global) void {
+ self._on_change = cb;
+ }
+
pub const JsApi = struct {
pub const bridge = js.Bridge(Orientation);
@@ -111,5 +155,8 @@ pub const Orientation = struct {
pub const angle = bridge.accessor(Orientation.getAngle, null, .{});
pub const @"type" = bridge.accessor(Orientation.getType, null, .{});
+ pub const lock = bridge.function(Orientation.lock, .{});
+ pub const unlock = bridge.function(Orientation.unlock, .{});
+ pub const onchange = bridge.accessor(Orientation.getOnChange, Orientation.setOnChange, .{});
};
};