File tree 1 file changed +8
-11
lines changed
1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change @@ -29,18 +29,15 @@ export function constructSvgElement(
29
29
retryable : boolean ,
30
30
options ?: ResolveIconOptions
31
31
) : SVGResult | null {
32
- const div = document . createElement ( "div" ) ;
33
- div . innerHTML = content ;
34
-
35
- const svg = div . firstElementChild ;
36
- if ( svg ?. tagName ?. toLowerCase ( ) !== "svg" )
37
- return retryable ? CACHEABLE_ERROR : null ;
38
-
39
32
if ( ! parser ) parser = new DOMParser ( ) ;
40
- const doc = parser . parseFromString ( svg . outerHTML , "text/html" ) ;
41
-
42
- const svgEl = doc . body . querySelector ( "svg" ) ;
43
- if ( ! svgEl ) return retryable ? CACHEABLE_ERROR : null ;
33
+ // Requires `xmlns`for the SVG to be parsed correctly
34
+ const refinedContent = content . includes ( "xmlns=" )
35
+ ? content
36
+ : content . replace ( / ( < s v g ) ( \s ) / i, '$1 xmlns="http://www.w3.org/2000/svg"$2' ) ;
37
+ const doc = parser . parseFromString ( refinedContent , "image/svg+xml" ) ;
38
+ const svgEl = doc . documentElement ;
39
+ if ( ! ( svgEl instanceof SVGSVGElement ) )
40
+ return retryable ? CACHEABLE_ERROR : null ;
44
41
45
42
const titles = svgEl . querySelectorAll ( "title" ) ;
46
43
for ( const title of titles ) {
You can’t perform that action at this time.
0 commit comments