Skip to content

Commit 0c8f448

Browse files
committed
refacto and use Element.toInterface
1 parent adb43ba commit 0c8f448

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

src/browser/dom/character_data.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const Node = @import("node.zig").Node;
2424
const Comment = @import("comment.zig").Comment;
2525
const Text = @import("text.zig");
2626
const ProcessingInstruction = @import("processing_instruction.zig").ProcessingInstruction;
27-
const HTMLElem = @import("../html/elements.zig");
27+
const Element = @import("element.zig").Element;
28+
const ElementUnion = @import("element.zig").Union;
2829

2930
// CharacterData interfaces
3031
pub const Interfaces = .{
@@ -49,20 +50,20 @@ pub const CharacterData = struct {
4950
return try parser.characterDataLength(self);
5051
}
5152

52-
pub fn get_nextElementSibling(self: *parser.CharacterData) !?HTMLElem.Union {
53+
pub fn get_nextElementSibling(self: *parser.CharacterData) !?ElementUnion {
5354
const res = try parser.nodeNextElementSibling(parser.characterDataToNode(self));
5455
if (res == null) {
5556
return null;
5657
}
57-
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
58+
return try Element.toInterface(res.?);
5859
}
5960

60-
pub fn get_previousElementSibling(self: *parser.CharacterData) !?HTMLElem.Union {
61+
pub fn get_previousElementSibling(self: *parser.CharacterData) !?ElementUnion {
6162
const res = try parser.nodePreviousElementSibling(parser.characterDataToNode(self));
6263
if (res == null) {
6364
return null;
6465
}
65-
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
66+
return try Element.toInterface(res.?);
6667
}
6768

6869
// Read/Write attributes

src/browser/dom/element.zig

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,23 @@ pub const Element = struct {
5555
};
5656

5757
pub fn toInterface(e: *parser.Element) !Union {
58-
return try HTMLElem.toInterface(Union, e);
59-
// SVGElement and MathML are not supported yet.
58+
return toInterfaceT(Union, e);
59+
}
60+
61+
pub fn toInterfaceT(comptime T: type, e: *parser.Element) !T {
62+
const tagname = try parser.elementGetTagName(e) orelse {
63+
// in case of null tagname, return the element as it.
64+
return .{ .Element = e };
65+
};
66+
67+
// TODO SVGElement and MathML are not supported yet.
68+
69+
const tag = parser.Tag.fromString(tagname) catch {
70+
// if the tag is invalid, we don't have an HTMLElement.
71+
return .{ .Element = e };
72+
};
73+
74+
return HTMLElem.toInterfaceFromTag(T, e, tag);
6075
}
6176

6277
// JS funcs
@@ -344,13 +359,13 @@ pub const Element = struct {
344359
pub fn get_previousElementSibling(self: *parser.Element) !?Union {
345360
const res = try parser.nodePreviousElementSibling(parser.elementToNode(self));
346361
if (res == null) return null;
347-
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
362+
return try toInterface(res.?);
348363
}
349364

350365
pub fn get_nextElementSibling(self: *parser.Element) !?Union {
351366
const res = try parser.nodeNextElementSibling(parser.elementToNode(self));
352367
if (res == null) return null;
353-
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
368+
return try toInterface(res.?);
354369
}
355370

356371
fn getElementById(self: *parser.Element, id: []const u8) !?*parser.Node {

src/browser/dom/node.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const EventTarget = @import("event_target.zig").EventTarget;
2929
const Attr = @import("attribute.zig").Attr;
3030
const CData = @import("character_data.zig");
3131
const Element = @import("element.zig").Element;
32+
const ElementUnion = @import("element.zig").Union;
3233
const NodeList = @import("nodelist.zig").NodeList;
3334
const Document = @import("document.zig").Document;
3435
const DocumentType = @import("document_type.zig").DocumentType;
@@ -40,7 +41,6 @@ const Walker = @import("walker.zig").WalkerDepthFirst;
4041

4142
// HTML
4243
const HTML = @import("../html/html.zig");
43-
const HTMLElem = @import("../html/elements.zig");
4444

4545
// Node interfaces
4646
pub const Interfaces = .{
@@ -67,7 +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(
70+
.element => try Element.toInterfaceT(
7171
Union,
7272
@as(*parser.Element, @ptrCast(node)),
7373
),
@@ -145,12 +145,12 @@ pub const Node = struct {
145145
return try Node.toInterface(res.?);
146146
}
147147

148-
pub fn get_parentElement(self: *parser.Node) !?HTMLElem.Union {
148+
pub fn get_parentElement(self: *parser.Node) !?ElementUnion {
149149
const res = try parser.nodeParentElement(self);
150150
if (res == null) {
151151
return null;
152152
}
153-
return try HTMLElem.toInterface(HTMLElem.Union, @as(*parser.Element, @ptrCast(res.?)));
153+
return try Element.toInterface(res.?);
154154
}
155155

156156
pub fn get_nodeName(self: *parser.Node) ![]const u8 {

src/browser/html/elements.zig

Lines changed: 3 additions & 8 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,
@@ -639,7 +640,7 @@ pub const HTMLImageElement = struct {
639640
pub const prototype = *HTMLImageElement;
640641

641642
pub fn constructor(width: ?u32, height: ?u32, page: *const Page) !*parser.Image {
642-
const element = try parser.documentCreateElement(parser.documentHTMLToDocument(page.window.document), "img");
643+
const element = try parser.documentCreateHTMLElement(parser.documentHTMLToDocument(page.window.document), "img");
643644
const image: *parser.Image = @ptrCast(element);
644645
if (width) |width_| try parser.imageSetWidth(image, width_);
645646
if (height) |height_| try parser.imageSetHeight(image, height_);
@@ -1108,13 +1109,7 @@ pub const HTMLVideoElement = struct {
11081109
pub const subtype = .node;
11091110
};
11101111

1111-
pub fn toInterface(comptime T: type, e: *parser.Element) !T {
1112-
const tagname = try parser.elementGetTagName(e) orelse {
1113-
// in case of null tagname, return an uknonwn HTMLElement.
1114-
return .{ .HTMLUnknownElement = @as(*parser.Unknown, @ptrCast(e)) };
1115-
};
1116-
const tag = try parser.Tag.fromString(tagname);
1117-
1112+
pub fn toInterfaceFromTag(comptime T: type, e: *parser.Element, tag: parser.Tag) !T {
11181113
return switch (tag) {
11191114
.abbr, .acronym, .address, .article, .aside, .b, .basefont, .bdi, .bdo, .bgsound, .big, .center, .cite, .code, .dd, .details, .dfn, .dt, .em, .figcaption, .figure, .footer, .header, .hgroup, .i, .isindex, .keygen, .kbd, .main, .mark, .marquee, .menu, .menuitem, .nav, .nobr, .noframes, .noscript, .rp, .rt, .ruby, .s, .samp, .section, .small, .spacer, .strike, .strong, .sub, .summary, .sup, .tt, .u, .wbr, ._var => .{ .HTMLElement = @as(*parser.ElementHTML, @ptrCast(e)) },
11201115
.a => .{ .HTMLAnchorElement = @as(*parser.Anchor, @ptrCast(e)) },

0 commit comments

Comments
 (0)