loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Don't color dot literal color names' (#2905) from gusted/forgejo-colordots into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2905
Reviewed-by: Algernon <algernon@noreply.codeberg.org>

Gusted 46fead4c bbf612b3

+70 -2
+19
modules/markup/markdown/color_util.go
··· 1 + // Copyright 2024 The Forgejo Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package markdown 5 + 6 + import "regexp" 7 + 8 + var ( 9 + hexRGB = regexp.MustCompile(`^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$`) 10 + hsl = regexp.MustCompile(`^hsl\([ ]*([012]?[0-9]{1,2}|3[0-5][0-9]|360),[ ]*([0-9]{0,2}|100)\%,[ ]*([0-9]{0,2}|100)\%\)$`) 11 + hsla = regexp.MustCompile(`^hsla\(([ ]*[012]?[0-9]{1,2}|3[0-5][0-9]|360),[ ]*([0-9]{0,2}|100)\%,[ ]*([0-9]{0,2}|100)\%,[ ]*(1|1\.0|0|(0\.[0-9]+))\)$`) 12 + rgb = regexp.MustCompile(`^rgb\(([ ]*((([0-9]{1,2}|100)\%)|(([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))),){2}([ ]*((([0-9]{1,2}|100)\%)|(([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))))\)$`) 13 + rgba = regexp.MustCompile(`^rgba\(([ ]*((([0-9]{1,2}|100)\%)|(([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))),){3}[ ]*(1(\.0)?|0|(0\.[0-9]+))\)$`) 14 + ) 15 + 16 + // matchColor return if color is in the form of hex RGB, HSL(A) or RGB(A). 17 + func matchColor(color string) bool { 18 + return hexRGB.MatchString(color) || rgb.MatchString(color) || rgba.MatchString(color) || hsl.MatchString(color) || hsla.MatchString(color) 19 + }
+50
modules/markup/markdown/color_util_test.go
··· 1 + // Copyright 2024 The Forgejo Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package markdown 5 + 6 + import ( 7 + "testing" 8 + 9 + "github.com/stretchr/testify/assert" 10 + ) 11 + 12 + func TestMatchColor(t *testing.T) { 13 + testCases := []struct { 14 + input string 15 + expected bool 16 + }{ 17 + {"#ddeeffa0", true}, 18 + {"#ddeefe", true}, 19 + {"#abcdef", true}, 20 + {"#abcdeg", false}, 21 + {"#abcdefg0", false}, 22 + {"black", false}, 23 + {"violet", false}, 24 + {"rgb(255, 255, 255)", true}, 25 + {"rgb(0, 0, 0)", true}, 26 + {"rgb(256, 0, 0)", false}, 27 + {"rgb(0, 256, 0)", false}, 28 + {"rgb(0, 0, 256)", false}, 29 + {"rgb(0, 0, 0, 1)", false}, 30 + {"rgba(0, 0, 0)", false}, 31 + {"rgba(0, 255, 0, 1)", true}, 32 + {"rgba(32, 255, 12, 0.55)", true}, 33 + {"rgba(32, 256, 12, 0.55)", false}, 34 + {"hsl(0, 0%, 0%)", true}, 35 + {"hsl(360, 100%, 100%)", true}, 36 + {"hsl(361, 100%, 50%)", false}, 37 + {"hsl(360, 101%, 50%)", false}, 38 + {"hsl(360, 100%, 101%)", false}, 39 + {"hsl(0, 0%, 0%, 0)", false}, 40 + {"hsla(0, 0%, 0%)", false}, 41 + {"hsla(0, 0%, 0%, 0)", true}, 42 + {"hsla(0, 0%, 0%, 1)", true}, 43 + {"hsla(0, 0%, 0%, 0.5)", true}, 44 + {"hsla(0, 0%, 0%, 1.5)", false}, 45 + } 46 + for _, testCase := range testCases { 47 + actual := matchColor(testCase.input) 48 + assert.Equal(t, testCase.expected, actual) 49 + } 50 + }
+1 -2
modules/markup/markdown/goldmark.go
··· 16 16 "code.gitea.io/gitea/modules/setting" 17 17 giteautil "code.gitea.io/gitea/modules/util" 18 18 19 - "github.com/microcosm-cc/bluemonday/css" 20 19 "github.com/yuin/goldmark/ast" 21 20 east "github.com/yuin/goldmark/extension/ast" 22 21 "github.com/yuin/goldmark/parser" ··· 199 198 } 200 199 case *ast.CodeSpan: 201 200 colorContent := n.Text(reader.Source()) 202 - if css.ColorHandler(strings.ToLower(string(colorContent))) { 201 + if matchColor(strings.ToLower(string(colorContent))) { 203 202 v.AppendChild(v, NewColorPreview(colorContent)) 204 203 } 205 204 }