From ec30b659dfd337f5edf512ea78032fe18251b704 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Oct 2026 13:52:25 +0200 Subject: refactor html module --- src/z/Component.zig | 174 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 99 insertions(+), 75 deletions(-) (limited to 'src/z/Component.zig') diff --git a/src/z/Component.zig b/src/z/Component.zig index f3a1a99..ba328b6 100644 --- a/src/z/Component.zig +++ b/src/z/Component.zig @@ -1,5 +1,6 @@ const std = @import("std"); -const Element = @import("html.zig").Element; +const Element = @import("root.zig").html.Element; +const Attribute = @import("root.zig").html.Attribute; const Object = @import("Object.zig"); const Self = @This(); @@ -11,24 +12,34 @@ link: type, fn exportFromHtml(self: *const Self, element: Element) void { comptime { - for (element.attributes) |attr| { - if (attr.value.len > 0 and attr.value[0] == '@') { - const func = struct { - pub fn func(obj: *self.link) callconv(.c) void { - @field(self.link, attr.value[1..])(obj); - } - }.func; + switch (element) { + .tag => |tag| for (tag.attributes) |attr| { + if (attr.value.len > 0 and attr.value[0] == '@') { + const func = struct { + pub fn func(obj: *self.link) callconv(.c) void { + @field(self.link, attr.value[1..])(obj); + } + }.func; - @export( - &func, - .{ - .name = std.fmt.comptimePrint( - "export::{s}::{s}", - .{ self.name, attr.value[1..] }, - ), + @export( + &func, + .{ + .name = std.fmt.comptimePrint( + "export::{s}::{s}", + .{ self.name, attr.value[1..] }, + ), + }, + ); + } + + switch (tag.content) { + .children => |children| for (children) |child| { + self.exportFromHtml(child); }, - ); - } + else => {}, + } + }, + else => {}, } } } @@ -47,7 +58,7 @@ pub fn exportLinks(self: *const Self) void { .{ .name = std.fmt.comptimePrint( "export::{s}::init", - .{ self.name }, + .{self.name}, ), }, ); @@ -55,72 +66,85 @@ pub fn exportLinks(self: *const Self) void { comptime self.exportFromHtml(self.html); } +fn linkAttributes(self: *const Self, attributes: []const Attribute) []const Attribute { + var linked: []const Attribute = &.{}; + + for (attributes) |*attr| { + if (attr.value.len > 0 and attr.value[0] == '@') { + const attribute: []const Attribute = &.{.{ + .name = attr.name, + .value = std.fmt.comptimePrint( + "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])", + .{ self.name, attr.value[1..] }, + ), + }}; + linked = linked ++ attribute; + } + } + return linked; +} + fn linkHtmlRecursive(self: *const Self, element: Element) Element { comptime { - return .{ - .tag = element.tag, - .attributes = attrs: { - var attributes: []const Element.Attribute = &.{}; + return switch (element) { + .tag => |el| .{ + .tag = .{ + .name = el.name, + .attributes = self.linkAttributes(el.attributes), + .content = switch (el.content) { + .children => |children| ch: { + var elements: []const Element = &.{}; - for (element.attributes) |*attr| { - if (attr.value.len > 0 and attr.value[0] == '@') { - const attribute: []const Element.Attribute = &.{.{ - .name = attr.name, - .value = std.fmt.comptimePrint( - "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])", - .{ self.name, attr.value[1..] }, - ), - }}; - attributes = attributes ++ attribute; - } - } - break :attrs attributes; + for (children) |child| { + const e: []const Element = &.{self.linkHtmlRecursive(child)}; + elements = elements ++ e; + } + + break :ch .{ .children = elements }; + }, + .void => .void, + }, + }, }, - .content = element.content, + .string => element, }; } } -pub fn toElement(self: *const Self) Element { - return .transparent( - .children( +pub fn toElement(self: *const Self) []const Element { + return &.{ + .node( + "template", + .{ .id = "@" ++ self.name }, &.{ - .native( - .template, - .{ .id = "@" ++ self.name }, - .children(&.{ - .native(.style, .{}, .content(self.css)), - self.linkHtmlRecursive(self.html) - }), - ), - .native( - .script, - .{}, - .content( - std.fmt.comptimePrint( - \\customElements.define("app-{s}", class extends HTMLElement {{ - \\ constructor() {{ - \\ super(); - \\ this.attachShadow({{ mode: 'open' }}); - \\ const template = document.getElementById("@{s}"); - \\ const node = document.importNode(template.content, true); - \\ this.shadowRoot.appendChild(node); - \\ this['@ptr'] = null; - \\ }} - \\ connectedCallback() {{ - \\ wasm_ready.then(() => {{ - \\ const id = ObjectStore.generateFreeId(); - \\ ObjectStore.registerObject(id, this); - \\ this['@ptr'] = wasm.exports['export::{s}::init'](id) - \\ }}); - \\ }} - \\}}) - , - .{self.name, self.name, self.name} - ), - ), - ), + .node("style", .{}, &.{.text(self.css)}), + comptime self.linkHtmlRecursive(self.html), }, ), - ); + .node( + "script", + .{}, + &.{.text( + std.fmt.comptimePrint( + \\customElements.define("app-{s}", class extends HTMLElement {{ + \\ constructor() {{ + \\ super(); + \\ this.attachShadow({{ mode: 'open' }}); + \\ const template = document.getElementById("@{s}"); + \\ const node = document.importNode(template.content, true); + \\ this.shadowRoot.appendChild(node); + \\ this['@ptr'] = null; + \\ }} + \\ connectedCallback() {{ + \\ wasm_ready.then(() => {{ + \\ const id = ObjectStore.generateFreeId(); + \\ ObjectStore.registerObject(id, this); + \\ this['@ptr'] = wasm.exports['export::{s}::init'](id) + \\ }}); + \\ }} + \\}}) + , .{ self.name, self.name, self.name }), + )}, + ), + }; } -- cgit v1.2.3-70-g09d2