From 221ad4ebaa951506f4f97547f71a89c231f53f0a Mon Sep 17 00:00:00 2001 From: maragubot Date: Tue, 15 Sep 2026 20:13:42 +0200 Subject: [PATCH 1/4] Escape `Text` and `Textf` at render time `Text` and `Textf` return a `text` node that escapes as it renders through `writeEscaped`, like attribute values do, instead of escaping into a new string at construction. A page built and rendered once per request saves one allocation per text node that needs escaping; a tree rendered many times escapes on every render instead of once. `escapeString` had no callers left, so it is gone, and the internal test and fuzz target now pin `writeEscaped` against `text/template` through both an `io.StringWriter` and a writer that only has `Write`. See #352, #356 --- gomponents.go | 34 ++++++++++++---------- gomponents_internal_test.go | 58 +++++++++++++++++++++++++++++++------ 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/gomponents.go b/gomponents.go index d858c3f..bab676e 100644 --- a/gomponents.go +++ b/gomponents.go @@ -219,7 +219,7 @@ func booleanAttr(name string) Node { } // htmlEscapeSet holds the characters that need escaping, and htmlEscaper is what escapes -// them. Both are the six replacements [text/template.HTMLEscape] makes, and escapeString +// them. Both are the six replacements [text/template.HTMLEscape] makes, and writeEscaped // is required by test to agree with [text/template.HTMLEscapeString] on every input. var htmlEscapeSet = [256]bool{0: true, '\'': true, '"': true, '&': true, '<': true, '>': true} @@ -232,19 +232,12 @@ var htmlEscaper = strings.NewReplacer( ">", ">", ) -// escapeString escapes the characters that HTML gives meaning to, deciding whether there is -// anything to escape with a table built once rather than with [strings.ContainsAny], which -// rebuilds an ASCII bitmap from its constant cutset on every call. Strings with nothing to -// escape, which is nearly all of them, are returned untouched. -func escapeString(s string) string { - for i := 0; i < len(s); i++ { - if htmlEscapeSet[s[i]] { - return htmlEscaper.Replace(s) - } - } - return s -} - +// writeEscaped writes s to w with the characters that HTML gives meaning to escaped. It +// decides whether there is anything to escape with a table built once rather than with +// [strings.ContainsAny], which rebuilds an ASCII bitmap from its constant cutset on every +// call, and strings with nothing to escape, which is nearly all of them, are written as they +// are. When there is, an [io.StringWriter] gets the escaped runs streamed into it, and any +// other writer gets one write of the escaped string. func writeEscaped(w io.Writer, s string) (int, error) { for i := 0; i < len(s); i++ { if htmlEscapeSet[s[i]] { @@ -316,12 +309,12 @@ func (a attrFunc) String() string { // Text creates a text DOM [Node] that Renders the escaped string t. func Text(t string) Node { - return raw(escapeString(t)) + return text(t) } // Textf creates a text DOM [Node] that Renders the interpolated and escaped string format. func Textf(format string, a ...interface{}) Node { - return raw(escapeString(fmt.Sprintf(format, a...))) + return text(fmt.Sprintf(format, a...)) } // Compile-time check that [raw] implements [fmt.Stringer], [Node], and [nodeTypeDescriber]. @@ -332,6 +325,15 @@ var _ interface { } = raw("") // raw is a text DOM [Node] that just Renders the unescaped, underlying string. +// text is a string that escapes itself as it renders, so escaping happens against the +// writer instead of into a new string. +type text string + +func (t text) Render(w io.Writer) error { + _, err := writeEscaped(w, string(t)) + return err +} + type raw string func (r raw) Render(w io.Writer) error { diff --git a/gomponents_internal_test.go b/gomponents_internal_test.go index 1fbbdfd..689ba8f 100644 --- a/gomponents_internal_test.go +++ b/gomponents_internal_test.go @@ -1,6 +1,7 @@ package gomponents import ( + "bytes" "html/template" "strings" "testing" @@ -15,14 +16,39 @@ func TestRaw(t *testing.T) { }) } -func TestEscapeString(t *testing.T) { - // escapeString must agree with template.HTMLEscapeString on every input. It no longer - // calls it — it decides whether to escape and then escapes on its own — so this is what - // holds the two together. A false negative would pass markup through. +// writeOnlyBuffer is a [bytes.Buffer] without its WriteString method, so that writeEscaped +// takes the path for writers that only have Write. +type writeOnlyBuffer struct { + b *bytes.Buffer +} + +func (w writeOnlyBuffer) Write(p []byte) (int, error) { + return w.b.Write(p) +} + +func TestWriteEscaped(t *testing.T) { + // writeEscaped must agree with template.HTMLEscapeString on every input, through both + // the streaming path for an io.StringWriter and the one write for any other writer. It + // never calls it, so this is what holds the two together. A false negative would pass + // markup through. same := func(t *testing.T, s string) { t.Helper() - if got, want := escapeString(s), template.HTMLEscapeString(s); got != want { - t.Fatalf("escapeString(%q) = %q, template.HTMLEscapeString = %q", s, got, want) + want := template.HTMLEscapeString(s) + + var sb strings.Builder + if _, err := writeEscaped(&sb, s); err != nil { + t.Fatal(err) + } + if got := sb.String(); got != want { + t.Fatalf("writeEscaped(strings.Builder, %q) = %q, template.HTMLEscapeString = %q", s, got, want) + } + + var bb bytes.Buffer + if _, err := writeEscaped(writeOnlyBuffer{&bb}, s); err != nil { + t.Fatal(err) + } + if got := bb.String(); got != want { + t.Fatalf("writeEscaped(write-only, %q) = %q, template.HTMLEscapeString = %q", s, got, want) } } @@ -85,13 +111,27 @@ func TestEscapeString(t *testing.T) { }) } -func FuzzEscapeString(f *testing.F) { +func FuzzWriteEscaped(f *testing.F) { for _, s := range []string{"", "hat", "