From 905c0e9d470195e2437dd312e5b9606fd934d5d1 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 2 Apr 2026 22:32:24 +0800 Subject: [PATCH] rename var and add comment --- modules/highlight/highlight.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index cf3bc41a5f..b00de43c0b 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -23,10 +23,10 @@ import ( const sizeLimit = 1024 * 1024 type globalVarsType struct { - highlightMapping map[string]string - githubStyles *chroma.Style - escapeFull []template.HTML - escapeControlChars []template.HTML + highlightMapping map[string]string + githubStyles *chroma.Style + escapeFull []template.HTML + escCtrlCharsMap []template.HTML } var ( @@ -42,7 +42,7 @@ func globalVars() *globalVarsType { globalVarsPtr = &globalVarsType{} globalVarsPtr.githubStyles = styles.Get("github") globalVarsPtr.highlightMapping = setting.GetHighlightMapping() - globalVarsPtr.escapeControlChars = make([]template.HTML, 256) + globalVarsPtr.escCtrlCharsMap = make([]template.HTML, 256) // ASCII Table 0x00 - 0x1F controlCharNames := []string{ "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", @@ -54,14 +54,14 @@ func globalVars() *globalVarsType { // don't worry, even if you forget to comment it out and push it to git repo, the CI tests will catch it and fail // controlCharNames = append(controlCharNames, "SP") for i, s := range controlCharNames { - globalVarsPtr.escapeControlChars[i] = template.HTML(`` + string(byte(i)) + ``) + globalVarsPtr.escCtrlCharsMap[i] = template.HTML(`` + string(byte(i)) + ``) } - globalVarsPtr.escapeControlChars[0x7f] = template.HTML(`` + string(byte(0x7f)) + ``) - globalVarsPtr.escapeControlChars['\t'] = "" - globalVarsPtr.escapeControlChars['\n'] = "" - globalVarsPtr.escapeControlChars['\r'] = "" + globalVarsPtr.escCtrlCharsMap[0x7f] = template.HTML(`` + string(byte(0x7f)) + ``) + globalVarsPtr.escCtrlCharsMap['\t'] = "" + globalVarsPtr.escCtrlCharsMap['\n'] = "" + globalVarsPtr.escCtrlCharsMap['\r'] = "" - globalVarsPtr.escapeFull = slices.Clone(globalVarsPtr.escapeControlChars) + globalVarsPtr.escapeFull = slices.Clone(globalVarsPtr.escCtrlCharsMap) // exactly the same as Golang's html.EscapeString globalVarsPtr.escapeFull['&'] = "&" globalVarsPtr.escapeFull['\''] = "'" @@ -102,7 +102,7 @@ func escapeFullString(code string) template.HTML { } func escapeControlChars(code []byte) template.HTML { - return escapeByMap(code, globalVars().escapeControlChars) + return escapeByMap(code, globalVars().escCtrlCharsMap) } // UnsafeSplitHighlightedLines splits highlighted code into lines preserving HTML tags @@ -174,6 +174,11 @@ func RenderCodeByLexer(lexer chroma.Lexer, code string) template.HTML { log.Error("Can't format code: %v", err) return escapeFullString(code) } + + // At the moment, we do not escape control chars here (unlike RenderFullFile which escapes control chars). + // The reason is: it is a very rare case that a text file contains control chars. + // This function is usually used by highlight diff and blame, not quite sure whether there will be side effects. + // If there would be new user feedback about this, we can re-consider about various edge cases. return template.HTML(htmlBuf.String()) }