Skip to content

Commit 6b0c532

Browse files
authored
Merge pull request #742 from lightpanda-io/focus_and_active_element
Focus and active element
2 parents 69215e7 + 18e6f9b commit 6b0c532

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

src/browser/dom/document.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub const Document = struct {
4242
pub const prototype = *Node;
4343
pub const subtype = .node;
4444

45+
active_element: ?*parser.Element = null,
46+
4547
pub fn constructor(page: *const Page) !*parser.DocumentHTML {
4648
const doc = try parser.documentCreateDocument(
4749
try parser.documentHTMLGetTitle(page.window.document),
@@ -242,6 +244,19 @@ pub const Document = struct {
242244
pub fn _createTreeWalker(_: *parser.Document, root: *parser.Node, what_to_show: ?u32, filter: ?TreeWalker.TreeWalkerOpts) !TreeWalker {
243245
return try TreeWalker.init(root, what_to_show, filter);
244246
}
247+
248+
pub fn get_activeElement(doc: *parser.Document, page: *Page) !?ElementUnion {
249+
const self = try page.getOrCreateNodeWrapper(Document, @ptrCast(doc));
250+
if (self.active_element) |ae| {
251+
return try Element.toInterface(ae);
252+
}
253+
254+
if (try parser.documentHTMLBody(page.window.document)) |body| {
255+
return try Element.toInterface(@ptrCast(body));
256+
}
257+
258+
return get_documentElement(doc);
259+
}
245260
};
246261

247262
const testing = @import("../../testing.zig");
@@ -412,6 +427,12 @@ test "Browser.DOM.Document" {
412427
},
413428
}, .{});
414429

430+
try runner.testCases(&.{
431+
.{ "document.activeElement === document.body", "true" },
432+
.{ "document.getElementById('link').focus()", "undefined" },
433+
.{ "document.activeElement === document.getElementById('link')", "true" },
434+
}, .{});
435+
415436
// this test breaks the doc structure, keep it at the end of the test
416437
// suite.
417438
try runner.testCases(&.{

src/browser/dom/element.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ pub const Element = struct {
368368
// Returns a 0 DOMRect object if the element is eventually detached from the main window
369369
pub fn _getBoundingClientRect(self: *parser.Element, page: *Page) !DOMRect {
370370
// Since we are lazy rendering we need to do this check. We could store the renderer in a viewport such that it could cache these, but it would require tracking changes.
371-
const root = try parser.nodeGetRootNode(parser.elementToNode(self));
372-
if (root != parser.documentToNode(parser.documentHTMLToDocument(page.window.document))) {
371+
if (!try page.isNodeAttached(parser.elementToNode(self))) {
373372
return DOMRect{ .x = 0, .y = 0, .width = 0, .height = 0 };
374373
}
375374
return page.renderer.getRect(self);
@@ -379,8 +378,7 @@ pub const Element = struct {
379378
// We do not render so it only always return the element's bounding rect.
380379
// Returns an empty array if the element is eventually detached from the main window
381380
pub fn _getClientRects(self: *parser.Element, page: *Page) ![]DOMRect {
382-
const root = try parser.nodeGetRootNode(parser.elementToNode(self));
383-
if (root != parser.documentToNode(parser.documentHTMLToDocument(page.window.document))) {
381+
if (!try page.isNodeAttached(parser.elementToNode(self))) {
384382
return &.{};
385383
}
386384
const heap_ptr = try page.call_arena.create(DOMRect);

src/browser/html/elements.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ pub const HTMLElement = struct {
148148
});
149149
_ = try parser.elementDispatchEvent(@ptrCast(e), @ptrCast(event));
150150
}
151+
152+
const FocusOpts = struct {
153+
preventScroll: bool,
154+
focusVisible: bool,
155+
};
156+
pub fn _focus(e: *parser.ElementHTML, _: ?FocusOpts, page: *Page) !void {
157+
if (!try page.isNodeAttached(@ptrCast(e))) {
158+
return;
159+
}
160+
161+
const root_node = try parser.nodeGetRootNode(@ptrCast(e));
162+
163+
const Document = @import("../dom/document.zig").Document;
164+
const document = try page.getOrCreateNodeWrapper(Document, @ptrCast(root_node));
165+
166+
// TODO: some elements can't be focused, like if they're disabled
167+
// but there doesn't seem to be a generic way to check this. For example
168+
// we could look for the "disabled" attribute, but that's only meaningful
169+
// on certain types, and libdom's vtable doesn't seem to expose this.
170+
document.active_element = @ptrCast(e);
171+
}
151172
};
152173

153174
// Deprecated HTMLElements in Chrome (2023/03/15)
@@ -1181,4 +1202,11 @@ test "Browser.HTML.Element" {
11811202
.{ "a.href = 'about'", null },
11821203
.{ "a.href", "https://lightpanda.io/opensource-browser/about" },
11831204
}, .{});
1205+
1206+
// detached node cannot be focused
1207+
try runner.testCases(&.{
1208+
.{ "const focused = document.activeElement", null },
1209+
.{ "document.createElement('a').focus()", null },
1210+
.{ "document.activeElement === focused", "true" },
1211+
}, .{});
11841212
}

src/browser/page.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,11 @@ pub const Page = struct {
643643
try self.navigateFromWebAPI(action, opts);
644644
}
645645

646+
pub fn isNodeAttached(self: *const Page, node: *parser.Node) !bool {
647+
const root = parser.documentToNode(parser.documentHTMLToDocument(self.window.document));
648+
return root == try parser.nodeGetRootNode(node);
649+
}
650+
646651
fn elementSubmitForm(self: *Page, element: *parser.Element) !void {
647652
const form = (try self.formForElement(element)) orelse return;
648653
return self.submitForm(@ptrCast(form), @ptrCast(element));

0 commit comments

Comments
 (0)