Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitizing CSS with Angular's DomSanitizer

I want to sanitize some CSS before I inject it into a <style> tag in the <head> of my app.

I am passing it through the DomSanitizer like this:

const safeCSS = this.domSanitzer.sanitize(SecurityContext.STYLE, css);

But I am getting this error message in the console, which thinks my CSS is unsafe.

WARNING: sanitizing unsafe style value [MY_CSS_HERE]

I want to understand why Angular thinks my CSS is unsafe.

I've put my CSS on codepen as it's quite large, but really simple.

See https://codepen.io/stevedeighton/pen/OvgLVb

Any ideas?

Thanks!

like image 887
Steve D Avatar asked Aug 29 '26 20:08

Steve D


1 Answers

You should use bypassSecurityTrustStyle(value: string) instead of sanitize().

Here is the example.

like image 120
Efe Avatar answered Aug 31 '26 10:08

Efe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!