fix: retain wasm callback and normalize codegen class names
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b08a226202
commit
691d504f01
3 changed files with 31 additions and 8 deletions
|
|
@ -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 {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue