mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-14 02:51:35 +02:00
chore: allow Normalize function to take any input and process it acc.
This commit is contained in:
parent
16c899cc92
commit
1eb8d5727d
1 changed files with 13 additions and 2 deletions
|
@ -25,7 +25,7 @@ var (
|
|||
|
||||
// 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 {
|
||||
func Normalize(data []byte, size any) []byte {
|
||||
normalizeVarsOnce.Do(func() {
|
||||
normalizeVars = &normalizeVarsStruct{
|
||||
reXMLDoc: regexp.MustCompile(`(?s)<\?xml.*?>`),
|
||||
|
@ -49,7 +49,18 @@ func Normalize(data []byte, size int) []byte {
|
|||
normalized = normalizeVars.reAttrSize.ReplaceAll(normalized, nil)
|
||||
normalized = normalizeVars.reAttrClassPrefix.ReplaceAll(normalized, []byte(` class="`))
|
||||
normalized = bytes.TrimSpace(normalized)
|
||||
normalized = fmt.Appendf(normalized, ` width="%d" height="%d"`, size, size)
|
||||
|
||||
sizeStr := ""
|
||||
switch v := size.(type) {
|
||||
case string:
|
||||
sizeStr = v
|
||||
case int:
|
||||
sizeStr = fmt.Sprintf("%dpx", v)
|
||||
default:
|
||||
sizeStr = "16px"
|
||||
}
|
||||
|
||||
normalized = fmt.Appendf(normalized, ` width="%s" height="%s"`, sizeStr, sizeStr)
|
||||
if !bytes.Contains(normalized, []byte(` class="`)) {
|
||||
normalized = append(normalized, ` class="svg"`...)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue