remove PreferColorSchemes and add comments

This commit is contained in:
wxiaoguang 2024-04-26 00:58:39 +08:00
parent cbb63bcd89
commit 0b9bfc9e1b
2 changed files with 9 additions and 24 deletions

View File

@ -28,13 +28,18 @@ const (
)
type ThemeMetaInfo struct {
FileName string
InternalName string
DisplayName string
PreferColorSchemes container.Set[string]
FileName string
InternalName string
DisplayName string
}
func parseThemeMetaInfoToMap(cssContent string) map[string]string {
/*
The theme meta info is stored in the CSS file's variables of `gitea-theme-meta-info` element,
which is a privately defined and is only used by backend to extract the meta info.
Not using ":root" because it is difficult to parse various ":root" blocks when importing other files,
it is difficult to control the overriding, and it's difficult to avoid user's customized overridden styles.
*/
metaInfoContent := cssContent
if pos := strings.LastIndex(metaInfoContent, "gitea-theme-meta-info"); pos >= 0 {
metaInfoContent = metaInfoContent[pos:]
@ -69,20 +74,6 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
return m
}
// @media (prefers-color-scheme: dark)
func parseThemePreferColorSchemes(cssContent string) container.Set[string] {
re := regexp.MustCompile(`@media\s*\(\s*prefers-color-scheme\s*:\s*([-\w]+)\s*\)`)
matched := re.FindAllStringSubmatch(cssContent, -1)
if len(matched) == 0 {
return nil
}
schemes := container.Set[string]{}
for _, m := range matched {
schemes.Add(m[1])
}
return schemes
}
func defaultThemeMetaInfoByFileName(fileName string) *ThemeMetaInfo {
themeInfo := &ThemeMetaInfo{
FileName: fileName,
@ -98,7 +89,6 @@ func defaultThemeMetaInfoByInternalName(fileName string) *ThemeMetaInfo {
func parseThemeMetaInfo(fileName, cssContent string) *ThemeMetaInfo {
themeInfo := defaultThemeMetaInfoByFileName(fileName)
themeInfo.PreferColorSchemes = parseThemePreferColorSchemes(cssContent)
m := parseThemeMetaInfoToMap(cssContent)
if m == nil {
return themeInfo

View File

@ -6,15 +6,10 @@ package webtheme
import (
"testing"
"code.gitea.io/gitea/modules/container"
"github.com/stretchr/testify/assert"
)
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)
schemes := parseThemePreferColorSchemes(`@media (prefers-color-scheme: dark) {} @media (prefers-color-scheme: light) {} @media (prefers-color-scheme:dark) {}`)
assert.Equal(t, container.SetOf("dark", "light"), schemes)
}