Skip to content

Commit 20832dc

Browse files
committed
elementsFromPoint
1 parent 97c6ac0 commit 20832dc

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
@@ -207,6 +207,23 @@ pub const HTMLDocument = struct {
207207
pub fn set_bgColor(_: *parser.DocumentHTML, _: []const u8) []const u8 {
208208
return "";
209209
}
210+
211+
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) !?*parser.Element {
212+
const ix: i32 = @intFromFloat(@floor(x));
213+
const iy: i32 = @intFromFloat(@floor(y));
214+
return state.renderer.getElementAtPosition(ix, iy) orelse return null;
215+
}
216+
217+
pub fn _elementsFromPoint(_: *parser.DocumentHTML, x: f32, y: f32, state: *SessionState) ![]*parser.Element { // empty array or optional array?
218+
const ix: i32 = @intFromFloat(@floor(x));
219+
const iy: i32 = @intFromFloat(@floor(y));
220+
const element = state.renderer.getElementAtPosition(ix, iy) orelse return &.{}; // Or should we return the window element instead of empty -> parser.documentGetDocumentElement(self);
221+
// We need to return either 0 or 1 item, so we cannot fix the size to [1]*parser.Element
222+
// Converting the pointer to a slice []parser.Element is not supported by our framework.
223+
// So instead we just need to allocate the pointer to create a slice of 1.
224+
const heap_ptr = try state.arena.create(@TypeOf(element));
225+
return heap_ptr[0..1];
226+
}
210227
};
211228

212229
// Tests
@@ -260,4 +277,14 @@ test "Browser.HTML.Document" {
260277
.{ "document.cookie = 'favorite_food=tripe; SameSite=None; Secure'", "favorite_food=tripe; SameSite=None; Secure" },
261278
.{ "document.cookie", "name=Oeschger; favorite_food=tripe" },
262279
}, .{});
280+
281+
try runner.testCases(&.{
282+
.{ "document.elementFromPoint(0.5, 0.5)", "null" },
283+
.{ "document.elementsFromPoint(0.5, 0.5)", "" },
284+
.{ "document.createElement('div').getClientRects()", "[object Object]" },
285+
.{ "document.elementFromPoint(0.5, 0.5)", "[object HTMLDivElement]" },
286+
.{ "let elems = document.elementsFromPoint(0.5, 0.5)", "undefined" },
287+
.{ "elems.length", "1" },
288+
.{ "elems[0]", "[object Element]" }, // TBD why is this not: HTMLDivElement?
289+
}, .{});
263290
}

0 commit comments

Comments
 (0)