Skip to content

improve specificity calculation #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions experimental/css-has-pseudo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to CSS Has Pseudo

### Unreleased

- Improved : selector specificity calculation

### 0.1.1 (January 5, 2022)

- Added : support for id and tag selector specificity.
Expand Down
37 changes: 33 additions & 4 deletions experimental/css-has-pseudo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,39 @@ function selectorSpecificity(node) {
case ':has':
case ':not':
{
const pseudoSpecificity = selectorSpecificity(node.nodes[0]);
a += pseudoSpecificity.a;
b += pseudoSpecificity.b;
c += pseudoSpecificity.c;
if (node.nodes && node.nodes.length > 0) {
let mostSpecificListItem = {
a: 0,
b: 0,
c: 0,
};

node.nodes.forEach((child) => {
const itemSpecificity = selectorSpecificity(child);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why I was mentioning extracting since I've been seeing this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, each time I copy/paste a bit I remind myself that I should fine a way to have these imported from a central util bundle :D

if (itemSpecificity.a > mostSpecificListItem.a) {
mostSpecificListItem = itemSpecificity;
return;
} else if (itemSpecificity.a < mostSpecificListItem.a) {
return;
}

if (itemSpecificity.b > mostSpecificListItem.b) {
mostSpecificListItem = itemSpecificity;
return;
} else if (itemSpecificity.b < mostSpecificListItem.b) {
return;
}

if (itemSpecificity.c > mostSpecificListItem.c) {
mostSpecificListItem = itemSpecificity;
return;
}
});

a += mostSpecificListItem.a;
b += mostSpecificListItem.b;
c += mostSpecificListItem.c;
}
break;
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/postcss-nesting/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to PostCSS Nesting

### Unreleased

- Improved : selector specificity calculation

### 10.1.1 (January 2, 2022)

- Removed Sourcemaps from package tarball.
Expand Down
37 changes: 33 additions & 4 deletions plugins/postcss-nesting/src/lib/merge-selectors/specificity.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,39 @@ export function selectorSpecificity(node) {
case ':has':
case ':not':
{
const pseudoSpecificity = selectorSpecificity(node.nodes[0]);
a += pseudoSpecificity.a;
b += pseudoSpecificity.b;
c += pseudoSpecificity.c;
if (node.nodes && node.nodes.length > 0) {
let mostSpecificListItem = {
a: 0,
b: 0,
c: 0,
};

node.nodes.forEach((child) => {
const itemSpecificity = selectorSpecificity(child);
if (itemSpecificity.a > mostSpecificListItem.a) {
mostSpecificListItem = itemSpecificity;
return;
} else if (itemSpecificity.a < mostSpecificListItem.a) {
return;
}

if (itemSpecificity.b > mostSpecificListItem.b) {
mostSpecificListItem = itemSpecificity;
return;
} else if (itemSpecificity.b < mostSpecificListItem.b) {
return;
}

if (itemSpecificity.c > mostSpecificListItem.c) {
mostSpecificListItem = itemSpecificity;
return;
}
});

a += mostSpecificListItem.a;
b += mostSpecificListItem.b;
c += mostSpecificListItem.c;
}
break;
}

Expand Down