Skip to content

Commit

Permalink
Fix Accidental Hover False Positive Within :not() Selectors (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuschick authored Jan 11, 2024
1 parent 31fde35 commit 63b5b8d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stylelint-plugin-defensive-css",
"version": "1.0.0",
"version": "1.0.1",
"description": "A Stylelint plugin to enforce defensive CSS best practices.",
"main": "src/index.js",
"type": "module",
Expand Down
5 changes: 5 additions & 0 deletions src/rules/use-defensive-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ const ruleFunction = (_, options) => {
const isHoverSelector = selector?.includes(':hover');
isWrappedInHoverAtRule = false;

// If the :hover selector is inside a :not() selector, ignore it
if (/:not\(([^)]*:hover[^)]*)\)/g.test(selector)) {
return;
}

if (isHoverSelector) {
traverseParentRules(parent);

Expand Down
8 changes: 8 additions & 0 deletions src/rules/use-defensive-css/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ testRule({
description:
'Use nested media queries with hover in the middle for button hover state.',
},
{
code: `div:not(:hover) { color: red; }`,
description: 'Use :hover selector inside of :not() selector.',
},
{
code: `div:not(:focus-visible, :hover) { color: red; }`,
description: 'Use :hover selector inside of a grouped :not() selector.',
},
],

reject: [
Expand Down

0 comments on commit 63b5b8d

Please sign in to comment.