Skip to content

is-pseudo : handle more complex selector patterns #923

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

Conversation

romainmenke
Copy link
Member

fixes : #922

Comment on lines 1 to +10
// :-csstools-matches(.a > .b) + :-csstools-matches(.c > .d)
// equivalent to
// .a.c > .b + .d
//
// and
//
// :-csstools-matches(.a > .b) ~ :-csstools-matches(.c > .d)
// equivalent to
// .a.c ~ .b + .d
//
Copy link
Member Author

Choose a reason for hiding this comment

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

These are actually the same, trivial to support both.

Comment on lines +64 to +84
const previous = sortedCompoundSelectors[i - 1];

sortedCompoundSelectors[i].remove();
node.prepend(sortedCompoundSelectors[i]);

if (previous && previous.type === 'tag' && sortedCompoundSelectors[i].type === 'tag') {
const wrapped = parser.pseudo({
value: ':is',
nodes: [
parser.selector({
value: '',
nodes: [
sortedCompoundSelectors[i],
],
}),
],
});

node.prepend(wrapped);
} else {
node.prepend(sortedCompoundSelectors[i]);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Sorting will group tag selectors a and b into ab which serializes as a single tag ab.

This bit is a "post fix" to wrap it back with :is() into a:is(b)

@@ -1,7 +1,7 @@
import parser from 'postcss-selector-parser';

export function sortCompoundSelectorsInsideComplexSelector(node) {
if (!node || !node.nodes) {
if (!node || !node.nodes || node.nodes.length === 1) {
Copy link
Member Author

Choose a reason for hiding this comment

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

:is(a) cause issues and doesn't require sorting anyway.
Better to short circuit.

background-color: #ffc;
}

:is(:-csstools-matches(.a, .b)) > .c {
Copy link
Member Author

Choose a reason for hiding this comment

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

I want to be sure that code looking for :-csstools-matches can also handle selector lists.

Internally we never produce :-csstools-matches with lists, but want to cover it anyway.

(We produce :-csstools-matches as an intermediate step with partially transpiled selectors to prevent infinite loops.)

Copy link
Member

@Antonio-Laguna Antonio-Laguna left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks al lot for the comments on specific areas of interest! Love it

@romainmenke romainmenke merged commit 51b3bc6 into main Apr 9, 2023
@romainmenke romainmenke deleted the is-pseudo-more-complex-patterns--passionate-newt-eadecf8d48 branch April 9, 2023 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[postcss-is-pseudo-class] Cannot expand simple statements with complex selectors
2 participants