// Copyright (c) 2026 Lark Technologies Pte. Ltd. // SPDX-License-Identifier: MIT package draft import ( "strings" "testing" xhtml "golang.org/x/net/html" ) func TestPlainTextFromHTML(t *testing.T) { tests := []struct { name string html string want string }{ { name: "strips inline style tag", html: `

Hello

`, want: "Hello", }, { name: "strips script tag", html: `

Before

After

`, want: "Before\nAfter", }, { name: "strips noscript tag", html: `

Visible

`, want: "Visible", }, { name: "strips head and title", html: `Page TitleContent`, want: "Content", }, { name: "plain text passthrough", html: `
Line one
Line two
`, want: "Line one\nLine two", }, { name: "mixed non-text and text tags", html: `
Only this
`, want: "Only this", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := plainTextFromHTML(tt.html) if got != tt.want { t.Errorf("plainTextFromHTML() = %q, want %q", got, tt.want) } }) } } func TestPlainTextFromHTMLDeepNesting(t *testing.T) { // Build HTML with 10000 levels of nesting — would overflow the stack // with the old recursive implementation. const depth = 10_000 var b strings.Builder for i := 0; i < depth; i++ { b.WriteString("
") } b.WriteString("deep") for i := 0; i < depth; i++ { b.WriteString("
") } got := plainTextFromHTML(b.String()) if got != "deep" { t.Errorf("deep nesting: got %q, want %q", got, "deep") } } func TestIsHTMLNonTextTag(t *testing.T) { tests := []struct { tag string want bool }{ {"script", true}, {"style", true}, {"head", true}, {"meta", true}, {"noscript", true}, {"link", true}, {"title", true}, {"div", false}, {"p", false}, {"span", false}, } for _, tt := range tests { t.Run(tt.tag, func(t *testing.T) { n := &xhtml.Node{Type: xhtml.ElementNode, Data: tt.tag} if got := isHTMLNonTextTag(n); got != tt.want { t.Errorf("isHTMLNonTextTag(%q) = %v, want %v", tt.tag, got, tt.want) } }) } }