@@ -23,10 +23,10 @@ import (
2323const sizeLimit = 1024 * 1024
2424
2525type globalVarsType struct {
26- highlightMapping map [string ]string
27- githubStyles * chroma.Style
28- escapeFull []template.HTML
29- escapeControlChars []template.HTML
26+ highlightMapping map [string ]string
27+ githubStyles * chroma.Style
28+ escapeFull []template.HTML
29+ escCtrlCharsMap []template.HTML
3030}
3131
3232var (
@@ -42,7 +42,7 @@ func globalVars() *globalVarsType {
4242 globalVarsPtr = & globalVarsType {}
4343 globalVarsPtr .githubStyles = styles .Get ("github" )
4444 globalVarsPtr .highlightMapping = setting .GetHighlightMapping ()
45- globalVarsPtr .escapeControlChars = make ([]template.HTML , 256 )
45+ globalVarsPtr .escCtrlCharsMap = make ([]template.HTML , 256 )
4646 // ASCII Table 0x00 - 0x1F
4747 controlCharNames := []string {
4848 "NUL" , "SOH" , "STX" , "ETX" , "EOT" , "ENQ" , "ACK" , "BEL" ,
@@ -54,14 +54,14 @@ func globalVars() *globalVarsType {
5454 // 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
5555 // controlCharNames = append(controlCharNames, "SP")
5656 for i , s := range controlCharNames {
57- globalVarsPtr .escapeControlChars [i ] = template .HTML (`<span class="broken-code-point" data-escaped="` + s + `"><span class="char">` + string (byte (i )) + `</span></span>` )
57+ globalVarsPtr .escCtrlCharsMap [i ] = template .HTML (`<span class="broken-code-point" data-escaped="` + s + `"><span class="char">` + string (byte (i )) + `</span></span>` )
5858 }
59- globalVarsPtr .escapeControlChars [0x7f ] = template .HTML (`<span class="broken-code-point" data-escaped="DEL"><span class="char">` + string (byte (0x7f )) + `</span></span>` )
60- globalVarsPtr .escapeControlChars ['\t' ] = ""
61- globalVarsPtr .escapeControlChars ['\n' ] = ""
62- globalVarsPtr .escapeControlChars ['\r' ] = ""
59+ globalVarsPtr .escCtrlCharsMap [0x7f ] = template .HTML (`<span class="broken-code-point" data-escaped="DEL"><span class="char">` + string (byte (0x7f )) + `</span></span>` )
60+ globalVarsPtr .escCtrlCharsMap ['\t' ] = ""
61+ globalVarsPtr .escCtrlCharsMap ['\n' ] = ""
62+ globalVarsPtr .escCtrlCharsMap ['\r' ] = ""
6363
64- globalVarsPtr .escapeFull = slices .Clone (globalVarsPtr .escapeControlChars )
64+ globalVarsPtr .escapeFull = slices .Clone (globalVarsPtr .escCtrlCharsMap )
6565 // exactly the same as Golang's html.EscapeString
6666 globalVarsPtr .escapeFull ['&' ] = "&"
6767 globalVarsPtr .escapeFull ['\'' ] = "'"
@@ -102,7 +102,7 @@ func escapeFullString(code string) template.HTML {
102102}
103103
104104func escapeControlChars (code []byte ) template.HTML {
105- return escapeByMap (code , globalVars ().escapeControlChars )
105+ return escapeByMap (code , globalVars ().escCtrlCharsMap )
106106}
107107
108108// UnsafeSplitHighlightedLines splits highlighted code into lines preserving HTML tags
@@ -174,6 +174,11 @@ func RenderCodeByLexer(lexer chroma.Lexer, code string) template.HTML {
174174 log .Error ("Can't format code: %v" , err )
175175 return escapeFullString (code )
176176 }
177+
178+ // At the moment, we do not escape control chars here (unlike RenderFullFile which escapes control chars).
179+ // The reason is: it is a very rare case that a text file contains control chars.
180+ // This function is usually used by highlight diff and blame, not quite sure whether there will be side effects.
181+ // If there would be new user feedback about this, we can re-consider about various edge cases.
177182 return template .HTML (htmlBuf .String ())
178183}
179184
0 commit comments