1
1
import octiconChevronDown from '../../public/img/svg/octicon-chevron-down.svg' ;
2
2
import octiconChevronRight from '../../public/img/svg/octicon-chevron-right.svg' ;
3
+ import octiconCopy from '../../public/img/svg/octicon-copy.svg' ;
3
4
import octiconGitMerge from '../../public/img/svg/octicon-git-merge.svg' ;
4
5
import octiconGitPullRequest from '../../public/img/svg/octicon-git-pull-request.svg' ;
5
6
import octiconIssueClosed from '../../public/img/svg/octicon-issue-closed.svg' ;
@@ -20,6 +21,7 @@ import Vue from 'vue';
20
21
export const svgs = {
21
22
'octicon-chevron-down' : octiconChevronDown ,
22
23
'octicon-chevron-right' : octiconChevronRight ,
24
+ 'octicon-copy' : octiconCopy ,
23
25
'octicon-git-merge' : octiconGitMerge ,
24
26
'octicon-git-pull-request' : octiconGitPullRequest ,
25
27
'octicon-issue-closed' : octiconIssueClosed ,
@@ -38,18 +40,33 @@ export const svgs = {
38
40
39
41
const parser = new DOMParser ( ) ;
40
42
const serializer = new XMLSerializer ( ) ;
43
+ const parsedSvgs = new Map ( ) ;
41
44
42
- // retrieve a HTML string for given SVG icon name, size and additional classes
43
- export function svg ( name , size = 16 , className = '' ) {
45
+ function getParsedSvg ( name ) {
46
+ if ( parsedSvgs . has ( name ) ) return parsedSvgs . get ( name ) ;
47
+ const root = parser . parseFromString ( svgs [ name ] , 'text/html' ) ;
48
+ const svgNode = root . querySelector ( 'svg' ) ;
49
+ parsedSvgs . set ( name , svgNode ) ;
50
+ return svgNode ;
51
+ }
52
+
53
+ function applyAttributes ( node , size , className ) {
54
+ if ( size !== 16 ) node . setAttribute ( 'width' , String ( size ) ) ;
55
+ if ( size !== 16 ) node . setAttribute ( 'height' , String ( size ) ) ;
56
+ if ( className ) node . classList . add ( ...className . split ( / \s + / ) ) ;
57
+ return node ;
58
+ }
59
+
60
+ // returns a SVG node for given SVG icon name, size and additional classes
61
+ export function svgNode ( name , size = 16 , className = '' ) {
62
+ return applyAttributes ( getParsedSvg ( name ) , size , className ) ;
63
+ }
64
+
65
+ // returns a HTML string for given SVG icon name, size and additional classes
66
+ export function svg ( name , size , className ) {
44
67
if ( ! ( name in svgs ) ) return '' ;
45
68
if ( size === 16 && ! className ) return svgs [ name ] ;
46
-
47
- const document = parser . parseFromString ( svgs [ name ] , 'image/svg+xml' ) ;
48
- const svgNode = document . firstChild ;
49
- if ( size !== 16 ) svgNode . setAttribute ( 'width' , String ( size ) ) ;
50
- if ( size !== 16 ) svgNode . setAttribute ( 'height' , String ( size ) ) ;
51
- if ( className ) svgNode . classList . add ( ...className . split ( / \s + / ) ) ;
52
- return serializer . serializeToString ( svgNode ) ;
69
+ return serializer . serializeToString ( svgNode ( name , size , className ) ) ;
53
70
}
54
71
55
72
export const SvgIcon = Vue . component ( 'SvgIcon' , {
0 commit comments