From fe1ccd974eff971a97638f948e8e1bf2f27c209c Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Wed, 19 Apr 2023 12:57:54 +0200 Subject: [PATCH] Use LXB_TAG to determine HTMLElement kind Signed-off-by: Francis Bouvier --- src/html/document.zig | 144 +----------------------------------------- src/html/elements.zig | 80 +++++++++++++++++++++++ 2 files changed, 81 insertions(+), 143 deletions(-) diff --git a/src/html/document.zig b/src/html/document.zig index e2e60d7d..be2a9f0c 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -50,149 +50,7 @@ pub const HTMLDocument = struct { pub fn _createElement(self: HTMLDocument, tag_name: []const u8) E.HTMLElements { const base = parser.documentCreateElement(self.proto.base.?, tag_name); - - // TODO: order by probability instead of alphabetically - // TODO: this does not seems very efficient, do we have a better way? - if (std.mem.eql(u8, tag_name, "a")) { - return .{ .anchor = E.HTMLAnchorElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "area")) { - return .{ .area = E.HTMLAreaElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "audio")) { - return .{ .audio = E.HTMLAudioElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "br")) { - return .{ .br = E.HTMLBRElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "base")) { - return .{ .base = E.HTMLBaseElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "body")) { - return .{ .body = E.HTMLBodyElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "button")) { - return .{ .button = E.HTMLButtonElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "canvas")) { - return .{ .canvas = E.HTMLCanvasElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "dl")) { - return .{ .dlist = E.HTMLDListElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "dialog")) { - return .{ .dialog = E.HTMLDialogElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "data")) { - return .{ .data = E.HTMLDataElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "div")) { - return .{ .div = E.HTMLDivElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "embed")) { - return .{ .embed = E.HTMLEmbedElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "fieldset")) { - return .{ .fieldset = E.HTMLFieldSetElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "form")) { - return .{ .form = E.HTMLFormElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "frameset")) { - return .{ .frameset = E.HTMLFrameSetElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "hr")) { - return .{ .hr = E.HTMLHRElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "head")) { - return .{ .head = E.HTMLHeadElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h1")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h2")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h3")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h4")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h5")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "h6")) { - return .{ .heading = E.HTMLHeadingElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "html")) { - return .{ .html = E.HTMLHtmlElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "iframe")) { - return .{ .iframe = E.HTMLIFrameElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "img")) { - return .{ .img = E.HTMLImageElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "input")) { - return .{ .input = E.HTMLInputElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "li")) { - return .{ .li = E.HTMLLIElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "label")) { - return .{ .label = E.HTMLLabelElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "legend")) { - return .{ .legend = E.HTMLLegendElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "link")) { - return .{ .link = E.HTMLLinkElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "map")) { - return .{ .map = E.HTMLMapElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "meta")) { - return .{ .meta = E.HTMLMetaElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "meter")) { - return .{ .meter = E.HTMLMeterElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "ins")) { - return .{ .mod = E.HTMLModElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "del")) { - return .{ .mod = E.HTMLModElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "ol")) { - return .{ .olist = E.HTMLOListElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "object")) { - return .{ .object = E.HTMLObjectElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "optgroup")) { - return .{ .optgroup = E.HTMLOptGroupElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "option")) { - return .{ .option = E.HTMLOptionElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "output")) { - return .{ .output = E.HTMLOutputElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "p")) { - return .{ .paragraph = E.HTMLParagraphElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "picture")) { - return .{ .picture = E.HTMLPictureElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "pre")) { - return .{ .pre = E.HTMLPreElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "progress")) { - return .{ .progress = E.HTMLProgressElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "blockquote")) { - return .{ .quote = E.HTMLQuoteElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "q")) { - return .{ .quote = E.HTMLQuoteElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "script")) { - return .{ .script = E.HTMLScriptElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "select")) { - return .{ .select = E.HTMLSelectElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "source")) { - return .{ .source = E.HTMLSourceElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "span")) { - return .{ .span = E.HTMLSpanElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "style")) { - return .{ .style = E.HTMLStyleElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "table")) { - return .{ .table = E.HTMLTableElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "caption")) { - return .{ .tablecaption = E.HTMLTableCaptionElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "th")) { - return .{ .tablecell = E.HTMLTableCellElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "td")) { - return .{ .tablecell = E.HTMLTableCellElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "col")) { - return .{ .tablecol = E.HTMLTableColElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "tr")) { - return .{ .tablerow = E.HTMLTableRowElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "thead")) { - return .{ .tablesection = E.HTMLTableSectionElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "tbody")) { - return .{ .tablesection = E.HTMLTableSectionElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "tfoot")) { - return .{ .tablesection = E.HTMLTableSectionElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "template")) { - return .{ .template = E.HTMLTemplateElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "textarea")) { - return .{ .textarea = E.HTMLTextAreaElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "time")) { - return .{ .time = E.HTMLTimeElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "title")) { - return .{ .title = E.HTMLTitleElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "track")) { - return .{ .track = E.HTMLTrackElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "ul")) { - return .{ .ulist = E.HTMLUListElement.init(base) }; - } else if (std.mem.eql(u8, tag_name, "video")) { - return .{ .video = E.HTMLVideoElement.init(base) }; - } - return .{ .unknown = E.HTMLUnknownElement.init(base) }; + return E.ElementToHTMLElementInterface(base); } }; diff --git a/src/html/elements.zig b/src/html/elements.zig index 7d4db0d2..a8804a04 100644 --- a/src/html/elements.zig +++ b/src/html/elements.zig @@ -761,3 +761,83 @@ pub const HTMLVideoElement = struct { return .{ .proto = HTMLMediaElement.init(elem_base) }; } }; + +const c = @cImport({ + @cInclude("lexbor/html/html.h"); +}); + +pub fn ElementToHTMLElementInterface(base: *parser.Element) HTMLElements { + return switch (base.*.node.local_name) { + c.LXB_TAG_A => .{ .anchor = HTMLAnchorElement.init(base) }, + c.LXB_TAG_AREA => .{ .area = HTMLAreaElement.init(base) }, + c.LXB_TAG_AUDIO => .{ .audio = HTMLAudioElement.init(base) }, + c.LXB_TAG_BR => .{ .br = HTMLBRElement.init(base) }, + c.LXB_TAG_BASE => .{ .base = HTMLBaseElement.init(base) }, + c.LXB_TAG_BODY => .{ .body = HTMLBodyElement.init(base) }, + c.LXB_TAG_BUTTON => .{ .button = HTMLButtonElement.init(base) }, + c.LXB_TAG_CANVAS => .{ .canvas = HTMLCanvasElement.init(base) }, + c.LXB_TAG_DL => .{ .dlist = HTMLDListElement.init(base) }, + c.LXB_TAG_DIALOG => .{ .dialog = HTMLDialogElement.init(base) }, + c.LXB_TAG_DATA => .{ .data = HTMLDataElement.init(base) }, + c.LXB_TAG_DIV => .{ .div = HTMLDivElement.init(base) }, + c.LXB_TAG_EMBED => .{ .embed = HTMLEmbedElement.init(base) }, + c.LXB_TAG_FIELDSET => .{ .fieldset = HTMLFieldSetElement.init(base) }, + c.LXB_TAG_FORM => .{ .form = HTMLFormElement.init(base) }, + c.LXB_TAG_FRAMESET => .{ .frameset = HTMLFrameSetElement.init(base) }, + c.LXB_TAG_HR => .{ .hr = HTMLHRElement.init(base) }, + c.LXB_TAG_HEAD => .{ .head = HTMLHeadElement.init(base) }, + c.LXB_TAG_H1 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_H2 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_H3 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_H4 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_H5 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_H6 => .{ .heading = HTMLHeadingElement.init(base) }, + c.LXB_TAG_HTML => .{ .html = HTMLHtmlElement.init(base) }, + c.LXB_TAG_IFRAME => .{ .iframe = HTMLIFrameElement.init(base) }, + c.LXB_TAG_IMG => .{ .img = HTMLImageElement.init(base) }, + c.LXB_TAG_INPUT => .{ .input = HTMLInputElement.init(base) }, + c.LXB_TAG_LI => .{ .li = HTMLLIElement.init(base) }, + c.LXB_TAG_LABEL => .{ .label = HTMLLabelElement.init(base) }, + c.LXB_TAG_LEGEND => .{ .legend = HTMLLegendElement.init(base) }, + c.LXB_TAG_LINK => .{ .link = HTMLLinkElement.init(base) }, + c.LXB_TAG_MAP => .{ .map = HTMLMapElement.init(base) }, + c.LXB_TAG_META => .{ .meta = HTMLMetaElement.init(base) }, + c.LXB_TAG_METER => .{ .meter = HTMLMeterElement.init(base) }, + c.LXB_TAG_INS => .{ .mod = HTMLModElement.init(base) }, + c.LXB_TAG_DEL => .{ .mod = HTMLModElement.init(base) }, + c.LXB_TAG_OL => .{ .olist = HTMLOListElement.init(base) }, + c.LXB_TAG_OBJECT => .{ .object = HTMLObjectElement.init(base) }, + c.LXB_TAG_OPTGROUP => .{ .optgroup = HTMLOptGroupElement.init(base) }, + c.LXB_TAG_OPTION => .{ .option = HTMLOptionElement.init(base) }, + c.LXB_TAG_OUTPUT => .{ .output = HTMLOutputElement.init(base) }, + c.LXB_TAG_P => .{ .paragraph = HTMLParagraphElement.init(base) }, + c.LXB_TAG_PICTURE => .{ .picture = HTMLPictureElement.init(base) }, + c.LXB_TAG_PRE => .{ .pre = HTMLPreElement.init(base) }, + c.LXB_TAG_PROGRESS => .{ .progress = HTMLProgressElement.init(base) }, + c.LXB_TAG_BLOCKQUOTE => .{ .quote = HTMLQuoteElement.init(base) }, + c.LXB_TAG_Q => .{ .quote = HTMLQuoteElement.init(base) }, + c.LXB_TAG_SCRIPT => .{ .script = HTMLScriptElement.init(base) }, + c.LXB_TAG_SELECT => .{ .select = HTMLSelectElement.init(base) }, + c.LXB_TAG_SOURCE => .{ .source = HTMLSourceElement.init(base) }, + c.LXB_TAG_SPAN => .{ .span = HTMLSpanElement.init(base) }, + c.LXB_TAG_STYLE => .{ .style = HTMLStyleElement.init(base) }, + c.LXB_TAG_TABLE => .{ .table = HTMLTableElement.init(base) }, + c.LXB_TAG_CAPTION => .{ .tablecaption = HTMLTableCaptionElement.init(base) }, + c.LXB_TAG_TH => .{ .tablecell = HTMLTableCellElement.init(base) }, + c.LXB_TAG_TD => .{ .tablecell = HTMLTableCellElement.init(base) }, + c.LXB_TAG_COL => .{ .tablecol = HTMLTableColElement.init(base) }, + c.LXB_TAG_TR => .{ .tablerow = HTMLTableRowElement.init(base) }, + c.LXB_TAG_THEAD => .{ .tablesection = HTMLTableSectionElement.init(base) }, + c.LXB_TAG_TBODY => .{ .tablesection = HTMLTableSectionElement.init(base) }, + c.LXB_TAG_TFOOT => .{ .tablesection = HTMLTableSectionElement.init(base) }, + c.LXB_TAG_TEMPLATE => .{ .template = HTMLTemplateElement.init(base) }, + c.LXB_TAG_TEXTAREA => .{ .textarea = HTMLTextAreaElement.init(base) }, + c.LXB_TAG_TIME => .{ .time = HTMLTimeElement.init(base) }, + c.LXB_TAG_TITLE => .{ .title = HTMLTitleElement.init(base) }, + c.LXB_TAG_TRACK => .{ .track = HTMLTrackElement.init(base) }, + c.LXB_TAG_UL => .{ .ulist = HTMLUListElement.init(base) }, + c.LXB_TAG_VIDEO => .{ .video = HTMLVideoElement.init(base) }, + c.LXB_TAG__UNDEF => .{ .unknown = HTMLUnknownElement.init(base) }, + else => .{ .unknown = HTMLUnknownElement.init(base) }, + }; +}