Skip to content

Commit 0112bcf

Browse files
committed
Fix issue styled-components#245 with recursive filtering
1 parent e1be59f commit 0112bcf

File tree

4 files changed

+74
-48
lines changed

4 files changed

+74
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.vscode
12
node_modules
23
yarn-error.log

src/styleSheetSerializer.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,19 @@ const filterRules = (classNames) => (rule) =>
6262
includesClassNames(classNames, rule.selectors) &&
6363
rule.declarations.length;
6464

65-
const getAtRules = (ast, filter) =>
66-
ast.stylesheet.rules
67-
.filter((rule) => rule.type === 'media' || rule.type === 'supports')
68-
.reduce((acc, atRule) => {
69-
atRule.rules = atRule.rules.filter(filter);
70-
71-
return acc.concat(atRule);
72-
}, []);
65+
const getAllRules = (rules, classNames) => rules
66+
.filter(
67+
(rule) => rule.type === 'media'
68+
|| rule.type === 'supports'
69+
|| filterRules(classNames)(rule)
70+
)
71+
.map(rule => (rule.type === "rule" ? rule : Object.assign({}, rule, { rules: getAllRules(rule.rules, classNames) })))
72+
.filter(rule => (rule.type === "rule" && rule.declarations.length) || rule.rules.length);
7373

7474
const getStyle = (classNames) => {
7575
const ast = getCSS();
76-
const filter = filterRules(classNames);
77-
const rules = ast.stylesheet.rules.filter(filter);
78-
const atRules = getAtRules(ast, filter);
7976

80-
ast.stylesheet.rules = rules.concat(atRules);
77+
ast.stylesheet.rules = getAllRules(ast.stylesheet.rules, classNames);
8178

8279
return css.stringify(ast);
8380
};

test/__snapshots__/styleSheetSerializer.spec.js.snap

+60-36
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,6 @@ exports[`supported css: mount 1`] = `
838838
background: palevioletred;
839839
}
840840
841-
.c1 > p {
842-
-webkit-text-decoration: underline;
843-
text-decoration: underline;
844-
}
845-
846-
html.test .c0 {
847-
display: none;
848-
}
849-
850841
@media (max-width:600px) {
851842
.c1 {
852843
background: tomato;
@@ -855,6 +846,21 @@ html.test .c0 {
855846
.c1:hover {
856847
background: yellow;
857848
}
849+
850+
@supports (top:max(1px,0px)) {
851+
.c1 {
852+
padding-left: max(1em,env(safe-area-inset-left,0px));
853+
}
854+
}
855+
}
856+
857+
.c1 > p {
858+
-webkit-text-decoration: underline;
859+
text-decoration: underline;
860+
}
861+
862+
html.test .c0 {
863+
display: none;
858864
}
859865
860866
<styled.div>
@@ -878,15 +884,6 @@ exports[`supported css: react-test-renderer 1`] = `
878884
background: palevioletred;
879885
}
880886
881-
.c1 > p {
882-
-webkit-text-decoration: underline;
883-
text-decoration: underline;
884-
}
885-
886-
html.test .c0 {
887-
display: none;
888-
}
889-
890887
@media (max-width:600px) {
891888
.c1 {
892889
background: tomato;
@@ -895,6 +892,21 @@ html.test .c0 {
895892
.c1:hover {
896893
background: yellow;
897894
}
895+
896+
@supports (top:max(1px,0px)) {
897+
.c1 {
898+
padding-left: max(1em,env(safe-area-inset-left,0px));
899+
}
900+
}
901+
}
902+
903+
.c1 > p {
904+
-webkit-text-decoration: underline;
905+
text-decoration: underline;
906+
}
907+
908+
html.test .c0 {
909+
display: none;
898910
}
899911
900912
<div
@@ -916,15 +928,6 @@ exports[`supported css: react-testing-library 1`] = `
916928
background: palevioletred;
917929
}
918930
919-
.c1 > p {
920-
-webkit-text-decoration: underline;
921-
text-decoration: underline;
922-
}
923-
924-
html.test .c0 {
925-
display: none;
926-
}
927-
928931
@media (max-width:600px) {
929932
.c1 {
930933
background: tomato;
@@ -933,6 +936,21 @@ html.test .c0 {
933936
.c1:hover {
934937
background: yellow;
935938
}
939+
940+
@supports (top:max(1px,0px)) {
941+
.c1 {
942+
padding-left: max(1em,env(safe-area-inset-left,0px));
943+
}
944+
}
945+
}
946+
947+
.c1 > p {
948+
-webkit-text-decoration: underline;
949+
text-decoration: underline;
950+
}
951+
952+
html.test .c0 {
953+
display: none;
936954
}
937955
938956
<div
@@ -954,15 +972,6 @@ exports[`supported css: shallow 1`] = `
954972
background: palevioletred;
955973
}
956974
957-
.c1 > p {
958-
-webkit-text-decoration: underline;
959-
text-decoration: underline;
960-
}
961-
962-
html.test .c0 {
963-
display: none;
964-
}
965-
966975
@media (max-width:600px) {
967976
.c1 {
968977
background: tomato;
@@ -971,6 +980,21 @@ html.test .c0 {
971980
.c1:hover {
972981
background: yellow;
973982
}
983+
984+
@supports (top:max(1px,0px)) {
985+
.c1 {
986+
padding-left: max(1em,env(safe-area-inset-left,0px));
987+
}
988+
}
989+
}
990+
991+
.c1 > p {
992+
-webkit-text-decoration: underline;
993+
text-decoration: underline;
994+
}
995+
996+
html.test .c0 {
997+
display: none;
974998
}
975999
9761000
<div

test/styleSheetSerializer.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ it('supported css', () => {
179179
&:hover {
180180
background: yellow;
181181
}
182+
183+
@supports (top: max(1px, 0px)) {
184+
padding-left: max(1em, env(safe-area-inset-left, 0px));
185+
}
182186
}
183187
184188
> p {

0 commit comments

Comments
 (0)