chore: fix various problems (#39298)

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
wxiaoguang
2026-09-14 22:15:21 +02:00
committed by GitHub
co-authored by Lunny Xiao
parent 85cbf477e5
commit c0ceea2f8f
14 changed files with 124 additions and 122 deletions
+12 -14
View File
@@ -28,23 +28,21 @@ func EscapeOptionsForView() EscapeOptions {
}
}
func EscapeControlHTMLTo(html template.HTML, locale translation.Locale, w htmlutil.HTMLWriter, opts ...EscapeOptions) *EscapeStatus {
if !setting.UI.AmbiguousUnicodeDetection {
w.WriteHTML(html)
return &EscapeStatus{}
}
escaped, _ := EscapeControlReader(strings.NewReader(string(html)), w.OriginWriter(), locale, opts...)
return escaped
}
// EscapeControlHTML escapes the Unicode control sequences in a provided html document
func EscapeControlHTML(html template.HTML, locale translation.Locale, opts ...EscapeOptions) (escaped *EscapeStatus, output template.HTML) {
sb, w := htmlutil.NewHTMLStringWriter()
escaped = EscapeControlHTMLTo(html, locale, w, opts...)
return escaped, template.HTML(sb.String())
if !setting.UI.AmbiguousUnicodeDetection {
return &EscapeStatus{}, html
}
w := &htmlutil.HTMLBuilder{}
escaped, _ = EscapeControlReader(strings.NewReader(string(html)), w, locale, opts...)
return escaped, w.HTMLString()
}
// EscapeControlReader escapes the Unicode control sequences in a provided reader of HTML content and writer in a locale and returns the findings as an EscapeStatus
func EscapeControlReader(reader io.Reader, writer io.Writer, locale translation.Locale, opts ...EscapeOptions) (*EscapeStatus, error) {
return escapeStream(locale, reader, writer, opts...)
func EscapeControlReader(reader io.Reader, writer htmlutil.HTMLWriter, locale translation.Locale, opts ...EscapeOptions) (*EscapeStatus, error) {
if !setting.UI.AmbiguousUnicodeDetection {
_, err := io.Copy(writer.OriginWriter(), reader)
return &EscapeStatus{}, err
}
return escapeStream(locale, reader, writer.OriginWriter(), opts...)
}
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"strings"
"testing"
"gitea.dev/modules/htmlutil"
"gitea.dev/modules/setting"
"gitea.dev/modules/test"
"gitea.dev/modules/translation"
@@ -145,7 +146,7 @@ then resh (ר), and finally heh (ה) (which should appear leftmost).`,
func TestEscapeControlReader(t *testing.T) {
for _, tt := range escapeControlTests {
t.Run(tt.name, func(t *testing.T) {
output := &strings.Builder{}
output := &htmlutil.HTMLBuilder{}
status, err := EscapeControlReader(strings.NewReader(tt.text), output, &translation.MockLocale{})
assert.NoError(t, err)
assert.Equal(t, tt.status, *status)
+25 -8
View File
@@ -90,18 +90,27 @@ func EscapeString(s string) template.HTML {
}
type HTMLWriter interface {
Err() error
OriginWriter() io.Writer
WriteString(s string) HTMLWriter
WriteHTML(s template.HTML) HTMLWriter
WriteFormatf(fmt template.HTML, args ...any) HTMLWriter
Err() error
}
var (
_ HTMLWriter = (*htmlWriter)(nil)
_ HTMLWriter = (*HTMLBuilder)(nil)
)
type htmlWriter struct {
w io.Writer
errs []error
}
func (h *htmlWriter) Err() error {
return errors.Join(h.errs...)
}
func (h *htmlWriter) OriginWriter() io.Writer {
return h.w
}
@@ -127,10 +136,6 @@ func (h *htmlWriter) WriteFormatf(fmt template.HTML, args ...any) HTMLWriter {
return h
}
func (h *htmlWriter) Err() error {
return errors.Join(h.errs...)
}
func NewHTMLWriter(w io.Writer) HTMLWriter {
return &htmlWriter{w: w}
}
@@ -144,17 +149,29 @@ type HTMLBuilder struct {
sb strings.Builder
}
func (b *HTMLBuilder) WriteString(s string) *HTMLBuilder {
func (b *HTMLBuilder) Err() error {
return nil
}
func (b *HTMLBuilder) OriginWriter() io.Writer {
return &b.sb
}
func (b *HTMLBuilder) Reset() {
b.sb.Reset()
}
func (b *HTMLBuilder) WriteString(s string) HTMLWriter {
b.sb.WriteString(template.HTMLEscapeString(s))
return b
}
func (b *HTMLBuilder) WriteHTML(s template.HTML) *HTMLBuilder {
func (b *HTMLBuilder) WriteHTML(s template.HTML) HTMLWriter {
b.sb.WriteString(string(s))
return b
}
func (b *HTMLBuilder) WriteFormatf(fmt template.HTML, args ...any) *HTMLBuilder {
func (b *HTMLBuilder) WriteFormatf(fmt template.HTML, args ...any) HTMLWriter {
_, _ = HTMLPrintf(&b.sb, fmt, args...)
return b
}
+8
View File
@@ -4,6 +4,7 @@
package util
import (
"iter"
"strings"
"unsafe"
)
@@ -131,3 +132,10 @@ func AsciiEqualFold(s, t string) bool {
}
return true
}
func StringSplitSeq[T, S ~string](s T, sep S) iter.Seq[T] {
f := strings.SplitSeq(string(s), string(sep))
return func(yield func(T) bool) {
f(func(v string) bool { return yield(T(v)) })
}
}