icon swap | InnerHTML security risks
Hey, if you had a button with an icon in it, let’s say it’s a theme toggle, and your icon switches from a sun to a moon when you click, the way I’d imagine doing that is by setting the button’s
Using
innerHTML
to a different svg. I’ve heard this poses security risks though since users can manipulate the data in there. What would a better alternative be for swapping out content like that?Using
adjacentHTML
doesn’t seem like it would fit the use case since this adds the content but doesn’t replace it. I know you could then remove the other icon but I feel you could do the whole thing in one swoop without doing that? I may be wrong. I believe there was a method called .replaceWith
or something along those lines but I’d assume you would put html in there too so I’d imagine there’s security risks with that as well, and I can’t imagine using a template for something small like this is worth it? Please correct me if I’m wrong with any of this.
I’d appreciate any insight, thanks in advance.27 Replies
I'd just put both svgs inside there
and just simply toggle display accordingly
like if the default theme is light then by default the moon svg will be display: none
and when u click the btn the display none gets removed from the moon and gets added to the sun
Oh okay I see, that makes more sense, thank you.
welcm
Thank you for taking the time to do that.
welcome
how about you use svg symbols?
or do this
Is that the
<use>
tag?
And you mean just swap the href for it on click?probably
<use>
and <symbol>
yup
you <use> the <symbol>
Briefly:
1) Class Based Approach as in Codepen below is good
2) Approach like this
Can lead to some XSS and DOM manipulation risk
3) replaceChild is wacky cause it creates a new DOM element on each toggle
4) SVG sprite is cool just like 1)
Thank you for the suggestion
Thank you for taking the time to write that, it definitely helps out
option 5: change the href of <use>
which is the easiest of all
also, do not set
display: none
on svgs
that royally fucks all animations, and they dont play anymoreDidn't play with the sprites enough, thanks for the insights
In what way?
the animations dont work
you have to set the width and height to 0, then use
visibility
ive had that problem before, and the display: none
was the causeso many little details, that's why I really love the frontend
Oh if you try to animate between the two icons?
no, if you try to animate at all
Any animation on the page? Or just regarding the button/svg
ya true
if u want to animate then display won't work
display is not animateable
i think i just made up that word "animateable" lol
just on any svg that uses the symbols from a
display: none
svg
lol you wouldnt have invented if you spelled it correctly:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_propertieshaha xD
don't worry, it happens
but didn't the red squiggly made you curious about if the word exists and you misspelled it? 🤔
i was typing on mobile so no red squiggly xD
🤔 no duck when you want to type ... pickles?
wdym?
i do have autocorrect off