11import octiconChevronDown from '../../public/img/svg/octicon-chevron-down.svg' ;
22import octiconChevronRight from '../../public/img/svg/octicon-chevron-right.svg' ;
3+ import octiconCopy from '../../public/img/svg/octicon-copy.svg' ;
34import octiconGitMerge from '../../public/img/svg/octicon-git-merge.svg' ;
45import octiconGitPullRequest from '../../public/img/svg/octicon-git-pull-request.svg' ;
56import octiconIssueClosed from '../../public/img/svg/octicon-issue-closed.svg' ;
@@ -20,6 +21,7 @@ import Vue from 'vue';
2021export const svgs = {
2122 'octicon-chevron-down' : octiconChevronDown ,
2223 'octicon-chevron-right' : octiconChevronRight ,
24+ 'octicon-copy' : octiconCopy ,
2325 'octicon-git-merge' : octiconGitMerge ,
2426 'octicon-git-pull-request' : octiconGitPullRequest ,
2527 'octicon-issue-closed' : octiconIssueClosed ,
@@ -38,18 +40,33 @@ export const svgs = {
3840
3941const parser = new DOMParser ( ) ;
4042const serializer = new XMLSerializer ( ) ;
43+ const parsedSvgs = new Map ( ) ;
4144
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 ) {
4467 if ( ! ( name in svgs ) ) return '' ;
4568 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 ) ) ;
5370}
5471
5572export const SvgIcon = Vue . component ( 'SvgIcon' , {
0 commit comments