Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/browser/dom/character_data.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const Node = @import("node.zig").Node;
const Comment = @import("comment.zig").Comment;
const Text = @import("text.zig");
const ProcessingInstruction = @import("processing_instruction.zig").ProcessingInstruction;
const HTMLElem = @import("../html/elements.zig");
const Element = @import("element.zig").Element;
const ElementUnion = @import("element.zig").Union;

// CharacterData interfaces
pub const Interfaces = .{
Expand All @@ -49,20 +50,20 @@ pub const CharacterData = struct {
return try parser.characterDataLength(self);
}

pub fn get_nextElementSibling(self: *parser.CharacterData) !?HTMLElem.Union {
pub fn get_nextElementSibling(self: *parser.CharacterData) !?ElementUnion {
const res = try parser.nodeNextElementSibling(parser.characterDataToNode(self));
if (res == null) {
return null;
}
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
return try Element.toInterface(res.?);
}

pub fn get_previousElementSibling(self: *parser.CharacterData) !?HTMLElem.Union {
pub fn get_previousElementSibling(self: *parser.CharacterData) !?ElementUnion {
const res = try parser.nodePreviousElementSibling(parser.characterDataToNode(self));
if (res == null) {
return null;
}
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
return try Element.toInterface(res.?);
}

// Read/Write attributes
Expand Down
6 changes: 2 additions & 4 deletions src/browser/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ pub const Document = struct {
return DOMImplementation{};
}

pub fn get_documentElement(self: *parser.Document) !?ElementUnion {
const e = try parser.documentGetDocumentElement(self);
if (e == null) return null;
return try Element.toInterface(e.?);
pub fn get_documentElement(self: *parser.Document) !?*parser.Element {
return try parser.documentGetDocumentElement(self);
}

pub fn get_documentURI(self: *parser.Document) ![]const u8 {
Expand Down
23 changes: 19 additions & 4 deletions src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@ pub const Element = struct {
};

pub fn toInterface(e: *parser.Element) !Union {
return try HTMLElem.toInterface(Union, e);
// SVGElement and MathML are not supported yet.
return toInterfaceT(Union, e);
}

pub fn toInterfaceT(comptime T: type, e: *parser.Element) !T {
const tagname = try parser.elementGetTagName(e) orelse {
// in case of null tagname, return the element as it.
return .{ .Element = e };
};

// TODO SVGElement and MathML are not supported yet.

const tag = parser.Tag.fromString(tagname) catch {
// if the tag is invalid, we don't have an HTMLElement.
return .{ .Element = e };
};

return HTMLElem.toInterfaceFromTag(T, e, tag);
}

// JS funcs
Expand Down Expand Up @@ -344,13 +359,13 @@ pub const Element = struct {
pub fn get_previousElementSibling(self: *parser.Element) !?Union {
const res = try parser.nodePreviousElementSibling(parser.elementToNode(self));
if (res == null) return null;
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
return try toInterface(res.?);
}

pub fn get_nextElementSibling(self: *parser.Element) !?Union {
const res = try parser.nodeNextElementSibling(parser.elementToNode(self));
if (res == null) return null;
return try HTMLElem.toInterface(HTMLElem.Union, res.?);
return try toInterface(res.?);
}

fn getElementById(self: *parser.Element, id: []const u8) !?*parser.Node {
Expand Down
8 changes: 4 additions & 4 deletions src/browser/dom/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const EventTarget = @import("event_target.zig").EventTarget;
const Attr = @import("attribute.zig").Attr;
const CData = @import("character_data.zig");
const Element = @import("element.zig").Element;
const ElementUnion = @import("element.zig").Union;
const NodeList = @import("nodelist.zig").NodeList;
const Document = @import("document.zig").Document;
const DocumentType = @import("document_type.zig").DocumentType;
Expand All @@ -40,7 +41,6 @@ const Walker = @import("walker.zig").WalkerDepthFirst;

// HTML
const HTML = @import("../html/html.zig");
const HTMLElem = @import("../html/elements.zig");

// Node interfaces
pub const Interfaces = .{
Expand All @@ -67,7 +67,7 @@ pub const Node = struct {

pub fn toInterface(node: *parser.Node) !Union {
return switch (try parser.nodeType(node)) {
.element => try HTMLElem.toInterface(
.element => try Element.toInterfaceT(
Union,
@as(*parser.Element, @ptrCast(node)),
),
Expand Down Expand Up @@ -145,12 +145,12 @@ pub const Node = struct {
return try Node.toInterface(res.?);
}

pub fn get_parentElement(self: *parser.Node) !?HTMLElem.Union {
pub fn get_parentElement(self: *parser.Node) !?ElementUnion {
const res = try parser.nodeParentElement(self);
if (res == null) {
return null;
}
return try HTMLElem.toInterface(HTMLElem.Union, @as(*parser.Element, @ptrCast(res.?)));
return try Element.toInterface(res.?);
}

pub fn get_nodeName(self: *parser.Node) ![]const u8 {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/dump.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn writeNode(node: *parser.Node, opts: Opts, writer: anytype) anyerror!void
switch (try parser.nodeType(node)) {
.element => {
// open the tag
const tag_type = try parser.elementHTMLGetTagType(@ptrCast(node));
const tag_type = try parser.nodeHTMLGetTagType(node) orelse .undef;
if (tag_type == .script and opts.exclude_scripts) {
return;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ pub fn writeChildren(root: *parser.Node, opts: Opts, writer: anytype) !void {
// area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr
// https://html.spec.whatwg.org/#void-elements
fn isVoid(elem: *parser.Element) !bool {
const tag = try parser.elementHTMLGetTagType(@as(*parser.ElementHTML, @ptrCast(elem)));
const tag = try parser.elementTag(elem);
return switch (tag) {
.area, .base, .br, .col, .embed, .hr, .img, .input, .link => true,
.meta, .source, .track, .wbr => true,
Expand Down
140 changes: 69 additions & 71 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");

// HTMLElement interfaces
pub const Interfaces = .{
Element,
HTMLElement,
HTMLUnknownElement,
HTMLAnchorElement,
Expand Down Expand Up @@ -1108,78 +1109,75 @@ pub const HTMLVideoElement = struct {
pub const subtype = .node;
};

pub fn toInterface(comptime T: type, e: *parser.Element) !T {
const elem: *align(@alignOf(*parser.Element)) parser.Element = @alignCast(e);
const tag = try parser.elementHTMLGetTagType(@as(*parser.ElementHTML, @ptrCast(elem)));

pub fn toInterfaceFromTag(comptime T: type, e: *parser.Element, tag: parser.Tag) !T {
return switch (tag) {
.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(elem)) },
.a => .{ .HTMLAnchorElement = @as(*parser.Anchor, @ptrCast(elem)) },
.applet => .{ .HTMLAppletElement = @as(*parser.Applet, @ptrCast(elem)) },
.area => .{ .HTMLAreaElement = @as(*parser.Area, @ptrCast(elem)) },
.audio => .{ .HTMLAudioElement = @as(*parser.Audio, @ptrCast(elem)) },
.base => .{ .HTMLBaseElement = @as(*parser.Base, @ptrCast(elem)) },
.body => .{ .HTMLBodyElement = @as(*parser.Body, @ptrCast(elem)) },
.br => .{ .HTMLBRElement = @as(*parser.BR, @ptrCast(elem)) },
.button => .{ .HTMLButtonElement = @as(*parser.Button, @ptrCast(elem)) },
.canvas => .{ .HTMLCanvasElement = @as(*parser.Canvas, @ptrCast(elem)) },
.dl => .{ .HTMLDListElement = @as(*parser.DList, @ptrCast(elem)) },
.data => .{ .HTMLDataElement = @as(*parser.Data, @ptrCast(elem)) },
.datalist => .{ .HTMLDataListElement = @as(*parser.DataList, @ptrCast(elem)) },
.dialog => .{ .HTMLDialogElement = @as(*parser.Dialog, @ptrCast(elem)) },
.dir => .{ .HTMLDirectoryElement = @as(*parser.Directory, @ptrCast(elem)) },
.div => .{ .HTMLDivElement = @as(*parser.Div, @ptrCast(elem)) },
.embed => .{ .HTMLEmbedElement = @as(*parser.Embed, @ptrCast(elem)) },
.fieldset => .{ .HTMLFieldSetElement = @as(*parser.FieldSet, @ptrCast(elem)) },
.font => .{ .HTMLFontElement = @as(*parser.Font, @ptrCast(elem)) },
.form => .{ .HTMLFormElement = @as(*parser.Form, @ptrCast(elem)) },
.frame => .{ .HTMLFrameElement = @as(*parser.Frame, @ptrCast(elem)) },
.frameset => .{ .HTMLFrameSetElement = @as(*parser.FrameSet, @ptrCast(elem)) },
.hr => .{ .HTMLHRElement = @as(*parser.HR, @ptrCast(elem)) },
.head => .{ .HTMLHeadElement = @as(*parser.Head, @ptrCast(elem)) },
.h1, .h2, .h3, .h4, .h5, .h6 => .{ .HTMLHeadingElement = @as(*parser.Heading, @ptrCast(elem)) },
.html => .{ .HTMLHtmlElement = @as(*parser.Html, @ptrCast(elem)) },
.iframe => .{ .HTMLIFrameElement = @as(*parser.IFrame, @ptrCast(elem)) },
.img => .{ .HTMLImageElement = @as(*parser.Image, @ptrCast(elem)) },
.input => .{ .HTMLInputElement = @as(*parser.Input, @ptrCast(elem)) },
.li => .{ .HTMLLIElement = @as(*parser.LI, @ptrCast(elem)) },
.label => .{ .HTMLLabelElement = @as(*parser.Label, @ptrCast(elem)) },
.legend => .{ .HTMLLegendElement = @as(*parser.Legend, @ptrCast(elem)) },
.link => .{ .HTMLLinkElement = @as(*parser.Link, @ptrCast(elem)) },
.map => .{ .HTMLMapElement = @as(*parser.Map, @ptrCast(elem)) },
.meta => .{ .HTMLMetaElement = @as(*parser.Meta, @ptrCast(elem)) },
.meter => .{ .HTMLMeterElement = @as(*parser.Meter, @ptrCast(elem)) },
.ins, .del => .{ .HTMLModElement = @as(*parser.Mod, @ptrCast(elem)) },
.ol => .{ .HTMLOListElement = @as(*parser.OList, @ptrCast(elem)) },
.object => .{ .HTMLObjectElement = @as(*parser.Object, @ptrCast(elem)) },
.optgroup => .{ .HTMLOptGroupElement = @as(*parser.OptGroup, @ptrCast(elem)) },
.option => .{ .HTMLOptionElement = @as(*parser.Option, @ptrCast(elem)) },
.output => .{ .HTMLOutputElement = @as(*parser.Output, @ptrCast(elem)) },
.p => .{ .HTMLParagraphElement = @as(*parser.Paragraph, @ptrCast(elem)) },
.param => .{ .HTMLParamElement = @as(*parser.Param, @ptrCast(elem)) },
.picture => .{ .HTMLPictureElement = @as(*parser.Picture, @ptrCast(elem)) },
.pre => .{ .HTMLPreElement = @as(*parser.Pre, @ptrCast(elem)) },
.progress => .{ .HTMLProgressElement = @as(*parser.Progress, @ptrCast(elem)) },
.blockquote, .q => .{ .HTMLQuoteElement = @as(*parser.Quote, @ptrCast(elem)) },
.script => .{ .HTMLScriptElement = @as(*parser.Script, @ptrCast(elem)) },
.select => .{ .HTMLSelectElement = @as(*parser.Select, @ptrCast(elem)) },
.source => .{ .HTMLSourceElement = @as(*parser.Source, @ptrCast(elem)) },
.span => .{ .HTMLSpanElement = @as(*parser.Span, @ptrCast(elem)) },
.style => .{ .HTMLStyleElement = @as(*parser.Style, @ptrCast(elem)) },
.table => .{ .HTMLTableElement = @as(*parser.Table, @ptrCast(elem)) },
.caption => .{ .HTMLTableCaptionElement = @as(*parser.TableCaption, @ptrCast(elem)) },
.th, .td => .{ .HTMLTableCellElement = @as(*parser.TableCell, @ptrCast(elem)) },
.col, .colgroup => .{ .HTMLTableColElement = @as(*parser.TableCol, @ptrCast(elem)) },
.tr => .{ .HTMLTableRowElement = @as(*parser.TableRow, @ptrCast(elem)) },
.thead, .tbody, .tfoot => .{ .HTMLTableSectionElement = @as(*parser.TableSection, @ptrCast(elem)) },
.template => .{ .HTMLTemplateElement = @as(*parser.Template, @ptrCast(elem)) },
.textarea => .{ .HTMLTextAreaElement = @as(*parser.TextArea, @ptrCast(elem)) },
.time => .{ .HTMLTimeElement = @as(*parser.Time, @ptrCast(elem)) },
.title => .{ .HTMLTitleElement = @as(*parser.Title, @ptrCast(elem)) },
.track => .{ .HTMLTrackElement = @as(*parser.Track, @ptrCast(elem)) },
.ul => .{ .HTMLUListElement = @as(*parser.UList, @ptrCast(elem)) },
.video => .{ .HTMLVideoElement = @as(*parser.Video, @ptrCast(elem)) },
.undef => .{ .HTMLUnknownElement = @as(*parser.Unknown, @ptrCast(elem)) },
.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)) },
.a => .{ .HTMLAnchorElement = @as(*parser.Anchor, @ptrCast(e)) },
.applet => .{ .HTMLAppletElement = @as(*parser.Applet, @ptrCast(e)) },
.area => .{ .HTMLAreaElement = @as(*parser.Area, @ptrCast(e)) },
.audio => .{ .HTMLAudioElement = @as(*parser.Audio, @ptrCast(e)) },
.base => .{ .HTMLBaseElement = @as(*parser.Base, @ptrCast(e)) },
.body => .{ .HTMLBodyElement = @as(*parser.Body, @ptrCast(e)) },
.br => .{ .HTMLBRElement = @as(*parser.BR, @ptrCast(e)) },
.button => .{ .HTMLButtonElement = @as(*parser.Button, @ptrCast(e)) },
.canvas => .{ .HTMLCanvasElement = @as(*parser.Canvas, @ptrCast(e)) },
.dl => .{ .HTMLDListElement = @as(*parser.DList, @ptrCast(e)) },
.data => .{ .HTMLDataElement = @as(*parser.Data, @ptrCast(e)) },
.datalist => .{ .HTMLDataListElement = @as(*parser.DataList, @ptrCast(e)) },
.dialog => .{ .HTMLDialogElement = @as(*parser.Dialog, @ptrCast(e)) },
.dir => .{ .HTMLDirectoryElement = @as(*parser.Directory, @ptrCast(e)) },
.div => .{ .HTMLDivElement = @as(*parser.Div, @ptrCast(e)) },
.embed => .{ .HTMLEmbedElement = @as(*parser.Embed, @ptrCast(e)) },
.fieldset => .{ .HTMLFieldSetElement = @as(*parser.FieldSet, @ptrCast(e)) },
.font => .{ .HTMLFontElement = @as(*parser.Font, @ptrCast(e)) },
.form => .{ .HTMLFormElement = @as(*parser.Form, @ptrCast(e)) },
.frame => .{ .HTMLFrameElement = @as(*parser.Frame, @ptrCast(e)) },
.frameset => .{ .HTMLFrameSetElement = @as(*parser.FrameSet, @ptrCast(e)) },
.hr => .{ .HTMLHRElement = @as(*parser.HR, @ptrCast(e)) },
.head => .{ .HTMLHeadElement = @as(*parser.Head, @ptrCast(e)) },
.h1, .h2, .h3, .h4, .h5, .h6 => .{ .HTMLHeadingElement = @as(*parser.Heading, @ptrCast(e)) },
.html => .{ .HTMLHtmlElement = @as(*parser.Html, @ptrCast(e)) },
.iframe => .{ .HTMLIFrameElement = @as(*parser.IFrame, @ptrCast(e)) },
.img => .{ .HTMLImageElement = @as(*parser.Image, @ptrCast(e)) },
.input => .{ .HTMLInputElement = @as(*parser.Input, @ptrCast(e)) },
.li => .{ .HTMLLIElement = @as(*parser.LI, @ptrCast(e)) },
.label => .{ .HTMLLabelElement = @as(*parser.Label, @ptrCast(e)) },
.legend => .{ .HTMLLegendElement = @as(*parser.Legend, @ptrCast(e)) },
.link => .{ .HTMLLinkElement = @as(*parser.Link, @ptrCast(e)) },
.map => .{ .HTMLMapElement = @as(*parser.Map, @ptrCast(e)) },
.meta => .{ .HTMLMetaElement = @as(*parser.Meta, @ptrCast(e)) },
.meter => .{ .HTMLMeterElement = @as(*parser.Meter, @ptrCast(e)) },
.ins, .del => .{ .HTMLModElement = @as(*parser.Mod, @ptrCast(e)) },
.ol => .{ .HTMLOListElement = @as(*parser.OList, @ptrCast(e)) },
.object => .{ .HTMLObjectElement = @as(*parser.Object, @ptrCast(e)) },
.optgroup => .{ .HTMLOptGroupElement = @as(*parser.OptGroup, @ptrCast(e)) },
.option => .{ .HTMLOptionElement = @as(*parser.Option, @ptrCast(e)) },
.output => .{ .HTMLOutputElement = @as(*parser.Output, @ptrCast(e)) },
.p => .{ .HTMLParagraphElement = @as(*parser.Paragraph, @ptrCast(e)) },
.param => .{ .HTMLParamElement = @as(*parser.Param, @ptrCast(e)) },
.picture => .{ .HTMLPictureElement = @as(*parser.Picture, @ptrCast(e)) },
.pre => .{ .HTMLPreElement = @as(*parser.Pre, @ptrCast(e)) },
.progress => .{ .HTMLProgressElement = @as(*parser.Progress, @ptrCast(e)) },
.blockquote, .q => .{ .HTMLQuoteElement = @as(*parser.Quote, @ptrCast(e)) },
.script => .{ .HTMLScriptElement = @as(*parser.Script, @ptrCast(e)) },
.select => .{ .HTMLSelectElement = @as(*parser.Select, @ptrCast(e)) },
.source => .{ .HTMLSourceElement = @as(*parser.Source, @ptrCast(e)) },
.span => .{ .HTMLSpanElement = @as(*parser.Span, @ptrCast(e)) },
.style => .{ .HTMLStyleElement = @as(*parser.Style, @ptrCast(e)) },
.table => .{ .HTMLTableElement = @as(*parser.Table, @ptrCast(e)) },
.caption => .{ .HTMLTableCaptionElement = @as(*parser.TableCaption, @ptrCast(e)) },
.th, .td => .{ .HTMLTableCellElement = @as(*parser.TableCell, @ptrCast(e)) },
.col, .colgroup => .{ .HTMLTableColElement = @as(*parser.TableCol, @ptrCast(e)) },
.tr => .{ .HTMLTableRowElement = @as(*parser.TableRow, @ptrCast(e)) },
.thead, .tbody, .tfoot => .{ .HTMLTableSectionElement = @as(*parser.TableSection, @ptrCast(e)) },
.template => .{ .HTMLTemplateElement = @as(*parser.Template, @ptrCast(e)) },
.textarea => .{ .HTMLTextAreaElement = @as(*parser.TextArea, @ptrCast(e)) },
.time => .{ .HTMLTimeElement = @as(*parser.Time, @ptrCast(e)) },
.title => .{ .HTMLTitleElement = @as(*parser.Title, @ptrCast(e)) },
.track => .{ .HTMLTrackElement = @as(*parser.Track, @ptrCast(e)) },
.ul => .{ .HTMLUListElement = @as(*parser.UList, @ptrCast(e)) },
.video => .{ .HTMLVideoElement = @as(*parser.Video, @ptrCast(e)) },
.undef => .{ .HTMLUnknownElement = @as(*parser.Unknown, @ptrCast(e)) },
};
}

Expand Down
Loading