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 (
2022-12-18 01:52:25 +05:30
"bufio"
2022-01-07 06:48:52 +05:30
"io"
"strings"
2022-08-14 00:02:34 +05:30
"code.gitea.io/gitea/modules/log"
"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
func EscapeControlHTML ( text string , locale translation . Locale , allowed ... rune ) ( escaped * EscapeStatus , output string ) {
2022-01-07 06:48:52 +05:30
sb := & strings . Builder { }
2022-08-14 00:02:34 +05:30
outputStream := & HTMLStreamerWriter { Writer : sb }
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 ( strings . NewReader ( text ) , streamer ) ; err != nil {
streamer . escaped . HasError = true
log . Error ( "Error whilst escaping: %v" , err )
}
return streamer . escaped , sb . String ( )
2022-01-07 06:48:52 +05:30
}
2022-12-18 01:52:25 +05:30
// EscapeControlReaders escapes the unicode control sequences in a provided reader of HTML content and writer in a locale and returns the findings as an EscapeStatus and the escaped []byte
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 ) {
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
}
2023-02-02 10:21:02 +05:30
// EscapeControlStringReader escapes the unicode control sequences in a provided reader of string content and writer in a locale and returns the findings as an EscapeStatus and the escaped []byte. HTML line breaks are not inserted after every newline by this method.
2022-12-18 01:52:25 +05:30
func EscapeControlStringReader ( reader io . Reader , writer io . Writer , locale translation . Locale , allowed ... rune ) ( escaped * EscapeStatus , err error ) {
bufRd := bufio . NewReader ( reader )
outputStream := & HTMLStreamerWriter { Writer : writer }
streamer := NewEscapeStreamer ( locale , outputStream , allowed ... ) . ( * escapeStreamer )
for {
line , rdErr := bufRd . ReadString ( '\n' )
if len ( line ) > 0 {
if err := streamer . Text ( line ) ; err != nil {
streamer . escaped . HasError = true
log . Error ( "Error whilst escaping: %v" , err )
return streamer . escaped , err
}
}
if rdErr != nil {
if rdErr != io . EOF {
err = rdErr
}
break
}
}
return streamer . escaped , err
}
2022-08-14 00:02:34 +05:30
// EscapeControlString escapes the unicode control sequences in a provided string and returns the findings as an EscapeStatus and the escaped string
func EscapeControlString ( text string , locale translation . Locale , allowed ... rune ) ( escaped * EscapeStatus , output string ) {
sb := & strings . Builder { }
outputStream := & HTMLStreamerWriter { Writer : sb }
streamer := NewEscapeStreamer ( locale , outputStream , allowed ... ) . ( * escapeStreamer )
2022-01-07 06:48:52 +05:30
2022-08-14 00:02:34 +05:30
if err := streamer . Text ( text ) ; err != nil {
streamer . escaped . HasError = true
log . Error ( "Error whilst escaping: %v" , err )
}
return streamer . escaped , sb . String ( )
2022-01-07 06:48:52 +05:30
}