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/app/components/hello/root.zig | 6 +- src/app/root.zig | 2 +- src/z/App/root.zig | 86 ++++++++++--------- src/z/Component.zig | 174 ++++++++++++++++++++++---------------- src/z/html.zig | 105 ----------------------- src/z/html/Attribute.zig | 45 ++++++++++ src/z/html/Document.zig | 11 +++ src/z/html/Tag.zig | 53 ++++++++++++ src/z/html/element.zig | 30 +++++++ src/z/html/root.zig | 4 + src/z/root.zig | 2 +- 11 files changed, 292 insertions(+), 226 deletions(-) delete mode 100644 src/z/html.zig create mode 100644 src/z/html/Attribute.zig create mode 100644 src/z/html/Document.zig create mode 100644 src/z/html/Tag.zig create mode 100644 src/z/html/element.zig create mode 100644 src/z/html/root.zig (limited to 'src') diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig index 55bf2e6..c4a7732 100644 --- a/src/app/components/hello/root.zig +++ b/src/app/components/hello/root.zig @@ -5,11 +5,7 @@ const Self = @This(); pub const component: z.Component = .{ .name = "hello", - .html = .native( - .div, - .{ .onclick = "@clicked" }, - .content("click me"), - ), + .html = .node("div", .{ .onclick = "@clicked" }, &.{.text("click me")}), .css = @embedFile("index.css"), .link = @This(), }; diff --git a/src/app/root.zig b/src/app/root.zig index eefc374..8ed7f43 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -16,7 +16,7 @@ pub const app: z.App(.{ .components = &.{ @import("components/hello/root.zig").component, }, - .body = .native(.@"app-hello", .{}, .content("")), + .body = .node("app-hello", .{}, &.{}), .theme = @embedFile("theme.css"), }) = .init; diff --git a/src/z/App/root.zig b/src/z/App/root.zig index 42ef26f..8fce591 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -1,13 +1,14 @@ const builtin = @import("builtin"); const std = @import("std"); -const h = @import("../html.zig"); +const Element = @import("../root.zig").html.Element; +const Document = @import("../root.zig").html.Document; const Component = @import("../root.zig").Component; pub const Options = struct { name: []const u8, globals: []const Global, components: []const Component, - body: h.Element, + body: Element, theme: []const u8, pub const Global = struct { @@ -49,44 +50,51 @@ pub fn App(comptime options: Options) type { } }; } else { + const components: []const Element = (comp: { + var cs: []const Element = &.{}; + + for (options.components) |component| { + cs = cs ++ component.toElement(); + } + + break :comp cs; + }); + return struct { - html: []const u8 = h.Element.native(.html, .{}, .children(&.{ - .native(.head, .{}, .children(&.{ - .native(.title, .{}, .content(options.name)), - .native(.meta, .{ - .name = "viewport", - .content = "width=device-width, initial-scale=1.0", - }, .void), - .native(.style, .{}, .content(options.theme)), - })), - .native(.body, .{}, .children(&.{ - .native(.script, .{}, .content(@embedFile("object-store.js"))), - .native(.script, .{}, .content(reg: { - var register_code: []const u8 = ""; - - for (options.globals, 1..) |field, id| { - register_code = register_code ++ std.fmt.comptimePrint( - "ObjectStore.registerObject({}, {s});", - .{ id, field.name }, - ); - } - - break :reg register_code; - })), - .native(.script, .{}, .content(@embedFile("main.js"))), - .transparent(.children(comp: { - var cs: []const h.Element = &.{}; - - for (options.components) |component| { - const c: []const h.Element = &.{component.toElement()}; - cs = cs ++ c; - } - - break :comp cs; - })), - options.body, - })), - })).toDocument(), + const node: Document = .{ + .root = .node("html", .{}, &.{ + .node("head", .{}, &.{ + .node("title", .{}, &.{.text(options.name)}), + .void("meta", .{ + .name = "viewport", + .content = "width=device-width, initial-scale=1.0", + }), + .node("style", .{}, &.{.text(options.theme)}), + }), + .node( + "body", + .{}, + &[_]Element{ + .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), + .node("script", .{}, &.{.text(reg: { + var register_code: []const u8 = ""; + + for (options.globals, 1..) |field, id| { + register_code = register_code ++ std.fmt.comptimePrint( + "ObjectStore.registerObject({}, {s});", + .{ id, field.name }, + ); + } + + break :reg register_code; + })}), + .node("script", .{}, &.{.text(@embedFile("main.js"))}), + } ++ components ++ &[1]Element{options.body}, + ), + }), + }; + + html: []const u8 = std.fmt.comptimePrint("{f}", .{node}), pub const init: @This() = .{}; }; 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 }), + )}, + ), + }; } diff --git a/src/z/html.zig b/src/z/html.zig deleted file mode 100644 index 9a5b3a2..0000000 --- a/src/z/html.zig +++ /dev/null @@ -1,105 +0,0 @@ -pub const Element = struct { - const Self = @This(); - - pub const Content = union(enum) { - elements: []const Self, - string: []const u8, - void: void, - - pub fn content(string: []const u8) @This() { - return .{ .string = string }; - } - - pub fn children(elements: []const Self) @This() { - return .{ .elements = elements }; - } - }; - - pub const Attribute = struct { - name: []const u8, - value: []const u8, - }; - - tag: []const u8, - attributes: []const Attribute, - content: Content, - - fn attributesFrom(attributes: anytype) []const Attribute { - const T = @TypeOf(attributes); - const info = @typeInfo(T); - - if (info != .@"struct") { - @compileError("struct needed"); - } - - const fields = info.@"struct".fields; - - var attrs: []const Attribute = &.{}; - - for (fields) |field| { - const name = field.name; - const value = @field(attributes, name); - - const next_attr: []const Attribute = &.{.{ .name = name, .value = value }}; - attrs = attrs ++ next_attr; - } - - return attrs; - } - - pub fn native(name: @EnumLiteral(), attrs: anytype, content: Content) Self { - return .{ - .tag = @tagName(name), - .attributes = attributesFrom(attrs), - .content = content, - }; - } - - pub fn transparent(content: Content) Self { - return .{ - .tag = "", - .attributes = &.{}, - .content = content, - }; - } - - pub fn toHtml(comptime self: Self) []const u8 { - var string: []const u8 = string: { - if (self.tag.len == 0) { - break :string ""; - } - - var string: []const u8 = "<" ++ self.tag; - - inline for (self.attributes) |attr| { - string = string ++ " " ++ attr.name ++ "='" ++ attr.value ++ "'"; - } - - string = string ++ ">"; - - break :string string; - }; - - switch (self.content) { - .void => {}, - .string => |s| { - string = string ++ s ++ ""; - }, - .elements => |els| { - for (els) |el| { - string = string ++ el.toHtml(); - } - - string = string ++ ""; - }, - } - - string = string ++ if (self.tag.len == 0) "" else ""; - - return string; - } - - pub fn toDocument(comptime self: Self) []const u8 { - return "" ++ self.toHtml(); - } -}; diff --git a/src/z/html/Attribute.zig b/src/z/html/Attribute.zig new file mode 100644 index 0000000..1db49a0 --- /dev/null +++ b/src/z/html/Attribute.zig @@ -0,0 +1,45 @@ +const std = @import("std"); + +const Self = @This(); + +name: []const u8, +value: []const u8, + +pub fn from(attributes: anytype) []const Self { + const T = @TypeOf(attributes); + const info = @typeInfo(T); + + if (info != .@"struct") { + @compileError("struct needed"); + } + + const fields = info.@"struct".fields; + + var attrs: []const Self = &.{}; + + for (fields) |field| { + const name = field.name; + const value = @field(attributes, name); + + const next_attr: []const Self = &.{.{ .name = name, .value = value }}; + attrs = attrs ++ next_attr; + } + + return attrs; +} + +pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try writer.writeAll(self.name); + try writer.writeAll("=\""); + + var value: []const u8 = self.value; + + while (std.mem.findScalar(u8, value, '"')) |index| { + try writer.writeAll(value[0..index]); + try writer.writeAll("""); + value = value[index + 1..]; + } + + try writer.writeAll(value); + try writer.writeByte('"'); +} diff --git a/src/z/html/Document.zig b/src/z/html/Document.zig new file mode 100644 index 0000000..b1122e1 --- /dev/null +++ b/src/z/html/Document.zig @@ -0,0 +1,11 @@ +const std = @import("std"); + +const Element = @import("root.zig").Element; + +const Self = @This(); + +root: Element, + +pub fn format(self: Self, writer: *std.Io.Writer) !void { + try writer.print("{f}", .{self.root}); +} diff --git a/src/z/html/Tag.zig b/src/z/html/Tag.zig new file mode 100644 index 0000000..56644ec --- /dev/null +++ b/src/z/html/Tag.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const ctutils = @import("ctutils"); + +const mod = @import("root.zig"); +const Attribute = mod.Attribute; +const Element = mod.Element; + +const Self = @This(); + +pub const Content = union(enum) { + children: []const Element, + void: void, +}; + +name: []const u8, +attributes: []const Attribute = &.{}, +content: Content, + +pub fn tag(name: []const u8, attributes: anytype, children: []const Element) Self { + return .{ + .name = name, + .attributes = Attribute.from(attributes), + .content = .{ .children = children }, + }; +} + +pub fn @"void"(name: []const u8, attributes: anytype) Self { + return .{ + .name = name, + .attributes = Attribute.from(attributes), + .content = .void, + }; +} + +pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try writer.writeByte('<'); + try writer.writeAll(self.name); + + for (self.attributes) |attribute| { + try writer.writeByte(' '); + try attribute.format(writer); + } + + try writer.writeByte('>'); + + switch (self.content) { + .children => |children| { + for (children) |child| try child.format(writer); + try writer.print("", .{self.name}); + }, + .void => {}, + } +} diff --git a/src/z/html/element.zig b/src/z/html/element.zig new file mode 100644 index 0000000..739eb0b --- /dev/null +++ b/src/z/html/element.zig @@ -0,0 +1,30 @@ +const std = @import("std"); +const mod = @import("root.zig"); +const Attribute = mod.Attribute; +const Tag = mod.Tag; + +pub const Element = union(enum) { + const Self = @This(); + + tag: Tag, + string: []const u8, + + pub fn node(name: []const u8, attributes: anytype, children: []const Element) Self { + return .{ .tag = .tag(name, attributes, children) }; + } + + pub fn @"void"(name: []const u8, attribute: anytype) Self { + return .{ .tag = .void(name, attribute) }; + } + + pub fn text(content: []const u8) Self { + return .{ .string = content }; + } + + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try switch (self.*) { + .string => |content| writer.writeAll(content), + .tag => |n| n.format(writer), + }; + } +}; diff --git a/src/z/html/root.zig b/src/z/html/root.zig new file mode 100644 index 0000000..fe58e75 --- /dev/null +++ b/src/z/html/root.zig @@ -0,0 +1,4 @@ +pub const Attribute = @import("Attribute.zig"); +pub const Tag = @import("Tag.zig"); +pub const Element = @import("element.zig").Element; +pub const Document = @import("Document.zig"); diff --git a/src/z/root.zig b/src/z/root.zig index b61f3ba..321bde4 100644 --- a/src/z/root.zig +++ b/src/z/root.zig @@ -4,7 +4,7 @@ const std = @import("std"); pub const log = @import("log/root.zig"); pub const Env = @import("Env/root.zig"); pub const Object = @import("Object.zig"); -pub const html = @import("html.zig"); +pub const html = @import("html/root.zig"); pub const App = @import("App/root.zig").App; pub const Options = @import("App/root.zig").Options; -- cgit v1.2.3-70-g09d2