From e0eee4515677c03feccae5ac3716f073ca5f5725 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Fri, 2 Jun 2023 16:54:57 +0200 Subject: [PATCH] Applies changes from jsruntime Self and mem_guaranteed Signed-off-by: Francis Bouvier --- src/dom/document.zig | 35 +-- src/dom/element.zig | 16 +- src/dom/event_target.zig | 11 +- src/dom/node.zig | 16 +- src/generate.zig | 24 +- src/html/document.zig | 43 +-- src/html/elements.zig | 630 ++++++++++++--------------------------- src/parser.zig | 72 ++++- src/run_tests.zig | 9 +- 9 files changed, 318 insertions(+), 538 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index de782fe8..2d5558c3 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -6,30 +6,20 @@ const Node = @import("node.zig").Node; const Element = @import("element.zig").Element; pub const Document = struct { - proto: Node, - base: ?*parser.Document, - + pub const Self = parser.Document; pub const prototype = *Node; + pub const mem_guarantied = true; - pub fn init(base: ?*parser.Document) Document { - return .{ - .proto = Node.init(null), - .base = base, - }; - } + // pub fn constructor() *parser.Document { + // // TODO + // return .{}; + // } - pub fn constructor() Document { - return Document.init(null); - } - - pub fn getElementById(self: Document, elem_dom: *parser.Element, id: []const u8) ?Element { - if (self.base == null) { - return null; - } - const collection = parser.collectionInit(self.base.?, 1); + pub fn getElementById(self: *parser.Document, elem: *parser.Element, id: []const u8) ?*parser.Element { + const collection = parser.collectionInit(self, 1); defer parser.collectionDeinit(collection); const case_sensitve = true; - parser.elementsByAttr(elem_dom, collection, "id", id, case_sensitve) catch |err| { + parser.elementsByAttr(elem, collection, "id", id, case_sensitve) catch |err| { std.debug.print("getElementById error: {s}\n", .{@errorName(err)}); return null; }; @@ -37,19 +27,18 @@ pub const Document = struct { // no results return null; } - const element_base = parser.collectionElement(collection, 0); - return Element.init(element_base); + return parser.collectionElement(collection, 0); } // JS funcs // -------- - pub fn get_body(_: Document) ?void { + pub fn get_body(_: *parser.Document) ?*parser.Body { // TODO return null; } - pub fn _getElementById(_: Document, _: []u8) ?Element { + pub fn _getElementById(_: *parser.Document, _: []u8) ?*parser.Element { // TODO return null; } diff --git a/src/dom/element.zig b/src/dom/element.zig index 72a4b8d0..27e851a2 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -5,22 +5,14 @@ const parser = @import("../parser.zig"); const Node = @import("node.zig").Node; pub const Element = struct { - proto: Node, - base: *parser.Element, - + pub const Self = parser.Element; pub const prototype = *Node; - - pub fn init(base: *parser.Element) Element { - return .{ - .proto = Node.init(null), - .base = base, - }; - } + pub const mem_guarantied = true; // JS funcs // -------- - pub fn get_localName(self: Element) []const u8 { - return parser.elementLocalName(self.base); + pub fn get_localName(self: *parser.Element) []const u8 { + return parser.elementLocalName(self); } }; diff --git a/src/dom/event_target.zig b/src/dom/event_target.zig index a423cb81..2e125aab 100644 --- a/src/dom/event_target.zig +++ b/src/dom/event_target.zig @@ -1,13 +1,6 @@ const parser = @import("../parser.zig"); pub const EventTarget = struct { - base: ?*parser.EventTarget = null, - - pub fn init(base: ?*parser.EventTarget) EventTarget { - return .{ .base = base }; - } - - pub fn constructor() EventTarget { - return .{}; - } + pub const Self = parser.EventTarget; + pub const mem_guarantied = true; }; diff --git a/src/dom/node.zig b/src/dom/node.zig index ef732071..6ad69380 100644 --- a/src/dom/node.zig +++ b/src/dom/node.zig @@ -18,19 +18,11 @@ pub fn create_tree(node: ?*parser.Node, _: ?*anyopaque) callconv(.C) parser.Acti } pub const Node = struct { - proto: EventTarget, - base: ?*parser.Node = null, - + pub const Self = parser.Node; pub const prototype = *EventTarget; + pub const mem_guarantied = true; - pub fn init(base: ?*parser.Node) Node { - return .{ .proto = EventTarget.init(null), .base = base }; - } - - pub fn make_tree(self: Node) !void { - if (self.base) |node| { - try parser.nodeWalk(node, create_tree); - } - return error.NodeParserNull; + pub fn make_tree(self: *parser.Node) !void { + try parser.nodeWalk(self, create_tree); } }; diff --git a/src/generate.zig b/src/generate.zig index 2c95b562..d5e7fe1d 100644 --- a/src/generate.zig +++ b/src/generate.zig @@ -54,7 +54,7 @@ pub const Union = struct { } else if (members_nb < 16) { tag_type = u4; } else if (members_nb < 32) { - tag_type = u4; + tag_type = u5; } else if (members_nb < 64) { tag_type = u6; } else if (members_nb < 128) { @@ -109,19 +109,31 @@ pub const Union = struct { if (member_info == .Union) { const member_union = member_info.Union; for (member_union.fields) |field| { + var T: type = undefined; + if (@hasDecl(field.field_type, "Self")) { + T = @field(field.field_type, "Self"); + T = *T; + } else { + T = field.field_type; + } union_fields[done] = .{ .name = fmtName(field.field_type), - .field_type = field.field_type, - .alignment = field.alignment, + .field_type = T, + .alignment = @alignOf(T), }; done += 1; } } else if (member_info == .Struct) { - const alignment = tuple_info.Struct.fields[i].alignment; + const member_name = try itoa(i); + var T = @field(tuple, member_name); + if (@hasDecl(T, "Self")) { + T = @field(T, "Self"); + T = *T; + } union_fields[done] = .{ .name = fmtName(member_T), - .field_type = member_T, - .alignment = alignment, + .field_type = T, + .alignment = @alignOf(T), }; done += 1; } diff --git a/src/html/document.zig b/src/html/document.zig index deee09c3..41b6003b 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -11,45 +11,28 @@ const Document = @import("../dom/document.zig").Document; const E = @import("elements.zig"); pub const HTMLDocument = struct { - proto: Document, - base: *parser.DocumentHTML, - + pub const Self = parser.DocumentHTML; pub const prototype = *Document; - - pub fn init() HTMLDocument { - return .{ - .proto = Document.init(null), - .base = parser.documentHTMLInit(), - }; - } - - pub fn deinit(self: HTMLDocument) void { - parser.documentHTMLDeinit(self.base); - } - - pub fn parse(self: *HTMLDocument, html: []const u8) !void { - try parser.documentHTMLParse(self.base, html); - self.proto.base = parser.documentHTMLToDocument(self.base); - } + pub const mem_guarantied = true; // JS funcs // -------- - pub fn get_body(self: HTMLDocument) ?E.HTMLBodyElement { - const body_dom = parser.documentHTMLBody(self.base); - return E.HTMLBodyElement.init(body_dom); + pub fn get_body(self: *parser.DocumentHTML) ?*parser.Body { + return parser.documentHTMLBody(self); } - pub fn _getElementById(self: HTMLDocument, id: []u8) ?E.HTMLElement { - const body_dom = parser.documentHTMLBody(self.base); - if (self.proto.getElementById(body_dom, id)) |elem| { - return E.HTMLElement.init(elem.base); - } - return null; + pub fn _getElementById(self: *parser.DocumentHTML, id: []u8) ?*parser.HTMLElement { + const body_html = parser.documentHTMLBody(self); + const body_dom = @ptrCast(*parser.Element, body_html); + const doc_dom = @ptrCast(*parser.Document, self); + const elem_dom = Document.getElementById(doc_dom, body_dom, id); + return @ptrCast(*parser.HTMLElement, elem_dom); } - pub fn _createElement(self: HTMLDocument, tag_name: []const u8) E.HTMLElements { - const base = parser.documentCreateElement(self.proto.base.?, tag_name); + pub fn _createElement(self: *parser.DocumentHTML, tag_name: []const u8) E.HTMLElements { + const doc_dom = parser.documentHTMLToDocument(self); + const base = parser.documentCreateElement(doc_dom, tag_name); return E.ElementToHTMLElementInterface(base); } }; diff --git a/src/html/elements.zig b/src/html/elements.zig index 19a5f72e..a0a38209 100644 --- a/src/html/elements.zig +++ b/src/html/elements.zig @@ -7,13 +7,9 @@ const Element = @import("../dom/element.zig").Element; // -------------- pub const HTMLElement = struct { - proto: Element, - + pub const Self = parser.HTMLElement; pub const prototype = *Element; - - pub fn init(elem_base: *parser.Element) HTMLElement { - return .{ .proto = Element.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLElementsTypes = .{ @@ -27,8 +23,8 @@ pub const HTMLElementsTypes = .{ HTMLButtonElement, HTMLCanvasElement, HTMLDListElement, - HTMLDialogElement, HTMLDataElement, + HTMLDialogElement, HTMLDivElement, HTMLEmbedElement, HTMLFieldSetElement, @@ -90,680 +86,436 @@ pub const HTMLElementsTags = HTMLElementsGenerated._enum; // -------------------- pub const HTMLMediaElement = struct { - proto: HTMLElement, - + pub const Self = parser.MediaElement; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLMediaElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; // HTML elements // ------------- pub const HTMLUnknownElement = struct { - proto: HTMLElement, - + pub const Self = parser.Unknown; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLUnknownElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLAnchorElement = struct { - proto: HTMLElement, - + pub const Self = parser.Anchor; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLAnchorElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLAreaElement = struct { - proto: HTMLElement, - + pub const Self = parser.Area; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLAreaElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLAudioElement = struct { - proto: HTMLMediaElement, - + pub const Self = parser.Audio; pub const prototype = *HTMLMediaElement; - - pub fn init(elem_base: *parser.Element) HTMLAudioElement { - return .{ .proto = HTMLMediaElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLBRElement = struct { - proto: HTMLElement, - + pub const Self = parser.BR; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLBRElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLBaseElement = struct { - proto: HTMLElement, - + pub const Self = parser.Base; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLBaseElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLBodyElement = struct { - proto: HTMLElement, - + pub const Self = parser.Body; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLBodyElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLButtonElement = struct { - proto: HTMLElement, - + pub const Self = parser.Button; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLButtonElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLCanvasElement = struct { - proto: HTMLElement, - + pub const Self = parser.Canvas; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLCanvasElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLDListElement = struct { - proto: HTMLElement, - + pub const Self = parser.DList; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLDListElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } -}; - -pub const HTMLDialogElement = struct { - proto: HTMLElement, - - pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLDialogElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLDataElement = struct { - proto: HTMLElement, - + pub const Self = parser.Data; pub const prototype = *HTMLElement; + pub const mem_guarantied = true; +}; - pub fn init(elem_base: *parser.Element) HTMLDataElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } +pub const HTMLDialogElement = struct { + pub const Self = parser.Dialog; + pub const prototype = *HTMLElement; + pub const mem_guarantied = true; }; pub const HTMLDivElement = struct { - proto: HTMLElement, - + pub const Self = parser.Div; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLDivElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLEmbedElement = struct { - proto: HTMLElement, - + pub const Self = parser.Embed; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLEmbedElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLFieldSetElement = struct { - proto: HTMLElement, - + pub const Self = parser.FieldSet; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLFieldSetElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLFormElement = struct { - proto: HTMLElement, - + pub const Self = parser.Form; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLFormElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLFrameSetElement = struct { - proto: HTMLElement, - + pub const Self = parser.FrameSet; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLFrameSetElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLHRElement = struct { - proto: HTMLElement, - + pub const Self = parser.HR; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLHRElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLHeadElement = struct { - proto: HTMLElement, - + pub const Self = parser.Head; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLHeadElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLHeadingElement = struct { - proto: HTMLElement, - + pub const Self = parser.Heading; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLHeadingElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLHtmlElement = struct { - proto: HTMLElement, - + pub const Self = parser.Html; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLHtmlElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLIFrameElement = struct { - proto: HTMLElement, - + pub const Self = parser.IFrame; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLIFrameElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLImageElement = struct { - proto: HTMLElement, - + pub const Self = parser.Image; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLImageElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLInputElement = struct { - proto: HTMLElement, - + pub const Self = parser.Input; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLInputElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLLIElement = struct { - proto: HTMLElement, - + pub const Self = parser.LI; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLLIElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLLabelElement = struct { - proto: HTMLElement, - + pub const Self = parser.Label; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLLabelElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLLegendElement = struct { - proto: HTMLElement, - + pub const Self = parser.Legend; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLLegendElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLLinkElement = struct { - proto: HTMLElement, - + pub const Self = parser.Link; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLLinkElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLMapElement = struct { - proto: HTMLElement, - + pub const Self = parser.Map; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLMapElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLMetaElement = struct { - proto: HTMLElement, - + pub const Self = parser.Meta; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLMetaElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLMeterElement = struct { - proto: HTMLElement, - + pub const Self = parser.Meter; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLMeterElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLModElement = struct { - proto: HTMLElement, - + pub const Self = parser.Mod; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLModElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLOListElement = struct { - proto: HTMLElement, - + pub const Self = parser.OList; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLOListElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLObjectElement = struct { - proto: HTMLElement, - + pub const Self = parser.Object; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLObjectElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLOptGroupElement = struct { - proto: HTMLElement, - + pub const Self = parser.OptGroup; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLOptGroupElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLOptionElement = struct { - proto: HTMLElement, - + pub const Self = parser.Option; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLOptionElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLOutputElement = struct { - proto: HTMLElement, - + pub const Self = parser.Output; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLOutputElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLParagraphElement = struct { - proto: HTMLElement, - + pub const Self = parser.Paragraph; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLParagraphElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLPictureElement = struct { - proto: HTMLElement, - + pub const Self = parser.Picture; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLPictureElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLPreElement = struct { - proto: HTMLElement, - + pub const Self = parser.Pre; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLPreElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLProgressElement = struct { - proto: HTMLElement, - + pub const Self = parser.Progress; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLProgressElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLQuoteElement = struct { - proto: HTMLElement, - + pub const Self = parser.Quote; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLQuoteElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLScriptElement = struct { - proto: HTMLElement, - + pub const Self = parser.Script; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLScriptElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLSelectElement = struct { - proto: HTMLElement, - + pub const Self = parser.Select; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLSelectElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLSourceElement = struct { - proto: HTMLElement, - + pub const Self = parser.Source; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLSourceElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLSpanElement = struct { - proto: HTMLElement, - + pub const Self = parser.Span; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLSpanElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLStyleElement = struct { - proto: HTMLElement, - + pub const Self = parser.Style; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLStyleElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableElement = struct { - proto: HTMLElement, - + pub const Self = parser.Table; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableCaptionElement = struct { - proto: HTMLElement, - + pub const Self = parser.TableCaption; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableCaptionElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableCellElement = struct { - proto: HTMLElement, - + pub const Self = parser.TableCell; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableCellElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableColElement = struct { - proto: HTMLElement, - + pub const Self = parser.TableCol; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableColElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableRowElement = struct { - proto: HTMLElement, - + pub const Self = parser.TableRow; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableRowElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTableSectionElement = struct { - proto: HTMLElement, - + pub const Self = parser.TableSection; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTableSectionElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTemplateElement = struct { - proto: HTMLElement, - + pub const Self = parser.Template; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTemplateElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTextAreaElement = struct { - proto: HTMLElement, - + pub const Self = parser.TextArea; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTextAreaElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTimeElement = struct { - proto: HTMLElement, - + pub const Self = parser.Time; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTimeElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTitleElement = struct { - proto: HTMLElement, - + pub const Self = parser.Title; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTitleElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLTrackElement = struct { - proto: HTMLElement, - + pub const Self = parser.Track; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLTrackElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLUListElement = struct { - proto: HTMLElement, - + pub const Self = parser.UList; pub const prototype = *HTMLElement; - - pub fn init(elem_base: *parser.Element) HTMLUListElement { - return .{ .proto = HTMLElement.init(elem_base) }; - } + pub const mem_guarantied = true; }; pub const HTMLVideoElement = struct { - proto: HTMLMediaElement, - - pub const prototype = *HTMLMediaElement; - - pub fn init(elem_base: *parser.Element) HTMLVideoElement { - return .{ .proto = HTMLMediaElement.init(elem_base) }; - } + pub const Self = parser.Video; + pub const prototype = *HTMLElement; + pub const mem_guarantied = true; }; -pub fn ElementToHTMLElementInterface(base: *parser.Element) HTMLElements { - const tag = parser.nodeTag(parser.elementNode(base)); +pub fn ElementToHTMLElementInterface(elem: *parser.Element) HTMLElements { + const tag = parser.nodeTag(parser.elementNode(elem)); return switch (tag) { - .a => .{ .HTMLAnchorElement = HTMLAnchorElement.init(base) }, - .area => .{ .HTMLAreaElement = HTMLAreaElement.init(base) }, - .audio => .{ .HTMLAudioElement = HTMLAudioElement.init(base) }, - .br => .{ .HTMLBRElement = HTMLBRElement.init(base) }, - .base => .{ .HTMLBaseElement = HTMLBaseElement.init(base) }, - .body => .{ .HTMLBodyElement = HTMLBodyElement.init(base) }, - .button => .{ .HTMLButtonElement = HTMLButtonElement.init(base) }, - .canvas => .{ .HTMLCanvasElement = HTMLCanvasElement.init(base) }, - .dl => .{ .HTMLDListElement = HTMLDListElement.init(base) }, - .dialog => .{ .HTMLDialogElement = HTMLDialogElement.init(base) }, - .data => .{ .HTMLDataElement = HTMLDataElement.init(base) }, - .div => .{ .HTMLDivElement = HTMLDivElement.init(base) }, - .embed => .{ .HTMLEmbedElement = HTMLEmbedElement.init(base) }, - .fieldset => .{ .HTMLFieldSetElement = HTMLFieldSetElement.init(base) }, - .form => .{ .HTMLFormElement = HTMLFormElement.init(base) }, - .frameset => .{ .HTMLFrameSetElement = HTMLFrameSetElement.init(base) }, - .hr => .{ .HTMLHRElement = HTMLHRElement.init(base) }, - .head => .{ .HTMLHeadElement = HTMLHeadElement.init(base) }, - .h1, .h2, .h3, .h4, .h5, .h6 => .{ .HTMLHeadingElement = HTMLHeadingElement.init(base) }, - .html => .{ .HTMLHtmlElement = HTMLHtmlElement.init(base) }, - .iframe => .{ .HTMLIFrameElement = HTMLIFrameElement.init(base) }, - .img => .{ .HTMLImageElement = HTMLImageElement.init(base) }, - .input => .{ .HTMLInputElement = HTMLInputElement.init(base) }, - .li => .{ .HTMLLIElement = HTMLLIElement.init(base) }, - .label => .{ .HTMLLabelElement = HTMLLabelElement.init(base) }, - .legend => .{ .HTMLLegendElement = HTMLLegendElement.init(base) }, - .link => .{ .HTMLLinkElement = HTMLLinkElement.init(base) }, - .map => .{ .HTMLMapElement = HTMLMapElement.init(base) }, - .meta => .{ .HTMLMetaElement = HTMLMetaElement.init(base) }, - .meter => .{ .HTMLMeterElement = HTMLMeterElement.init(base) }, - .ins, .del => .{ .HTMLModElement = HTMLModElement.init(base) }, - .ol => .{ .HTMLOListElement = HTMLOListElement.init(base) }, - .object => .{ .HTMLObjectElement = HTMLObjectElement.init(base) }, - .optgroup => .{ .HTMLOptGroupElement = HTMLOptGroupElement.init(base) }, - .option => .{ .HTMLOptionElement = HTMLOptionElement.init(base) }, - .output => .{ .HTMLOutputElement = HTMLOutputElement.init(base) }, - .p => .{ .HTMLParagraphElement = HTMLParagraphElement.init(base) }, - .picture => .{ .HTMLPictureElement = HTMLPictureElement.init(base) }, - .pre => .{ .HTMLPreElement = HTMLPreElement.init(base) }, - .progress => .{ .HTMLProgressElement = HTMLProgressElement.init(base) }, - .blockquote, .q => .{ .HTMLQuoteElement = HTMLQuoteElement.init(base) }, - .script => .{ .HTMLScriptElement = HTMLScriptElement.init(base) }, - .select => .{ .HTMLSelectElement = HTMLSelectElement.init(base) }, - .source => .{ .HTMLSourceElement = HTMLSourceElement.init(base) }, - .span => .{ .HTMLSpanElement = HTMLSpanElement.init(base) }, - .style => .{ .HTMLStyleElement = HTMLStyleElement.init(base) }, - .table => .{ .HTMLTableElement = HTMLTableElement.init(base) }, - .caption => .{ .HTMLTableCaptionElement = HTMLTableCaptionElement.init(base) }, - .th, .td => .{ .HTMLTableCellElement = HTMLTableCellElement.init(base) }, - .col => .{ .HTMLTableColElement = HTMLTableColElement.init(base) }, - .tr => .{ .HTMLTableRowElement = HTMLTableRowElement.init(base) }, - .thead, .tbody, .tfoot => .{ .HTMLTableSectionElement = HTMLTableSectionElement.init(base) }, - .template => .{ .HTMLTemplateElement = HTMLTemplateElement.init(base) }, - .textarea => .{ .HTMLTextAreaElement = HTMLTextAreaElement.init(base) }, - .time => .{ .HTMLTimeElement = HTMLTimeElement.init(base) }, - .title => .{ .HTMLTitleElement = HTMLTitleElement.init(base) }, - .track => .{ .HTMLTrackElement = HTMLTrackElement.init(base) }, - .ul => .{ .HTMLUListElement = HTMLUListElement.init(base) }, - .video => .{ .HTMLVideoElement = HTMLVideoElement.init(base) }, - .undef => .{ .HTMLUnknownElement = HTMLUnknownElement.init(base) }, + .a => .{ .HTMLAnchorElement = @ptrCast(*parser.Anchor, elem) }, + .area => .{ .HTMLAreaElement = @ptrCast(*parser.Area, elem) }, + .audio => .{ .HTMLAudioElement = @ptrCast(*parser.Audio, elem) }, + .br => .{ .HTMLBRElement = @ptrCast(*parser.BR, elem) }, + .base => .{ .HTMLBaseElement = @ptrCast(*parser.Base, elem) }, + .body => .{ .HTMLBodyElement = @ptrCast(*parser.Body, elem) }, + .button => .{ .HTMLButtonElement = @ptrCast(*parser.Button, elem) }, + .canvas => .{ .HTMLCanvasElement = @ptrCast(*parser.Canvas, elem) }, + .dl => .{ .HTMLDListElement = @ptrCast(*parser.DList, elem) }, + .data => .{ .HTMLDataElement = @ptrCast(*parser.Data, elem) }, + .dialog => .{ .HTMLDialogElement = @ptrCast(*parser.Dialog, elem) }, + .div => .{ .HTMLDivElement = @ptrCast(*parser.Div, elem) }, + .embed => .{ .HTMLEmbedElement = @ptrCast(*parser.Embed, elem) }, + .fieldset => .{ .HTMLFieldSetElement = @ptrCast(*parser.FieldSet, elem) }, + .form => .{ .HTMLFormElement = @ptrCast(*parser.Form, elem) }, + .frameset => .{ .HTMLFrameSetElement = @ptrCast(*parser.FrameSet, elem) }, + .hr => .{ .HTMLHRElement = @ptrCast(*parser.HR, elem) }, + .head => .{ .HTMLHeadElement = @ptrCast(*parser.Head, elem) }, + .h1, .h2, .h3, .h4, .h5, .h6 => .{ .HTMLHeadingElement = @ptrCast(*parser.Heading, elem) }, + .html => .{ .HTMLHtmlElement = @ptrCast(*parser.Html, elem) }, + .iframe => .{ .HTMLIFrameElement = @ptrCast(*parser.IFrame, elem) }, + .img => .{ .HTMLImageElement = @ptrCast(*parser.Image, elem) }, + .input => .{ .HTMLInputElement = @ptrCast(*parser.Input, elem) }, + .li => .{ .HTMLLIElement = @ptrCast(*parser.LI, elem) }, + .label => .{ .HTMLLabelElement = @ptrCast(*parser.Label, elem) }, + .legend => .{ .HTMLLegendElement = @ptrCast(*parser.Legend, elem) }, + .link => .{ .HTMLLinkElement = @ptrCast(*parser.Link, elem) }, + .map => .{ .HTMLMapElement = @ptrCast(*parser.Map, elem) }, + .meta => .{ .HTMLMetaElement = @ptrCast(*parser.Meta, elem) }, + .meter => .{ .HTMLMeterElement = @ptrCast(*parser.Meter, elem) }, + .ins, .del => .{ .HTMLModElement = @ptrCast(*parser.Mod, elem) }, + .ol => .{ .HTMLOListElement = @ptrCast(*parser.OList, elem) }, + .object => .{ .HTMLObjectElement = @ptrCast(*parser.Object, elem) }, + .optgroup => .{ .HTMLOptGroupElement = @ptrCast(*parser.OptGroup, elem) }, + .option => .{ .HTMLOptionElement = @ptrCast(*parser.Option, elem) }, + .output => .{ .HTMLOutputElement = @ptrCast(*parser.Output, elem) }, + .p => .{ .HTMLParagraphElement = @ptrCast(*parser.Paragraph, elem) }, + .picture => .{ .HTMLPictureElement = @ptrCast(*parser.Picture, elem) }, + .pre => .{ .HTMLPreElement = @ptrCast(*parser.Pre, elem) }, + .progress => .{ .HTMLProgressElement = @ptrCast(*parser.Progress, elem) }, + .blockquote, .q => .{ .HTMLQuoteElement = @ptrCast(*parser.Quote, elem) }, + .script => .{ .HTMLScriptElement = @ptrCast(*parser.Script, elem) }, + .select => .{ .HTMLSelectElement = @ptrCast(*parser.Select, elem) }, + .source => .{ .HTMLSourceElement = @ptrCast(*parser.Source, elem) }, + .span => .{ .HTMLSpanElement = @ptrCast(*parser.Span, elem) }, + .style => .{ .HTMLStyleElement = @ptrCast(*parser.Style, elem) }, + .table => .{ .HTMLTableElement = @ptrCast(*parser.Table, elem) }, + .caption => .{ .HTMLTableCaptionElement = @ptrCast(*parser.TableCaption, elem) }, + .th, .td => .{ .HTMLTableCellElement = @ptrCast(*parser.TableCell, elem) }, + .col => .{ .HTMLTableColElement = @ptrCast(*parser.TableCol, elem) }, + .tr => .{ .HTMLTableRowElement = @ptrCast(*parser.TableRow, elem) }, + .thead, .tbody, .tfoot => .{ .HTMLTableSectionElement = @ptrCast(*parser.TableSection, elem) }, + .template => .{ .HTMLTemplateElement = @ptrCast(*parser.Template, elem) }, + .textarea => .{ .HTMLTextAreaElement = @ptrCast(*parser.TextArea, elem) }, + .time => .{ .HTMLTimeElement = @ptrCast(*parser.Time, elem) }, + .title => .{ .HTMLTitleElement = @ptrCast(*parser.Title, elem) }, + .track => .{ .HTMLTrackElement = @ptrCast(*parser.Track, elem) }, + .ul => .{ .HTMLUListElement = @ptrCast(*parser.UList, elem) }, + .video => .{ .HTMLVideoElement = @ptrCast(*parser.Video, elem) }, + .undef => .{ .HTMLUnknownElement = @ptrCast(*parser.Unknown, elem) }, }; } diff --git a/src/parser.zig b/src/parser.zig index f018a230..399d3ff2 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -125,7 +125,6 @@ pub const Tag = enum(u8) { fn elementName(comptime tag: Tag) []const u8 { return switch (tag) { - .area, .audio, .base, .body, .button, .br, .canvas, .dialog, .data, .div, .embed, .form, .head, .html, .hr, .input, .label, .li, .legend, .link, .map, .meta, .meter, .object, .option, .output, .picture, .pre, .progress, .script, .select, .source, .span, .style, .table, .template, .time, .title, .track, .video => upperName(@tagName(tag)), .a => "Anchor", .dl => "DList", .fieldset => "FieldSet", @@ -146,6 +145,7 @@ pub const Tag = enum(u8) { .textarea => "TextArea", .ul => "UList", .undef => "Unknown", + else => upperName(@tagName(tag)), }; } }; @@ -260,8 +260,8 @@ pub inline fn documentHTMLToDocument(document_html: *DocumentHTML) *Document { return &document_html.dom_document; } -pub inline fn documentHTMLBody(document_html: *DocumentHTML) *Element { - return c.lxb_dom_interface_element(document_html.body); +pub inline fn documentHTMLBody(document_html: *DocumentHTML) *Body { + return document_html.body; } // Document @@ -288,6 +288,72 @@ pub inline fn collectionElement(collection: *Collection, index: usize) *Element return c.lxb_dom_collection_element(collection, index); } +// HTML Elements + +pub const HTMLElement = c.lxb_html_element_t; +pub const MediaElement = c.lxb_html_media_element_t; + +pub const Unknown = c.lxb_html_unknown_element_t; +pub const Anchor = c.lxb_html_anchor_element_t; +pub const Area = c.lxb_html_area_element_t; +pub const Audio = c.lxb_html_audio_element_t; +pub const BR = c.lxb_html_br_element_t; +pub const Base = c.lxb_html_base_element_t; +pub const Body = c.lxb_html_body_element_t; +pub const Button = c.lxb_html_button_element_t; +pub const Canvas = c.lxb_html_canvas_element_t; +pub const DList = c.lxb_html_d_list_element_t; +pub const Data = c.lxb_html_data_element_t; +pub const Dialog = c.lxb_html_dialog_element_t; +pub const Div = c.lxb_html_div_element_t; +pub const Embed = c.lxb_html_embed_element_t; +pub const FieldSet = c.lxb_html_field_set_element_t; +pub const Form = c.lxb_html_form_element_t; +pub const FrameSet = c.lxb_html_frame_set_element_t; +pub const HR = c.lxb_html_hr_element_t; +pub const Head = c.lxb_html_head_element_t; +pub const Heading = c.lxb_html_heading_element_t; +pub const Html = c.lxb_html_html_element_t; +pub const IFrame = c.lxb_html_iframe_element_t; +pub const Image = c.lxb_html_image_element_t; +pub const Input = c.lxb_html_input_element_t; +pub const LI = c.lxb_html_li_element_t; +pub const Label = c.lxb_html_label_element_t; +pub const Legend = c.lxb_html_legend_element_t; +pub const Link = c.lxb_html_link_element_t; +pub const Map = c.lxb_html_map_element_t; +pub const Meta = c.lxb_html_meta_element_t; +pub const Meter = c.lxb_html_meter_element_t; +pub const Mod = c.lxb_html_mod_element_t; +pub const OList = c.lxb_html_o_list_element_t; +pub const Object = c.lxb_html_object_element_t; +pub const OptGroup = c.lxb_html_opt_group_element_t; +pub const Option = c.lxb_html_option_element_t; +pub const Output = c.lxb_html_output_element_t; +pub const Paragraph = c.lxb_html_paragraph_element_t; +pub const Picture = c.lxb_html_picture_element_t; +pub const Pre = c.lxb_html_pre_element_t; +pub const Progress = c.lxb_html_progress_element_t; +pub const Quote = c.lxb_html_quote_element_t; +pub const Script = c.lxb_html_script_element_t; +pub const Select = c.lxb_html_select_element_t; +pub const Source = c.lxb_html_source_element_t; +pub const Span = c.lxb_html_span_element_t; +pub const Style = c.lxb_html_style_element_t; +pub const Table = c.lxb_html_table_element_t; +pub const TableCaption = c.lxb_html_table_caption_element_t; +pub const TableCell = c.lxb_html_table_cell_element_t; +pub const TableCol = c.lxb_html_table_col_element_t; +pub const TableRow = c.lxb_html_table_row_element_t; +pub const TableSection = c.lxb_html_table_section_element_t; +pub const Template = c.lxb_html_template_element_t; +pub const TextArea = c.lxb_html_text_area_element_t; +pub const Time = c.lxb_html_time_element_t; +pub const Title = c.lxb_html_title_element_t; +pub const Track = c.lxb_html_track_element_t; +pub const UList = c.lxb_html_u_list_element_t; +pub const Video = c.lxb_html_video_element_t; + // Base pub const Action = c.lexbor_action_t; diff --git a/src/run_tests.zig b/src/run_tests.zig index 8fbac87b..ff4f51de 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -3,12 +3,13 @@ const std = @import("std"); const jsruntime = @import("jsruntime"); const generate = @import("generate.zig"); +const parser = @import("parser.zig"); const DOM = @import("dom.zig"); const testExecFn = @import("html/document.zig").testExecFn; const html_test = @import("html_test.zig").html; -var doc: DOM.HTMLDocument = undefined; +var doc: *parser.DocumentHTML = undefined; fn testsExecFn( alloc: std.mem.Allocator, @@ -37,9 +38,9 @@ test { const apis = jsruntime.compile(DOM.Interfaces); // document - doc = DOM.HTMLDocument.init(); - defer doc.deinit(); - try doc.parse(html_test); + doc = parser.documentHTMLInit(); + defer parser.documentHTMLDeinit(doc); + try parser.documentHTMLParse(doc, html_test); // create JS vm const vm = jsruntime.VM.init();