Skip to content

Commit fdfd440

Browse files
committed
update doc, add tests
1 parent d941875 commit fdfd440

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

docs/content/administration/customizing-gitea.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ It could add theme meta information into the custom theme CSS file to provide mo
388388
389389
If a custom theme is a dark theme, please set the global css variable `--is-dark-theme: true` in the `:root` block.
390390
This allows Gitea to adjust the Monaco code editor's theme accordingly.
391+
An "auto" theme could be implemented by using "theme-gitea-auto.css" as a reference.
391392
392393
```css
393394
gitea-theme-meta-info {

services/webtheme/webtheme.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
4949
(
5050
\s*(--[-\w]+)
5151
\s*:
52-
\s*("(\\"|[^"])*")
52+
\s*(
53+
("(\\"|[^"])*")
54+
|('(\\'|[^'])*')
55+
|([^'";]+)
56+
)
5357
\s*;
5458
\s*
5559
)
@@ -66,9 +70,13 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
6670
m := map[string]string{}
6771
for _, item := range matchedItems {
6872
v := item[3]
69-
v = strings.TrimPrefix(v, "\"")
70-
v = strings.TrimSuffix(v, "\"")
71-
v = strings.ReplaceAll(v, `\"`, `"`)
73+
if strings.HasPrefix(v, `"`) {
74+
v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`)
75+
v = strings.ReplaceAll(v, `\"`, `"`)
76+
} else if strings.HasPrefix(v, `'`) {
77+
v = strings.TrimSuffix(strings.TrimPrefix(v, `'`), `'`)
78+
v = strings.ReplaceAll(v, `\'`, `'`)
79+
}
7280
m[item[2]] = v
7381
}
7482
return m

services/webtheme/webtheme_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ import (
1010
)
1111

1212
func TestParseThemeMetaInfo(t *testing.T) {
13-
m := parseThemeMetaInfoToMap(`gitea-theme-meta-info { --k1: "v1"; --k2: "a\"b"; }`)
14-
assert.Equal(t, map[string]string{"--k1": "v1", "--k2": `a"b`}, m)
13+
m := parseThemeMetaInfoToMap(`gitea-theme-meta-info {
14+
--k1: "v1";
15+
--k2: "v\"2";
16+
--k3: 'v3';
17+
--k4: 'v\'4';
18+
--k5: v5;
19+
}`)
20+
assert.Equal(t, map[string]string{
21+
"--k1": "v1",
22+
"--k2": `v"2`,
23+
"--k3": "v3",
24+
"--k4": "v'4",
25+
"--k5": "v5",
26+
}, m)
27+
28+
// if an auto theme imports others, the meta info should be extracted from the last one
29+
// the meta in imported themes should be ignored to avoid incorrect overriding
30+
m = parseThemeMetaInfoToMap(`
31+
@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: foo; } }
32+
@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: bar; } }
33+
gitea-theme-meta-info {
34+
--k2: real;
35+
}`)
36+
assert.Equal(t, map[string]string{"--k2": "real"}, m)
1537
}

0 commit comments

Comments
 (0)