Skip to content

Commit 6ac9bcd

Browse files
committed
make elementFromPoint more robust against future changes
1 parent a0937b3 commit 6ac9bcd

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;
@@ -213,15 +215,16 @@ pub const HTMLDocument = struct {
213215
return "";
214216
}
215217

216-
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?*parser.Element {
218+
// This returns an ElementUnion instead of a *Parser.Element in case the element somehow hasn't passed through the js runtime yet.
219+
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?ElementUnion {
217220
const ix: i32 = @intFromFloat(@floor(x));
218221
const iy: i32 = @intFromFloat(@floor(y));
219222
const element = state.renderer.getElementAtPosition(ix, iy) orelse return null;
220223
// TODO if pointer-events set to none the underlying element should be returned (parser.documentGetDocumentElement(self.document);?)
221-
return element;
224+
return try Element.toInterface(element);
222225
}
223226

224-
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]*parser.Element {
227+
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]ElementUnion {
225228
const ix: i32 = @intFromFloat(@floor(x));
226229
const iy: i32 = @intFromFloat(@floor(y));
227230
const element = state.renderer.getElementAtPosition(ix, iy) orelse return &.{};
@@ -230,8 +233,8 @@ pub const HTMLDocument = struct {
230233
// We need to return either 0 or 1 item, so we cannot fix the size to [1]*parser.Element
231234
// Converting the pointer to a slice []parser.Element is not supported by our framework.
232235
// So instead we just need to allocate the pointer to create a slice of 1.
233-
const heap_ptr = try state.call_arena.create(@TypeOf(element));
234-
heap_ptr.* = element;
236+
const heap_ptr = try state.call_arena.create(ElementUnion);
237+
heap_ptr.* = try Element.toInterface(element);
235238
return heap_ptr[0..1];
236239
}
237240
};

0 commit comments

Comments
 (0)