π Require CSS.escape() for interpolated values in CSS selectors.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
When interpolating arbitrary values into CSS selectors, use CSS.escape() to make sure the resulting selector remains valid.
By default, this rule only checks interpolations inside attribute selectors to avoid noisy reports for common class and ID selector patterns.
// β
document.querySelector(`[data-id="${id}"]`);
// β
document.querySelector(`[data-id="${CSS.escape(id)}"]`);// β
element.querySelectorAll(`a[href^="#${hash}"]`);
// β
element.querySelectorAll(`a[href^="#${CSS.escape(hash)}"]`);// β
document.querySelector(cssEscape`#${id}`);Type: boolean
Default: false
When set to true, checks all selector interpolations instead of only interpolations inside attribute selectors.
// eslint unicorn/require-css-escape: ["error", {"checkAllSelectors": true}]
// β
document.querySelector(`#${id}`);
// β
document.querySelector(`#${CSS.escape(id)}`);