Skip to content

Commit 97bb484

Browse files
committed
make elementFromPoint more robust against future changes
1 parent 8f75d67 commit 97bb484

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/browser/html/document.zig

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const parser = @import("../netsurf.zig");
2222
const SessionState = @import("../env.zig").SessionState;
2323

2424
const Window = @import("window.zig").Window;
25+
const Element = @import("../dom/element.zig").Element;
26+
const ElementUnion = @import("../dom/element.zig").Union;
2527
const Document = @import("../dom/document.zig").Document;
2628
const NodeList = @import("../dom/nodelist.zig").NodeList;
2729
const Location = @import("location.zig").Location;
@@ -226,15 +228,16 @@ pub const HTMLDocument = struct {
226228
return "";
227229
}
228230

229-
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?*parser.Element {
231+
// This returns an ElementUnion instead of a *Parser.Element in case the element somehow hasn't passed through the js runtime yet.
232+
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?ElementUnion {
230233
const ix: i32 = @intFromFloat(@floor(x));
231234
const iy: i32 = @intFromFloat(@floor(y));
232235
const element = state.renderer.getElementAtPosition(ix, iy) orelse return null;
233236
// TODO if pointer-events set to none the underlying element should be returned (parser.documentGetDocumentElement(self.document);?)
234-
return element;
237+
return try Element.toInterface(element);
235238
}
236239

237-
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]*parser.Element {
240+
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]ElementUnion {
238241
const ix: i32 = @intFromFloat(@floor(x));
239242
const iy: i32 = @intFromFloat(@floor(y));
240243
const element = state.renderer.getElementAtPosition(ix, iy) orelse return &.{};
@@ -243,8 +246,8 @@ pub const HTMLDocument = struct {
243246
// We need to return either 0 or 1 item, so we cannot fix the size to [1]*parser.Element
244247
// Converting the pointer to a slice []parser.Element is not supported by our framework.
245248
// So instead we just need to allocate the pointer to create a slice of 1.
246-
const heap_ptr = try state.call_arena.create(@TypeOf(element));
247-
heap_ptr.* = element;
249+
const heap_ptr = try state.call_arena.create(ElementUnion);
250+
heap_ptr.* = try Element.toInterface(element);
248251
return heap_ptr[0..1];
249252
}
250253

0 commit comments

Comments
 (0)