aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-03-31 19:17:34 +0800
committerGitHub <noreply@github.com>2024-03-31 19:17:34 +0800
commitab028356c7f4f29adb99505c078c162a317c7c37 (patch)
tree67936cfb90c2a1acc3d0c491d01b9a53e15244c5 /modules
parent6d34ce25b16cdfd6e2e364aebe546e3c2fbb76c6 (diff)
downloadgitea-ab028356c7f4f29adb99505c078c162a317c7c37.tar.gz
gitea-ab028356c7f4f29adb99505c078c162a317c7c37.zip
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.
Diffstat (limited to 'modules')
-rw-r--r--modules/markup/markdown/markdown_test.go8
-rw-r--r--modules/markup/markdown/transform_codespan.go21
2 files changed, 26 insertions, 3 deletions
diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go
index ebac3fbe9e..c664758a27 100644
--- a/modules/markup/markdown/markdown_test.go
+++ b/modules/markup/markdown/markdown_test.go
@@ -436,6 +436,10 @@ func TestColorPreview(t *testing.T) {
testcase 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
"`#FF0000`",
`<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,
},
{ // short hex
- "This is the color white `#000`",
- `<p>This is the color white <code>#000<span class="color-preview" style="background-color: #000"></span></code></p>` + nl,
+ "This is the color white `#0a0`",
+ `<p>This is the color white <code>#0a0<span class="color-preview" style="background-color: #0a0"></span></code></p>` + nl,
},
{ // hsl
"HSL stands for hue, saturation, and lightness. An example: `hsl(0, 100%, 50%)`.",
diff --git a/modules/markup/markdown/transform_codespan.go b/modules/markup/markdown/transform_codespan.go
index bfff2897b0..5b07d72999 100644
--- a/modules/markup/markdown/transform_codespan.go
+++ b/modules/markup/markdown/transform_codespan.go
@@ -49,9 +49,28 @@ func (r *HTMLRenderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Nod
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) {
colorContent := v.Text(reader.Source())
- if css.ColorHandler(strings.ToLower(string(colorContent))) {
+ if cssColorHandler(string(colorContent)) {
v.AppendChild(v, NewColorPreview(colorContent))
}
}