feat: add StripTags for render-reverse pipeline

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Claude 2026-02-17 00:08:44 +00:00
parent f49ddbf374
commit 8ac512362a
No known key found for this signature in database
GPG key ID: AF404715446AEB41
2 changed files with 79 additions and 0 deletions

30
pipeline.go Normal file
View file

@ -0,0 +1,30 @@
package html
import "strings"
// StripTags removes HTML tags from rendered output, returning plain text.
// Tag boundaries are replaced with a single space; result is trimmed.
func StripTags(html string) string {
var b strings.Builder
inTag := false
for _, r := range html {
if r == '<' {
inTag = true
b.WriteByte(' ')
continue
}
if r == '>' {
inTag = false
continue
}
if !inTag {
b.WriteRune(r)
}
}
// Collapse multiple spaces into one.
result := b.String()
for strings.Contains(result, " ") {
result = strings.ReplaceAll(result, " ", " ")
}
return strings.TrimSpace(result)
}

49
pipeline_test.go Normal file
View file

@ -0,0 +1,49 @@
package html
import "testing"
func TestStripTags_Simple(t *testing.T) {
got := StripTags(`<div>hello</div>`)
want := "hello"
if got != want {
t.Errorf("StripTags(<div>hello</div>) = %q, want %q", got, want)
}
}
func TestStripTags_Nested(t *testing.T) {
got := StripTags(`<header role="banner"><h1>Title</h1></header>`)
want := "Title"
if got != want {
t.Errorf("StripTags(nested) = %q, want %q", got, want)
}
}
func TestStripTags_MultipleRegions(t *testing.T) {
got := StripTags(`<header>Head</header><main>Body</main><footer>Foot</footer>`)
want := "Head Body Foot"
if got != want {
t.Errorf("StripTags(multi) = %q, want %q", got, want)
}
}
func TestStripTags_Empty(t *testing.T) {
got := StripTags("")
if got != "" {
t.Errorf("StripTags(\"\") = %q, want empty", got)
}
}
func TestStripTags_NoTags(t *testing.T) {
got := StripTags("plain text")
if got != "plain text" {
t.Errorf("StripTags(plain) = %q, want %q", got, "plain text")
}
}
func TestStripTags_Entities(t *testing.T) {
got := StripTags(`&lt;script&gt;`)
want := "&lt;script&gt;"
if got != want {
t.Errorf("StripTags should preserve entities, got %q, want %q", got, want)
}
}