svg/svgo

includesAttrSelector: support different equality checks in attribute selectors too

Open

#2145 aperta il 21 giu 2025

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)JavaScript (1361 fork)batch import
good first issuehelp wanted

Metriche repository

Star
 (20.308 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

We have a helper function named includesAttrSelector. We use this to check if an attribute has been referenced in the stylesheet. If so, the plugin may have different behavior. Usually, by not removing that attribute because it may break styles otherwise.

However, our logic takes a very safe approach, of never deleting attributes if it's been referenced in CSS, even if the operator/check wouldn't match.

For example, suppose an element in an SVG had preserveAspectRatio="xMidYMid meet", with a CSS selector of [preserveAspectRatio^="y"].

This would not match, yet to be on the safe side, we will not remove the attribute. We do not actually check the operator and value, but only the key. Let's improve the logic to actually perform these checks so that we do remove the attribute when it's truly unnecessary.

References:

Guida contributor