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 Feb 3, 2021
1 parent 5448282 commit 0ee1968
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.vscode
node_modules
24 changes: 10 additions & 14 deletions src/styleSheetSerializer.js
Expand Up @@ -52,22 +52,18 @@ const includesClassNames = (classNames, selectors) =>
const filterRules = classNames => rule =>
rule.type === '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'
|| (rule.type === 'rule' && includesClassNames(classNames, rule.selectors) && rule.declarations.length)
)
.map(rule => (rule.type === "rule" ? rule : Object.assign({}, rule, { rules: getAllRules(rule.rules, classNames) })));

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 All @@ -93,8 +89,8 @@ const replaceClassNames = (result, classNames, style) =>
.reduce((acc, className, index) => acc.replace(new RegExp(className, 'g'), `c${index++}`), result);

const stripUnreferencedClassNames = (result, classNames) =>
classNames
.reduce((acc, className) => acc.replace(new RegExp(`${className}\\s?`,'g'), ''), result);
classNames
.reduce((acc, className) => acc.replace(new RegExp(`${className}\\s?`, 'g'), ''), result);

const replaceHashes = (result, hashes) =>
hashes.reduce(
Expand Down
96 changes: 60 additions & 36 deletions test/__snapshots__/styleSheetSerializer.spec.js.snap
Expand Up @@ -727,15 +727,6 @@ exports[`supported css: mount 1`] = `
background: palevioletred;
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c0 {
background: tomato;
Expand All @@ -744,6 +735,21 @@ html.test .c0 {
.c0:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c0 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<styled.div>
Expand All @@ -767,15 +773,6 @@ exports[`supported css: react-test-renderer 1`] = `
background: palevioletred;
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c0 {
background: tomato;
Expand All @@ -784,6 +781,21 @@ html.test .c0 {
.c0:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c0 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<div
Expand All @@ -805,15 +817,6 @@ exports[`supported css: react-testing-library 1`] = `
background: palevioletred;
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c0 {
background: tomato;
Expand All @@ -822,6 +825,21 @@ html.test .c0 {
.c0:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c0 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
<div
Expand All @@ -843,15 +861,6 @@ exports[`supported css: shallow 1`] = `
background: palevioletred;
}
.c0 > p {
-webkit-text-decoration: underline;
text-decoration: underline;
}
html.test .c0 {
display: none;
}
@media (max-width:600px) {
.c0 {
background: tomato;
Expand All @@ -860,6 +869,21 @@ html.test .c0 {
.c0:hover {
background: yellow;
}
@supports (top:max(1px,0px)) {
.c0 {
padding-left: max(1em,env(safe-area-inset-left,0px));
}
}
}
.c0 > 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 0ee1968

Please sign in to comment.