diff --git a/README.md b/README.md index 2152f04..d7a0ed1 100644 --- a/README.md +++ b/README.md @@ -897,6 +897,28 @@ All bubbletea/lipgloss/bubbles code is behind `//go:build !js`. All `syscall/js` --- +## Testing views + +A View is a pure function of the model, so both renderings can be tested in plain `go test`, with no terminal or browser: + +```go +func TestEmptyState(t *testing.T) { + view := model{}.View() + + tui := wui.RenderTUISized(view, 60, 10) // what the terminal shows + if !strings.Contains(tui, "Nothing to do") { t.Fatal(tui) } + + html := wui.RenderHTML(view) // what the browser build puts in #wui-root + if !strings.Contains(html, ` placeholders, as they +// do in the live DOM. Attributes are written in sorted order, so the +// output is deterministic. +func RenderHTML(el Element) string { + var b strings.Builder + writeHTML(&b, describeTree(el)) + return b.String() +} + +// RenderHTMLPage renders a complete, standalone HTML document: the base +// stylesheet (dark theme) and el inside #wui-root. The page is static; +// it does not load the WASM build. +func RenderHTMLPage(title string, el Element) string { + var b strings.Builder + b.WriteString("\n\n\n\n") + b.WriteString("\n") + b.WriteString(html.EscapeString(title)) + b.WriteString("\n\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, "