2022-08-14 00:02:34 +05:30
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2022-01-07 06:48:52 +05:30
2022-08-14 00:02:34 +05:30
//go:generate go run invisible/generate.go -v -o ./invisible_gen.go
//go:generate go run ambiguous/generate.go -v -o ./ambiguous_gen.go ambiguous/ambiguous.json
2022-01-07 06:48:52 +05:30
package charset
import (
2023-12-17 20:08:54 +05:30
"html/template"
2022-01-07 06:48:52 +05:30
"io"
"strings"
2022-08-14 00:02:34 +05:30
"code.gitea.io/gitea/modules/log"
2023-12-17 20:08:54 +05:30
"code.gitea.io/gitea/modules/setting"
2022-08-14 00:02:34 +05:30
"code.gitea.io/gitea/modules/translation"
2022-01-07 06:48:52 +05:30
)
2022-08-14 00:02:34 +05:30
// RuneNBSP is the codepoint for NBSP
const RuneNBSP = 0xa0
2022-01-07 06:48:52 +05:30
2022-08-14 00:02:34 +05:30
// EscapeControlHTML escapes the unicode control sequences in a provided html document
2023-12-17 20:08:54 +05:30
func EscapeControlHTML ( html template . HTML , locale translation . Locale , allowed ... rune ) ( escaped * EscapeStatus , output template . HTML ) {
2022-01-07 06:48:52 +05:30
sb := & strings . Builder { }
2023-12-17 20:08:54 +05:30
escaped , _ = EscapeControlReader ( strings . NewReader ( string ( html ) ) , sb , locale , allowed ... ) // err has been handled in EscapeControlReader
return escaped , template . HTML ( sb . String ( ) )
2022-01-07 06:48:52 +05:30
}
2023-12-17 20:08:54 +05:30
// 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
2022-08-14 00:02:34 +05:30
func EscapeControlReader ( reader io . Reader , writer io . Writer , locale translation . Locale , allowed ... rune ) ( escaped * EscapeStatus , err error ) {
2023-12-17 20:08:54 +05:30
if ! setting . UI . AmbiguousUnicodeDetection {
_ , err = io . Copy ( writer , reader )
return & EscapeStatus { } , err
}
2022-08-14 00:02:34 +05:30
outputStream := & HTMLStreamerWriter { Writer : writer }
streamer := NewEscapeStreamer ( locale , outputStream , allowed ... ) . ( * escapeStreamer )
2022-01-07 06:48:52 +05:30
2022-08-14 00:02:34 +05:30
if err = StreamHTML ( reader , streamer ) ; err != nil {
streamer . escaped . HasError = true
log . Error ( "Error whilst escaping: %v" , err )
2022-01-07 06:48:52 +05:30
}
2022-08-14 00:02:34 +05:30
return streamer . escaped , err
2022-01-07 06:48:52 +05:30
}