gitea source for verification 2026-05-22
This commit is contained in:
56
modules/svg/processor.go
Normal file
56
modules/svg/processor.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package svg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type globalVarsStruct struct {
|
||||
reXMLDoc,
|
||||
reComment,
|
||||
reAttrXMLNs,
|
||||
reAttrSize,
|
||||
reAttrClassPrefix *regexp.Regexp
|
||||
}
|
||||
|
||||
var globalVars = sync.OnceValue(func() *globalVarsStruct {
|
||||
return &globalVarsStruct{
|
||||
reXMLDoc: regexp.MustCompile(`(?s)<\?xml.*?>`),
|
||||
reComment: regexp.MustCompile(`(?s)<!--.*?-->`),
|
||||
|
||||
reAttrXMLNs: regexp.MustCompile(`(?s)\s+xmlns\s*=\s*"[^"]*"`),
|
||||
reAttrSize: regexp.MustCompile(`(?s)\s+(width|height)\s*=\s*"[^"]+"`),
|
||||
reAttrClassPrefix: regexp.MustCompile(`(?s)\s+class\s*=\s*"`),
|
||||
}
|
||||
})
|
||||
|
||||
// Normalize normalizes the SVG content: set default width/height, remove unnecessary tags/attributes
|
||||
// It's designed to work with valid SVG content. For invalid SVG content, the returned content is not guaranteed.
|
||||
func Normalize(data []byte, size int) []byte {
|
||||
vars := globalVars()
|
||||
data = vars.reXMLDoc.ReplaceAll(data, nil)
|
||||
data = vars.reComment.ReplaceAll(data, nil)
|
||||
|
||||
data = bytes.TrimSpace(data)
|
||||
svgTag, svgRemaining, ok := bytes.Cut(data, []byte(">"))
|
||||
if !ok || !bytes.HasPrefix(svgTag, []byte(`<svg`)) {
|
||||
return data
|
||||
}
|
||||
normalized := bytes.Clone(svgTag)
|
||||
normalized = vars.reAttrXMLNs.ReplaceAll(normalized, nil)
|
||||
normalized = vars.reAttrSize.ReplaceAll(normalized, nil)
|
||||
normalized = vars.reAttrClassPrefix.ReplaceAll(normalized, []byte(` class="`))
|
||||
normalized = bytes.TrimSpace(normalized)
|
||||
normalized = fmt.Appendf(normalized, ` width="%d" height="%d"`, size, size)
|
||||
if !bytes.Contains(normalized, []byte(` class="`)) {
|
||||
normalized = append(normalized, ` class="svg"`...)
|
||||
}
|
||||
normalized = append(normalized, '>')
|
||||
normalized = append(normalized, svgRemaining...)
|
||||
return normalized
|
||||
}
|
||||
29
modules/svg/processor_test.go
Normal file
29
modules/svg/processor_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package svg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNormalize(t *testing.T) {
|
||||
res := Normalize([]byte("foo"), 1)
|
||||
assert.Equal(t, "foo", string(res))
|
||||
|
||||
res = Normalize([]byte(`<?xml version="1.0"?>
|
||||
<!--
|
||||
comment
|
||||
-->
|
||||
<svg xmlns = "...">content</svg>`), 1)
|
||||
assert.Equal(t, `<svg width="1" height="1" class="svg">content</svg>`, string(res))
|
||||
|
||||
res = Normalize([]byte(`<svg
|
||||
width="100"
|
||||
class="svg-icon"
|
||||
>content</svg>`), 16)
|
||||
|
||||
assert.Equal(t, `<svg class="svg-icon" width="16" height="16">content</svg>`, string(res))
|
||||
}
|
||||
75
modules/svg/svg.go
Normal file
75
modules/svg/svg.go
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package svg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
gitea_html "code.gitea.io/gitea/modules/htmlutil"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/public"
|
||||
)
|
||||
|
||||
var svgIcons map[string]string
|
||||
|
||||
const defaultSize = 16
|
||||
|
||||
// Init discovers SVG icons and populates the `svgIcons` variable
|
||||
func Init() error {
|
||||
const svgAssetsPath = "assets/img/svg"
|
||||
files, err := public.AssetFS().ListFiles(svgAssetsPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svgIcons = make(map[string]string, len(files))
|
||||
for _, file := range files {
|
||||
if path.Ext(file) != ".svg" {
|
||||
continue
|
||||
}
|
||||
bs, err := public.AssetFS().ReadFile(svgAssetsPath, file)
|
||||
if err != nil {
|
||||
log.Error("Failed to read SVG file %s: %v", file, err)
|
||||
} else {
|
||||
svgIcons[file[:len(file)-4]] = string(Normalize(bs, defaultSize))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MockIcon(icon string) func() {
|
||||
if svgIcons == nil {
|
||||
svgIcons = make(map[string]string)
|
||||
}
|
||||
orig, exist := svgIcons[icon]
|
||||
svgIcons[icon] = fmt.Sprintf(`<svg class="svg %s" width="%d" height="%d"></svg>`, icon, defaultSize, defaultSize)
|
||||
return func() {
|
||||
if exist {
|
||||
svgIcons[icon] = orig
|
||||
} else {
|
||||
delete(svgIcons, icon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RenderHTML renders icons - arguments icon name (string), size (int), class (string)
|
||||
func RenderHTML(icon string, others ...any) template.HTML {
|
||||
size, class := gitea_html.ParseSizeAndClass(defaultSize, "", others...)
|
||||
if svgStr, ok := svgIcons[icon]; ok {
|
||||
// the code is somewhat hacky, but it just works, because the SVG contents are all normalized
|
||||
if size != defaultSize {
|
||||
svgStr = strings.Replace(svgStr, fmt.Sprintf(`width="%d"`, defaultSize), fmt.Sprintf(`width="%d"`, size), 1)
|
||||
svgStr = strings.Replace(svgStr, fmt.Sprintf(`height="%d"`, defaultSize), fmt.Sprintf(`height="%d"`, size), 1)
|
||||
}
|
||||
if class != "" {
|
||||
svgStr = strings.Replace(svgStr, `class="`, fmt.Sprintf(`class="%s `, class), 1)
|
||||
}
|
||||
return template.HTML(svgStr)
|
||||
}
|
||||
// during test (or something wrong happens), there is no SVG loaded, so use a dummy span to tell that the icon is missing
|
||||
return template.HTML(fmt.Sprintf("<span>%s(%d/%s)</span>", template.HTMLEscapeString(icon), size, template.HTMLEscapeString(class)))
|
||||
}
|
||||
Reference in New Issue
Block a user