Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package charset
|
|
|
|
import (
|
|
"html/template"
|
|
"io"
|
|
"strings"
|
|
|
|
"gitea.dev/modules/htmlutil"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/translation"
|
|
)
|
|
|
|
type EscapeOptions struct {
|
|
Allowed map[rune]bool
|
|
}
|
|
|
|
func AllowRuneNBSP() map[rune]bool {
|
|
return map[rune]bool{0xa0: true}
|
|
}
|
|
|
|
func EscapeOptionsForView() EscapeOptions {
|
|
return EscapeOptions{
|
|
// it's safe to see NBSP in the view, but maybe not in the diff
|
|
Allowed: AllowRuneNBSP(),
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
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 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...)
|
|
}
|