Skip to content
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

fix(postcss-merge-rules): preserve container queries #1493

Merged
merged 2 commits into from
Apr 23, 2023
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
10 changes: 10 additions & 0 deletions packages/postcss-merge-rules/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,19 @@ function canMerge(ruleA, ruleB, browsers, compatibilityCache) {
) {
return false;
}
if (ruleA.some(isRuleOrAtRule) || ruleB.some(isRuleOrAtRule)) {
return false;
}
return parent && (selectors.every(noVendor) || sameVendor(a, b));
}

/**
* @param {import('postcss').ChildNode} node
* @return {boolean}
*/
function isRuleOrAtRule(node) {
return node.type === 'rule' || node.type === 'atrule';
}
/**
* @param {import('postcss').ChildNode} node
* @return {node is import('postcss').Declaration}
Expand Down
38 changes: 38 additions & 0 deletions packages/postcss-merge-rules/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ test(
passthroughCSS('@media print{h1{display:block}}h1{color:red}')
);

test(
'should not merge across container queries',
passthroughCSS(
`@container (min-width: 200px) {
.mobile {
display: none;
}
}
@container (max-width: 100px) {
.notMobile {
display: none;
}
}`
)
);

test(
'should perform partial merging of selectors',
processCSS(
Expand Down Expand Up @@ -843,4 +859,26 @@ test(
'h1{color:#001;color:#002;color:#003}h2{color:#001;color:#002}'
)
);

test(
'should not merge nested rules',
passthroughCSS('h1 { .a { color: red; } } h1 { .b { color: red; } }')
);

test(
'should not merge nested container rules',
passthroughCSS(
`.mobile {
@container (min-width: 200px) {
display: none;
}
}

.notMobile {
@container (max-width: 100px) {
display: none;
}
}`
)
);
test.run();