Skip to content

Commit

Permalink
Fix issue styled-components#245 with recursive filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
nhardy committed Jul 19, 2021
1 parent e1be59f commit 0112bcf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.vscode
node_modules
yarn-error.log
21 changes: 9 additions & 12 deletions src/styleSheetSerializer.js
Expand Up @@ -62,22 +62,19 @@ const filterRules = (classNames) => (rule) =>
includesClassNames(classNames, rule.selectors) &&
rule.declarations.length;

const getAtRules = (ast, filter) =>
ast.stylesheet.rules
.filter((rule) => rule.type === 'media' || rule.type === 'supports')
.reduce((acc, atRule) => {
atRule.rules = atRule.rules.filter(filter);

return acc.concat(atRule);
}, []);
const getAllRules = (rules, classNames) => rules
.filter(
(rule) => rule.type === 'media'
|| rule.type === 'supports'
|| filterRules(classNames)(rule)
)
.map(rule => (rule.type === "rule" ? rule : Object.assign({}, rule, { rules: getAllRules(rule.rules, classNames) })))
.filter(rule => (rule.type === "rule" && rule.declarations.length) || rule.rules.length);

const getStyle = (classNames) => {
const ast = getCSS();
const filter = filterRules(classNames);
const rules = ast.stylesheet.rules.filter(filter);
const atRules = getAtRules(ast, filter);

ast.stylesheet.rules = rules.concat(atRules);
ast.stylesheet.rules = getAllRules(ast.stylesheet.rules, classNames);

return css.stringify(ast);
};
Expand Down
96 changes: 60 additions & 36 deletions test/__snapshots__/styleSheetSerializer.spec.js.snap
Expand Up @@ -838,15 +838,6 @@ exports[`supported css: mount 1`] = `
background: palevioletred;
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c1 {
background: tomato;
Expand All @@ -855,6 +846,21 @@ html.test .c0 {
.c1:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c1 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<styled.div>
Expand All @@ -878,15 +884,6 @@ exports[`supported css: react-test-renderer 1`] = `
background: palevioletred;
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c1 {
background: tomato;
Expand All @@ -895,6 +892,21 @@ html.test .c0 {
.c1:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c1 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<div
Expand All @@ -916,15 +928,6 @@ exports[`supported css: react-testing-library 1`] = `
background: palevioletred;
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c1 {
background: tomato;
Expand All @@ -933,6 +936,21 @@ html.test .c0 {
.c1:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c1 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<div
Expand All @@ -954,15 +972,6 @@ exports[`supported css: shallow 1`] = `
background: palevioletred;
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c1 {
background: tomato;
Expand All @@ -971,6 +980,21 @@ html.test .c0 {
.c1:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c1 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c1 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<div
Expand Down
4 changes: 4 additions & 0 deletions test/styleSheetSerializer.spec.js
Expand Up @@ -179,6 +179,10 @@ it('supported css', () => {
&:hover {
background: yellow;
}
@supports (top: max(1px, 0px)) {
padding-left: max(1em, env(safe-area-inset-left, 0px));
}
}
> p {
Expand Down

0 comments on commit 0112bcf

Please sign in to comment.