Skip to content

Commit afd519e

Browse files
authored
fix (icon picker): revert removal of defs, add instanceId suffix to ensure uniquene… (#3303)
* fix: revert removal of defs, add instanceId suffix to ensure uniqueness of IDs * chore: typo in comment * fix: use random number instead of instanceId * chore: remove comments
1 parent 8e75e0e commit afd519e

File tree

1 file changed

+6
-5
lines changed
  • src/components/icon-search-popover

1 file changed

+6
-5
lines changed

src/components/icon-search-popover/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ export const cleanSvgString = svgString => {
5858
let newSvg = svgString.replace( /(^[\s\S]*?)(<svg)/gm, '$2' )
5959
.replace( /(<\/svg>)([\s\S]*)/g, '$1' )
6060

61-
// Remove defs
62-
if ( newSvg.indexOf( '<defs' ) !== -1 ) {
63-
newSvg = newSvg.replace( /<defs[\s\S]*?<\/defs>/gm, '' )
64-
}
61+
// Generate a random numbere to append to the IDs
62+
const svgId = Math.floor( Math.random() * new Date().getTime() ) % 100000
63+
newSvg = newSvg.replace( /id="([^"]*)"/g, `id="$1-${ svgId }"` )
64+
newSvg = newSvg.replace( /url\(#([^)]*)\)/g, `url(#$1-${ svgId })` )
65+
newSvg = newSvg.replace( /href="#([^"]*)"/g, `href="#$1-${ svgId }"` )
6566

6667
// Remove comments
6768
if ( newSvg.indexOf( '<!--' ) !== -1 ) {
@@ -145,7 +146,7 @@ const IconSearchPopover = props => {
145146
return
146147
}
147148

148-
// Read the SVG,
149+
// Read the SVG
149150
const fr = new FileReader()
150151
fr.onload = function( e ) {
151152
setIsDropping( false )

0 commit comments

Comments
 (0)