From 23ac8d3e55049e87fa186a734b40dc24f37e21ee Mon Sep 17 00:00:00 2001 From: Matt Edmonston Date: Sat, 28 Nov 2026 06:52:49 +0000 Subject: [PATCH] Add RenderHTML and RenderHTMLPage RenderHTML serializes the browser renderer's vnode description of a View to the HTML the WASM build puts in #wui-root: same tags, classes and inline styles, values reflected as attributes (input value, textarea text, checked, selected option), sorted attributes for stable output, raw text for the container-query \n\n\n
") + writeHTML(&b, describeTree(el)) + b.WriteString("
\n\n\n") + return b.String() +} + +// voidElements have no closing tag. +var voidElements = map[string]bool{"input": true, "hr": true, "br": true, "img": true} + +// rawTextElements hold text that must not be entity-escaped. +var rawTextElements = map[string]bool{"style": true, "script": true} + +func writeHTML(b *strings.Builder, v *vnode) { + if v.tag == commentTag { + b.WriteString("") + return + } + + attrs := make(map[string]string, len(v.attrs)+1) + for k, val := range v.attrs { + attrs[k] = val + } + text, selected := v.text, "" + for _, p := range v.props { + switch { + case p.name == "value" && v.tag == "textarea": + text, _ = p.value.(string) + case p.name == "value" && v.tag == "select": + selected, _ = p.value.(string) + case p.name == "value": + if s, _ := p.value.(string); s != "" { + attrs["value"] = s + } + case p.name == "checked": + if on, _ := p.value.(bool); on { + attrs["checked"] = "" + } + } + } + + b.WriteByte('<') + b.WriteString(v.tag) + names := make([]string, 0, len(attrs)) + for k := range attrs { + names = append(names, k) + } + sort.Strings(names) + for _, k := range names { + b.WriteByte(' ') + b.WriteString(k) + if val := attrs[k]; val != "" { + b.WriteString(`="`) + b.WriteString(html.EscapeString(val)) + b.WriteByte('"') + } + } + b.WriteByte('>') + if voidElements[v.tag] { + return + } + + switch { + case len(v.kids) > 0: + for _, k := range v.kids { + if v.tag == "select" && k.tag == "option" && k.attrs["value"] == selected { + k = withAttr(k, "selected", "") + } + writeHTML(b, k) + } + case rawTextElements[v.tag]: + b.WriteString(strings.ReplaceAll(text, "') +} + +// withAttr returns a shallow copy of v with one more attribute. +func withAttr(v *vnode, name, value string) *vnode { + c := *v + c.attrs = make(map[string]string, len(v.attrs)+1) + for k, val := range v.attrs { + c.attrs[k] = val + } + c.attrs[name] = value + return &c +} diff --git a/render_html_test.go b/render_html_test.go new file mode 100644 index 0000000..7e5f0c9 --- /dev/null +++ b/render_html_test.go @@ -0,0 +1,76 @@ +package wui + +import ( + "strings" + "testing" +) + +func TestRenderHTMLElements(t *testing.T) { + nop := func() Msg { return nil } + tests := []struct { + name string + el Element + want string + }{ + {"text escaped", Text(`a < b & "c"`), `a < b & "c"`}, + {"styled text", Text("hi", WithTextStyle(NewStyle().SetBold(true).Classed("x"))), + `hi`}, + {"button", Button("Go", nop), ``}, + {"submit button", Button("Send", nil, Disabled()), ``}, + {"input value", Input("q", WithValue(`say "hi"`), WithPlaceholder("Search")), + ``}, + {"empty input value omitted", Input("q"), ``}, + {"textarea value as text", TextArea("t", WithAreaValue("aa<b`}, + {"checkbox checked", Checkbox("c", "Done", true, nil), + ``}, + {"select marks the selected option", Select("s", Options("a", "b"), "b", nil), + ``}, + {"empty placeholder", Box(Column, Empty()), + `
`}, + {"divider", Divider(""), `
`}, + {"link", Link("Docs", "https://x.test/?a=1&b=2"), + `Docs`}, + {"table", Table([]string{"A"}, [][]string{{"1"}}), + `
A
1
`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RenderHTML(tt.el); got != tt.want { + t.Errorf("RenderHTML =\n%s\nwant\n%s", got, tt.want) + } + }) + } +} + +func TestRenderHTMLResponsiveStyleIsRaw(t *testing.T) { + got := RenderHTML(Responsive(At(0, Text("a")), At(40, Text("b")))) + if !strings.Contains(got, "