Skip to content

Commit f20457c

Browse files
committed
Fix activeElement
#742 was only correct in isolation. The problem is that both html/document and dom/document are now attempting to attach state to the same *parser.Node. This is a problem because they're two different types of state. If an HTMLDocument is attached to the node, and you then try to access that as a DOMDocument, it'll crash. I moved the dom/document state (active_element) into the html/document. The other option is to introduce something like src/browser/state/document.zig and have both HTMLDocument and DOMDocument share/load that.
1 parent 9efc1a1 commit f20457c

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/browser/dom/document.zig

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

45-
active_element: ?*parser.Element = null,
45+
// DO NOT ADD STATE HERE
46+
// ADD IT TO html/document
4647

4748
pub fn constructor(page: *const Page) !*parser.DocumentHTML {
4849
const doc = try parser.documentCreateDocument(
@@ -246,8 +247,9 @@ pub const Document = struct {
246247
}
247248

248249
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| {
250+
const HTMLDocument = @import("../html/document.zig").HTMLDocument;
251+
const html_doc = try page.getOrCreateNodeWrapper(HTMLDocument, @ptrCast(doc));
252+
if (html_doc.active_element) |ae| {
251253
return try Element.toInterface(ae);
252254
}
253255

src/browser/html/document.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub const HTMLDocument = struct {
4040
pub const subtype = .node;
4141

4242
ready_state: ReadyState = .loading,
43+
active_element: ?*parser.Element = null,
4344

4445
const ReadyState = enum {
4546
loading,

src/browser/html/elements.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ pub const HTMLElement = struct {
161161

162162
const root_node = try parser.nodeGetRootNode(@ptrCast(e));
163163

164-
const Document = @import("../dom/document.zig").Document;
165-
const document = try page.getOrCreateNodeWrapper(Document, @ptrCast(root_node));
164+
const HTMLDocument = @import("../html/document.zig").HTMLDocument;
165+
const html_doc = try page.getOrCreateNodeWrapper(HTMLDocument, @ptrCast(root_node));
166166

167167
// TODO: some elements can't be focused, like if they're disabled
168168
// but there doesn't seem to be a generic way to check this. For example
169169
// we could look for the "disabled" attribute, but that's only meaningful
170170
// on certain types, and libdom's vtable doesn't seem to expose this.
171-
document.active_element = @ptrCast(e);
171+
html_doc.active_element = @ptrCast(e);
172172
}
173173
};
174174

0 commit comments

Comments
 (0)