Skip to content

Commit a797b84

Browse files
wxiaoguangsilverwinddelvh
authored
Keep (add if not existing) xmlns attribute for generated SVG images (#23410)
Fix #23409 Developers could browse & preview the local SVG images files directly. It still has clear output. ![image](https://user-images.githubusercontent.com/2114189/224317107-f4b26c76-e36a-4e80-9eee-d8dc2e16421f.png) ![image](https://user-images.githubusercontent.com/2114189/224317527-2d4ca131-978c-4933-b071-4bae483f06e1.png) --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: delvh <leon@kske.dev>
1 parent 34a2cf5 commit a797b84

File tree

343 files changed

+357
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

343 files changed

+357
-343
lines changed

build/generate-svg.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ async function processFile(file, {prefix, fullName} = {}) {
2525
if (prefix === 'octicon') name = name.replace(/-[0-9]+$/, ''); // chop of '-16' on octicons
2626
}
2727

28+
// Set the `xmlns` attribute so that the files are displayable in standalone documents
29+
// The svg backend module will strip the attribute during startup for inline display
2830
const {data} = optimize(await readFile(file, 'utf8'), {
2931
plugins: [
3032
{name: 'preset-default'},
31-
{name: 'removeXMLNS'},
3233
{name: 'removeDimensions'},
3334
{name: 'prefixIds', params: {prefix: () => name}},
3435
{name: 'addClassesToSVGElement', params: {classNames: ['svg', name]}},
35-
{name: 'addAttributesToSVGElement', params: {attributes: [{'width': '16'}, {'height': '16'}, {'aria-hidden': 'true'}]}},
36+
{
37+
name: 'addAttributesToSVGElement', params: {
38+
attributes: [
39+
{'xmlns': 'http://www.w3.org/2000/svg'},
40+
{'width': '16'}, {'height': '16'}, {'aria-hidden': 'true'},
41+
]
42+
}
43+
},
3644
],
3745
});
3846

modules/svg/svg.go

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const defaultSize = 16
2525
// Init discovers SVGs and populates the `SVGs` variable
2626
func Init() {
2727
SVGs = Discover()
28+
29+
// Remove `xmlns` because inline SVG does not need it
30+
r := regexp.MustCompile(`(<svg\b[^>]*?)\s+xmlns="[^"]*"`)
31+
for name, svg := range SVGs {
32+
SVGs[name] = r.ReplaceAllString(svg, "$1")
33+
}
2834
}
2935

3036
// Render render icons - arguments icon name (string), size (int), class (string)

public/img/svg/fontawesome-openid.svg

+1-1
Loading

public/img/svg/fontawesome-save.svg

+1-1
Loading

public/img/svg/fontawesome-send.svg

+1-1
Loading
+1-1
Loading

public/img/svg/gitea-cargo.svg

+1-1
Loading

public/img/svg/gitea-chef.svg

+1-1
Loading

0 commit comments

Comments
 (0)