Skip to content

Commit 74963d4

Browse files
committed
refacto and use Element.toInterface
1 parent d99b49e commit 74963d4

File tree

8 files changed

+31
-24
lines changed

8 files changed

+31
-24
lines changed

src/browser/dom/document.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,19 @@ pub const Document = struct {
119119

120120
pub fn _getElementById(self: *parser.Document, id: []const u8) !?ElementUnion {
121121
const e = try parser.documentGetElementById(self, id) orelse return null;
122-
return try Element.toInterface(e);
122+
return try Element.toInterface(ElementUnion, e);
123123
}
124124

125125
pub fn _createElement(self: *parser.Document, tag_name: []const u8) !ElementUnion {
126126
// The element’s namespace is the HTML namespace when document is an HTML document
127127
// https://dom.spec.whatwg.org/#ref-for-dom-document-createelement%E2%91%A0
128128
const e = try parser.documentCreateElementNS(self, "http://www.w3.org/1999/xhtml", tag_name);
129-
return Element.toInterface(e);
129+
return Element.toInterface(ElementUnion, e);
130130
}
131131

132132
pub fn _createElementNS(self: *parser.Document, ns: []const u8, tag_name: []const u8) !ElementUnion {
133133
const e = try parser.documentCreateElementNS(self, ns, tag_name);
134-
return try Element.toInterface(e);
134+
return try Element.toInterface(ElementUnion, e);
135135
}
136136

137137
// We can't simply use libdom dom_document_get_elements_by_tag_name here.
@@ -210,12 +210,12 @@ pub const Document = struct {
210210

211211
pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion {
212212
const elt = try parser.documentGetDocumentElement(self) orelse return null;
213-
return try Element.toInterface(elt);
213+
return try Element.toInterface(ElementUnion, elt);
214214
}
215215

216216
pub fn get_lastElementChild(self: *parser.Document) !?ElementUnion {
217217
const elt = try parser.documentGetDocumentElement(self) orelse return null;
218-
return try Element.toInterface(elt);
218+
return try Element.toInterface(ElementUnion, elt);
219219
}
220220

221221
pub fn get_childElementCount(self: *parser.Document) !u32 {
@@ -230,7 +230,7 @@ pub const Document = struct {
230230

231231
if (n == null) return null;
232232

233-
return try Element.toInterface(parser.nodeToElement(n.?));
233+
return try Element.toInterface(ElementUnion, parser.nodeToElement(n.?));
234234
}
235235

236236
pub fn _querySelectorAll(self: *parser.Document, selector: []const u8, page: *Page) !NodeList {
@@ -273,7 +273,7 @@ pub const Document = struct {
273273

274274
pub fn get_activeElement(self: *parser.Document, page: *Page) !?ElementUnion {
275275
const ae = (try getActiveElement(self, page)) orelse return null;
276-
return try Element.toInterface(ae);
276+
return try Element.toInterface(ElementUnion, ae);
277277
}
278278

279279
// TODO: some elements can't be focused, like if they're disabled

src/browser/dom/document_fragment.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub const DocumentFragment = struct {
6666

6767
if (n == null) return null;
6868

69-
return try Element.toInterface(parser.nodeToElement(n.?));
69+
return try Element.toInterface(ElementUnion, parser.nodeToElement(n.?));
7070
}
7171

7272
pub fn _querySelectorAll(self: *parser.DocumentFragment, selector: []const u8, page: *Page) !NodeList {

src/browser/dom/element.zig

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ pub const Element = struct {
5454
left: f64,
5555
};
5656

57-
pub fn toInterface(e: *parser.Element) !Union {
58-
return try HTMLElem.toInterface(Union, e);
57+
pub fn toInterface(comptime T: type, e: *parser.Element) !T {
58+
const tagname = try parser.elementGetTagName(e) orelse {
59+
// in case of null tagname, return the element as it.
60+
return .{ .Element = e };
61+
};
62+
_ = parser.Tag.fromString(tagname) catch {
63+
// if the tag is invalid, we don't have an HTMLElement.
64+
return .{ .Element = e };
65+
};
5966
// SVGElement and MathML are not supported yet.
67+
return try HTMLElem.toInterface(T, e);
6068
}
6169

6270
// JS funcs
@@ -376,7 +384,7 @@ pub const Element = struct {
376384

377385
if (n == null) return null;
378386

379-
return try toInterface(parser.nodeToElement(n.?));
387+
return try toInterface(Union, parser.nodeToElement(n.?));
380388
}
381389

382390
pub fn _querySelectorAll(self: *parser.Element, selector: []const u8, page: *Page) !NodeList {

src/browser/dom/html_collection.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Allocator = std.mem.Allocator;
2222
const parser = @import("../netsurf.zig");
2323

2424
const Element = @import("element.zig").Element;
25+
const ElementUnion = @import("element.zig").Union;
2526
const Union = @import("element.zig").Union;
2627
const JsThis = @import("../env.zig").JsThis;
2728
const Walker = @import("walker.zig").Walker;
@@ -395,7 +396,7 @@ pub const HTMLCollection = struct {
395396
pub fn _item(self: *HTMLCollection, index: u32) !?Union {
396397
const node = try self.item(index) orelse return null;
397398
const e = @as(*parser.Element, @ptrCast(node));
398-
return try Element.toInterface(e);
399+
return try Element.toInterface(ElementUnion, e);
399400
}
400401

401402
pub fn _namedItem(self: *const HTMLCollection, name: []const u8) !?Union {
@@ -412,13 +413,13 @@ pub const HTMLCollection = struct {
412413
var attr = try parser.elementGetAttribute(elem, "id");
413414
// check if the node id corresponds to the name argument.
414415
if (attr != null and std.mem.eql(u8, name, attr.?)) {
415-
return try Element.toInterface(elem);
416+
return try Element.toInterface(ElementUnion, elem);
416417
}
417418

418419
attr = try parser.elementGetAttribute(elem, "name");
419420
// check if the node id corresponds to the name argument.
420421
if (attr != null and std.mem.eql(u8, name, attr.?)) {
421-
return try Element.toInterface(elem);
422+
return try Element.toInterface(ElementUnion, elem);
422423
}
423424
}
424425
}
@@ -445,7 +446,7 @@ pub const HTMLCollection = struct {
445446
for (0..len) |i| {
446447
const node = try self.item(@intCast(i)) orelse unreachable;
447448
const e = @as(*parser.Element, @ptrCast(node));
448-
const as_interface = try Element.toInterface(e);
449+
const as_interface = try Element.toInterface(ElementUnion, e);
449450
try js_this.setIndex(@intCast(i), as_interface, .{});
450451

451452
if (try item_name(e)) |name| {

src/browser/dom/node.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ pub const Node = struct {
6767

6868
pub fn toInterface(node: *parser.Node) !Union {
6969
return switch (try parser.nodeType(node)) {
70-
.element => try HTMLElem.toInterface(
71-
Union,
72-
@as(*parser.Element, @ptrCast(node)),
73-
),
70+
.element => try Element.toInterface(Union, @as(*parser.Element, @ptrCast(node))),
7471
.comment => .{ .Comment = @as(*parser.Comment, @ptrCast(node)) },
7572
.text => .{ .Text = @as(*parser.Text, @ptrCast(node)) },
7673
.cdata_section => .{ .CDATASection = @as(*parser.CDATASection, @ptrCast(node)) },

src/browser/dom/shadow_root.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub const ShadowRoot = struct {
3636
};
3737

3838
pub fn get_host(self: *const ShadowRoot) !ElementUnion {
39-
return Element.toInterface(self.host);
39+
return Element.toInterface(ElementUnion, self.host);
4040
}
4141
};
4242

src/browser/html/document.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub const HTMLDocument = struct {
258258
pub fn _elementFromPoint(_: *parser.DocumentHTML, x: i32, y: i32, page: *Page) !?ElementUnion {
259259
const element = page.renderer.getElementAtPosition(x, y) orelse return null;
260260
// TODO if pointer-events set to none the underlying element should be returned (parser.documentGetDocumentElement(self.document);?)
261-
return try Element.toInterface(element);
261+
return try Element.toInterface(ElementUnion, element);
262262
}
263263

264264
// 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.
@@ -272,7 +272,7 @@ pub const HTMLDocument = struct {
272272

273273
var list: std.ArrayListUnmanaged(ElementUnion) = .empty;
274274
try list.ensureTotalCapacity(page.call_arena, 3);
275-
list.appendAssumeCapacity(try Element.toInterface(element));
275+
list.appendAssumeCapacity(try Element.toInterface(ElementUnion, element));
276276

277277
// Since we are using a flat renderer there is no hierarchy of elements. What we do know is that the element is part of the main document.
278278
// Thus we can add the HtmlHtmlElement and it's child HTMLBodyElement to the returned list.
@@ -282,9 +282,9 @@ pub const HTMLDocument = struct {
282282
return list.items;
283283
};
284284
if (try parser.documentHTMLBody(page.window.document)) |body| {
285-
list.appendAssumeCapacity(try Element.toInterface(parser.bodyToElement(body)));
285+
list.appendAssumeCapacity(try Element.toInterface(ElementUnion, parser.bodyToElement(body)));
286286
}
287-
list.appendAssumeCapacity(try Element.toInterface(doc_elem));
287+
list.appendAssumeCapacity(try Element.toInterface(ElementUnion, doc_elem));
288288
return list.items;
289289
}
290290

src/browser/html/elements.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
3434

3535
// HTMLElement interfaces
3636
pub const Interfaces = .{
37+
Element,
3738
HTMLElement,
3839
HTMLUnknownElement,
3940
HTMLAnchorElement,

0 commit comments

Comments
 (0)