Skip to content

Commit 46a06a4

Browse files
committed
elementsFromPoint
1 parent a99d193 commit 46a06a4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/browser/html/document.zig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ pub const HTMLDocument = struct {
212212
pub fn set_bgColor(_: *parser.DocumentHTML, _: []const u8) []const u8 {
213213
return "";
214214
}
215+
216+
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?*parser.Element {
217+
const ix: i32 = @intFromFloat(@floor(x));
218+
const iy: i32 = @intFromFloat(@floor(y));
219+
return state.renderer.getElementAtPosition(ix, iy) orelse return null;
220+
}
221+
222+
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]*parser.Element { // empty array or optional array?
223+
const ix: i32 = @intFromFloat(@floor(x));
224+
const iy: i32 = @intFromFloat(@floor(y));
225+
const element = state.renderer.getElementAtPosition(ix, iy) orelse return &.{}; // Or should we return the window element instead of empty -> parser.documentGetDocumentElement(self);
226+
// We need to return either 0 or 1 item, so we cannot fix the size to [1]*parser.Element
227+
// Converting the pointer to a slice []parser.Element is not supported by our framework.
228+
// So instead we just need to allocate the pointer to create a slice of 1.
229+
const heap_ptr = try state.arena.create(@TypeOf(element));
230+
return heap_ptr[0..1];
231+
}
215232
};
216233

217234
// Tests
@@ -266,6 +283,16 @@ test "Browser.HTML.Document" {
266283
.{ "document.cookie", "name=Oeschger; favorite_food=tripe" },
267284
}, .{});
268285

286+
try runner.testCases(&.{
287+
.{ "document.elementFromPoint(0.5, 0.5)", "null" },
288+
.{ "document.elementsFromPoint(0.5, 0.5)", "" },
289+
.{ "document.createElement('div').getClientRects()", "[object Object]" },
290+
.{ "document.elementFromPoint(0.5, 0.5)", "[object HTMLDivElement]" },
291+
.{ "let elems = document.elementsFromPoint(0.5, 0.5)", "undefined" },
292+
.{ "elems.length", "1" },
293+
.{ "elems[0]", "[object Element]" }, // TBD why is this not: HTMLDivElement?
294+
}, .{});
295+
269296
try runner.testCases(&.{
270297
.{ "!document.all", "true" },
271298
.{ "!!document.all", "false" },

0 commit comments

Comments
 (0)