Skip to content

Commit 42c3841

Browse files
authored
Merge pull request #842 from lightpanda-io/fix_elementFromPoint_crash
Rely on js.zig for float->int translation
2 parents 002d9c1 + 2885cec commit 42c3841

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/browser/html/document.zig

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,19 +233,23 @@ pub const HTMLDocument = struct {
233233
// Since LightPanda requires the client to know what they are clicking on we do not return the underlying element at this moment
234234
// This can currenty only happen if the first pixel is clicked without having rendered any element. This will change when css properties are supported.
235235
// This returns an ElementUnion instead of a *Parser.Element in case the element somehow hasn't passed through the js runtime yet.
236-
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, page: *Page) !?ElementUnion {
237-
const ix: i32 = @intFromFloat(@floor(x));
238-
const iy: i32 = @intFromFloat(@floor(y));
239-
const element = page.renderer.getElementAtPosition(ix, iy) orelse return null;
236+
// While x and y should be f32, here we take i32 since that's what our
237+
// "renderer" uses. By specifying i32 here, rather than f32 and doing the
238+
// conversion ourself, we rely on v8's type conversion which is both more
239+
// flexible (e.g. handles NaN) and will be more consistent with a browser.
240+
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: i32, y: i32, page: *Page) !?ElementUnion {
241+
const element = page.renderer.getElementAtPosition(x, y) orelse return null;
240242
// TODO if pointer-events set to none the underlying element should be returned (parser.documentGetDocumentElement(self.document);?)
241243
return try Element.toInterface(element);
242244
}
243245

244246
// Returns an array of all elements at the specified coordinates (relative to the viewport). The elements are ordered from the topmost to the bottommost box of the viewport.
245-
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, page: *Page) ![]ElementUnion {
246-
const ix: i32 = @intFromFloat(@floor(x));
247-
const iy: i32 = @intFromFloat(@floor(y));
248-
const element = page.renderer.getElementAtPosition(ix, iy) orelse return &.{};
247+
// While x and y should be f32, here we take i32 since that's what our
248+
// "renderer" uses. By specifying i32 here, rather than f32 and doing the
249+
// conversion ourself, we rely on v8's type conversion which is both more
250+
// flexible (e.g. handles NaN) and will be more consistent with a browser.
251+
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: i32, y: i32, page: *Page) ![]ElementUnion {
252+
const element = page.renderer.getElementAtPosition(x, y) orelse return &.{};
249253
// TODO if pointer-events set to none the underlying element should be returned (parser.documentGetDocumentElement(self.document);?)
250254

251255
var list: std.ArrayListUnmanaged(ElementUnion) = .empty;

0 commit comments

Comments
 (0)