From fdfd4400615afc19d4d42524aeac1f8dffa6ed2d Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 8 May 2024 09:36:47 +0800 Subject: [PATCH] update doc, add tests --- .../administration/customizing-gitea.en-us.md | 1 + services/webtheme/webtheme.go | 16 +++++++++--- services/webtheme/webtheme_test.go | 26 +++++++++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/content/administration/customizing-gitea.en-us.md b/docs/content/administration/customizing-gitea.en-us.md index f25f124242..e472fefbbf 100644 --- a/docs/content/administration/customizing-gitea.en-us.md +++ b/docs/content/administration/customizing-gitea.en-us.md @@ -388,6 +388,7 @@ It could add theme meta information into the custom theme CSS file to provide mo If a custom theme is a dark theme, please set the global css variable `--is-dark-theme: true` in the `:root` block. This allows Gitea to adjust the Monaco code editor's theme accordingly. +An "auto" theme could be implemented by using "theme-gitea-auto.css" as a reference. ```css gitea-theme-meta-info { diff --git a/services/webtheme/webtheme.go b/services/webtheme/webtheme.go index 5e2245f7e4..58aea3bc74 100644 --- a/services/webtheme/webtheme.go +++ b/services/webtheme/webtheme.go @@ -49,7 +49,11 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string { ( \s*(--[-\w]+) \s*: -\s*("(\\"|[^"])*") +\s*( +("(\\"|[^"])*") +|('(\\'|[^'])*') +|([^'";]+) +) \s*; \s* ) @@ -66,9 +70,13 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string { m := map[string]string{} for _, item := range matchedItems { v := item[3] - v = strings.TrimPrefix(v, "\"") - v = strings.TrimSuffix(v, "\"") - v = strings.ReplaceAll(v, `\"`, `"`) + if strings.HasPrefix(v, `"`) { + v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`) + v = strings.ReplaceAll(v, `\"`, `"`) + } else if strings.HasPrefix(v, `'`) { + v = strings.TrimSuffix(strings.TrimPrefix(v, `'`), `'`) + v = strings.ReplaceAll(v, `\'`, `'`) + } m[item[2]] = v } return m diff --git a/services/webtheme/webtheme_test.go b/services/webtheme/webtheme_test.go index 42af39719d..5be2cb4165 100644 --- a/services/webtheme/webtheme_test.go +++ b/services/webtheme/webtheme_test.go @@ -10,6 +10,28 @@ import ( ) func TestParseThemeMetaInfo(t *testing.T) { - m := parseThemeMetaInfoToMap(`gitea-theme-meta-info { --k1: "v1"; --k2: "a\"b"; }`) - assert.Equal(t, map[string]string{"--k1": "v1", "--k2": `a"b`}, m) + m := parseThemeMetaInfoToMap(`gitea-theme-meta-info { + --k1: "v1"; + --k2: "v\"2"; + --k3: 'v3'; + --k4: 'v\'4'; + --k5: v5; +}`) + assert.Equal(t, map[string]string{ + "--k1": "v1", + "--k2": `v"2`, + "--k3": "v3", + "--k4": "v'4", + "--k5": "v5", + }, m) + + // if an auto theme imports others, the meta info should be extracted from the last one + // the meta in imported themes should be ignored to avoid incorrect overriding + m = parseThemeMetaInfoToMap(` +@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: foo; } } +@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: bar; } } +gitea-theme-meta-info { + --k2: real; +}`) + assert.Equal(t, map[string]string{"--k2": "real"}, m) }