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 issue #245 with recursive filtering #354

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -59,22 +59,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
29 changes: 12 additions & 17 deletions src/toHaveStyleRule.js
Expand Up @@ -36,17 +36,6 @@ const getClassNames = (received) => {

const hasAtRule = (options) => Object.keys(options).some((option) => ['media', 'supports'].includes(option));

const getAtRules = (ast, options) => {
return Object.keys(options)
.map((option) =>
ast.stylesheet.rules
.filter((rule) => rule.type === option && rule[option] === options[option].replace(/:\s/g, ":"))
.map((rule) => rule.rules)
.reduce((acc, rules) => acc.concat(rules), [])
)
.reduce((acc, rules) => acc.concat(rules), []);
};

const normalizeQuotations = (input) => input.replace(/['"]/g, '"');

const getModifiedClassName = (className, staticClassName, modifier = '') => {
Expand Down Expand Up @@ -82,14 +71,20 @@ const hasClassNames = (classNames, selectors, options) => {
);
};

const getRules = (ast, classNames, options) => {
const rules = (hasAtRule(options) ? getAtRules(ast, options) : ast.stylesheet.rules).map((rule) => ({
const getRules = (rules, classNames, options) =>
rules.map((rule) => ({
...rule,
selectors: Array.isArray(rule.selectors) ? rule.selectors.map(normalizeQuotations) : rule.selectors,
}));
}))
.flatMap((rule) => {
if (!hasAtRule(options)) {
return rule.type === 'rule' && hasClassNames(classNames, rule.selectors, options) ? [rule] : [];
}

return rules.filter((rule) => rule.type === 'rule' && hasClassNames(classNames, rule.selectors, options));
};
return ['media', 'supports'].includes(rule.type) && options[rule.type] && rule[rule.type] === options[rule.type].replace(/:\s/g, ':')
? getRules(rule.rules, classNames, Object.fromEntries(Object.entries(options).filter(([key]) => key !== rule.type)))
: [];
});

const handleMissingRules = (options) => ({
pass: false,
Expand All @@ -116,7 +111,7 @@ function toHaveStyleRule(component, property, expected, options = {}) {
const classNames = getClassNames(component);
const ast = getCSS();
const normalizedOptions = normalizeOptions(options);
const rules = getRules(ast, classNames, normalizedOptions);
const rules = getRules(ast.stylesheet.rules, classNames, normalizedOptions);

if (!rules.length) {
return handleMissingRules(normalizedOptions);
Expand Down
96 changes: 60 additions & 36 deletions test/__snapshots__/styleSheetSerializer.spec.js.snap
Expand Up @@ -1008,15 +1008,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 @@ -1025,6 +1016,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 @@ -1048,15 +1054,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 @@ -1065,6 +1062,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 @@ -1086,15 +1098,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 @@ -1103,6 +1106,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 @@ -1124,15 +1142,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 @@ -1141,6 +1150,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 @@ -180,6 +180,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
36 changes: 36 additions & 0 deletions test/toHaveStyleRule.spec.js
Expand Up @@ -252,6 +252,42 @@ it('at rules', () => {
});
});

it('nested at rules', () => {
const Container = styled.div`
@media (min-width: 320px) {
top: 0px;
@supports (top:env(safe-area-inset-top,0px)) {
top: env(safe-area-inset-top,0px);
}
}

@supports (bottom:env(safe-area-inset-bottom,0px)) {
bottom: 0px;
@media (min-width: 320px) {
bottom: env(safe-area-inset-bottom,0px);
}
}
`;

toHaveStyleRule(<Container />, "top", "0px", {
media: "(min-width: 320px)",
});

toHaveStyleRule(<Container />, "top", "env(safe-area-inset-top,0px)", {
media: "(min-width: 320px)",
supports: "(top:env(safe-area-inset-top,0px))",
});

toHaveStyleRule(<Container />, "bottom", "0px", {
supports: "(bottom:env(safe-area-inset-bottom,0px))",
});

toHaveStyleRule(<Container />, "bottom", "env(safe-area-inset-bottom,0px)", {
media: "(min-width: 320px)",
supports: "(bottom:env(safe-area-inset-bottom,0px))",
});
});

it('selector modifiers', () => {
const Link = styled.a`
color: white;
Expand Down