1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-22 16:44:26 +00:00

Fix markdown color code detection (#30208)

When reviewing PRs, some color names might be mentioned, the
`transformCodeSpan` (which calls `css.ColorHandler`) considered it as a
valid color, but actually it shouldn't be rendered as a color codespan.
This commit is contained in:
wxiaoguang 2024-03-31 19:17:34 +08:00 committed by GitHub
parent 6d34ce25b1
commit ab028356c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 3 deletions

View File

@ -436,6 +436,10 @@ func TestColorPreview(t *testing.T) {
testcase string testcase string
expected string expected string
}{ }{
{ // do not render color names
"The CSS class `red` is there",
"<p>The CSS class <code>red</code> is there</p>\n",
},
{ // hex { // hex
"`#FF0000`", "`#FF0000`",
`<p><code>#FF0000<span class="color-preview" style="background-color: #FF0000"></span></code></p>` + nl, `<p><code>#FF0000<span class="color-preview" style="background-color: #FF0000"></span></code></p>` + nl,
@ -445,8 +449,8 @@ func TestColorPreview(t *testing.T) {
`<p><code>rgb(16, 32, 64)<span class="color-preview" style="background-color: rgb(16, 32, 64)"></span></code></p>` + nl, `<p><code>rgb(16, 32, 64)<span class="color-preview" style="background-color: rgb(16, 32, 64)"></span></code></p>` + nl,
}, },
{ // short hex { // short hex
"This is the color white `#000`", "This is the color white `#0a0`",
`<p>This is the color white <code>#000<span class="color-preview" style="background-color: #000"></span></code></p>` + nl, `<p>This is the color white <code>#0a0<span class="color-preview" style="background-color: #0a0"></span></code></p>` + nl,
}, },
{ // hsl { // hsl
"HSL stands for hue, saturation, and lightness. An example: `hsl(0, 100%, 50%)`.", "HSL stands for hue, saturation, and lightness. An example: `hsl(0, 100%, 50%)`.",

View File

@ -49,9 +49,28 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
// cssColorHandler checks if a string is a render-able CSS color value.
// The code is from "github.com/microcosm-cc/bluemonday/css.ColorHandler", except that it doesn't handle color words like "red".
func cssColorHandler(value string) bool {
value = strings.ToLower(value)
if css.HexRGB.MatchString(value) {
return true
}
if css.RGB.MatchString(value) {
return true
}
if css.RGBA.MatchString(value) {
return true
}
if css.HSL.MatchString(value) {
return true
}
return css.HSLA.MatchString(value)
}
func (g *ASTTransformer) transformCodeSpan(ctx *markup.RenderContext, v *ast.CodeSpan, reader text.Reader) { func (g *ASTTransformer) transformCodeSpan(ctx *markup.RenderContext, v *ast.CodeSpan, reader text.Reader) {
colorContent := v.Text(reader.Source()) colorContent := v.Text(reader.Source())
if css.ColorHandler(strings.ToLower(string(colorContent))) { if cssColorHandler(string(colorContent)) {
v.AppendChild(v, NewColorPreview(colorContent)) v.AppendChild(v, NewColorPreview(colorContent))
} }
} }