From 691d504f01bb938c12777e05ff4a98002fd7df92 Mon Sep 17 00:00:00 2001 From: Snider Date: Wed, 15 Apr 2026 01:39:31 +0100 Subject: [PATCH] fix: retain wasm callback and normalize codegen class names Co-Authored-By: Virgil --- cmd/wasm/main.go | 11 ++++++++--- codegen/codegen.go | 25 ++++++++++++++++++++----- codegen/codegen_test.go | 3 +++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cmd/wasm/main.go b/cmd/wasm/main.go index dc74244..867964b 100644 --- a/cmd/wasm/main.go +++ b/cmd/wasm/main.go @@ -8,6 +8,9 @@ import ( html "dappco.re/go/core/html" ) +// Keep the callback alive for the lifetime of the WASM module. +var renderToStringFunc js.Func + // renderToString builds an HLCRF layout from JS arguments and returns HTML. // Slot content is injected via Raw() — the caller is responsible for sanitisation. // This is intentional: the WASM module is a rendering engine for trusted content @@ -55,9 +58,11 @@ func renderToString(_ js.Value, args []js.Value) any { } func main() { - js.Global().Set("gohtml", js.ValueOf(map[string]any{ - "renderToString": js.FuncOf(renderToString), - })) + renderToStringFunc = js.FuncOf(renderToString) + + api := js.Global().Get("Object").New() + api.Set("renderToString", renderToStringFunc) + js.Global().Set("gohtml", api) select {} } diff --git a/codegen/codegen.go b/codegen/codegen.go index 2fbcebf..b411b9e 100644 --- a/codegen/codegen.go +++ b/codegen/codegen.go @@ -83,14 +83,29 @@ func GenerateRegistration(tag, className string) string { return `customElements.define("` + tag + `", ` + className + `);` } -// TagToClassName converts a kebab-case tag to PascalCase class name. +// TagToClassName converts a custom element tag to PascalCase class name. // Usage example: className := TagToClassName("nav-bar") func TagToClassName(tag string) string { b := core.NewBuilder() - for _, p := range core.Split(tag, "-") { - if len(p) > 0 { - b.WriteString(core.Upper(p[:1])) - b.WriteString(p[1:]) + upperNext := true + for i := 0; i < len(tag); i++ { + ch := tag[i] + switch { + case ch >= 'a' && ch <= 'z': + if upperNext { + b.WriteByte(ch - ('a' - 'A')) + } else { + b.WriteByte(ch) + } + upperNext = false + case ch >= 'A' && ch <= 'Z': + b.WriteByte(ch) + upperNext = false + case ch >= '0' && ch <= '9': + b.WriteByte(ch) + upperNext = false + default: + upperNext = true } } return b.String() diff --git a/codegen/codegen_test.go b/codegen/codegen_test.go index 3fdcdc4..b4e3221 100644 --- a/codegen/codegen_test.go +++ b/codegen/codegen_test.go @@ -42,6 +42,9 @@ func TestTagToClassName_KebabCase_Good(t *testing.T) { {"photo-grid", "PhotoGrid"}, {"nav-breadcrumb", "NavBreadcrumb"}, {"my-super-widget", "MySuperWidget"}, + {"nav_bar", "NavBar"}, + {"nav.bar", "NavBar"}, + {"nav--bar", "NavBar"}, } for _, tt := range tests { got := TagToClassName(tt.tag)