Skip to content

Commit ca12618

Browse files
committed
[babel-plugin] fix priority calculation for compound pseudo selectors
compound selectors like :hover::after now return the sum of individual priorities (130 + 5000 = 5130) instead of the default 40, matching the behavior when pseudos are specified separately via the nested api.
1 parent 830b6d1 commit ca12618

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

packages/@stylexjs/shared/src/utils/property-priorities.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,28 @@ const RELATIONAL_SELECTORS = {
734734
/^:where\(\.[0-9a-zA-Z_-]+(:[a-zA-Z-]+)\s+~\s+\*,\s+:has\(~\s\.[0-9a-zA-Z_-]+(:[a-zA-Z-]+)\)\)$/,
735735
};
736736

737+
// Matches pseudo-elements (::after) and pseudo-classes (:hover, :nth-child(2))
738+
const PSEUDO_PART_REGEX = /::[a-zA-Z-]+|:[a-zA-Z-]+(?:\([^)]*\))?/g;
739+
740+
// Calculate priority for compound pseudo selectors like :hover::after
741+
function getCompoundPseudoPriority(key: string): number | void {
742+
const pseudoParts = key.match(PSEUDO_PART_REGEX);
743+
if (pseudoParts && pseudoParts.length > 1) {
744+
let total = 0;
745+
for (const part of pseudoParts) {
746+
if (part.startsWith('::')) {
747+
total += PSEUDO_ELEMENT_PRIORITY;
748+
} else {
749+
const prop = part.includes('(')
750+
? part.slice(0, part.indexOf('('))
751+
: part;
752+
total += PSEUDO_CLASS_PRIORITIES[prop] ?? 40;
753+
}
754+
}
755+
return total;
756+
}
757+
}
758+
737759
export function getAtRulePriority(key: string): number | void {
738760
if (key.startsWith('--')) {
739761
return 1;
@@ -754,6 +776,10 @@ export function getAtRulePriority(key: string): number | void {
754776

755777
export function getPseudoElementPriority(key: string): number | void {
756778
if (key.startsWith('::')) {
779+
const compoundPriority = getCompoundPseudoPriority(key);
780+
if (compoundPriority != null) {
781+
return compoundPriority;
782+
}
757783
return PSEUDO_ELEMENT_PRIORITY;
758784
}
759785
}
@@ -790,10 +816,14 @@ export function getPseudoClassPriority(key: string): number | void {
790816
}
791817

792818
if (key.startsWith(':')) {
793-
const prop =
794-
key.startsWith(':') && key.includes('(')
795-
? key.slice(0, key.indexOf('('))
796-
: key;
819+
const compoundPriority = getCompoundPseudoPriority(key);
820+
if (compoundPriority != null) {
821+
return compoundPriority;
822+
}
823+
824+
const prop = key.includes('(')
825+
? key.slice(0, key.indexOf('('))
826+
: key;
797827

798828
return PSEUDO_CLASS_PRIORITIES[prop] ?? 40;
799829
}

0 commit comments

Comments
 (0)