Skip to content

Latest commit

Β 

History

History
56 lines (37 loc) Β· 1.54 KB

File metadata and controls

56 lines (37 loc) Β· 1.54 KB

require-css-escape

πŸ“ 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.

Examples

// ❌
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}`);

Options

checkAllSelectors

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)}`);