Skip to content

Commit

Permalink
fix: handle basic css media query selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Oct 21, 2019
1 parent 091ca82 commit 168ee25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
37 changes: 18 additions & 19 deletions src/css.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from './xml';
import csstree, {
Atrule,
AtrulePrelude,
CssNode,
Declaration,
DeclarationList,
Expand Down Expand Up @@ -89,7 +90,7 @@ function removeSubsets(nodes: Array<XmlAST | string>): Array<XmlAST | string> {
replace = true;

while (ancestor) {
if (nodes.indexOf(ancestor) > -1) {
if (nodes.includes(ancestor)) {
replace = false;
nodes.splice(idx, 1);
break;
Expand Down Expand Up @@ -276,15 +277,14 @@ function flattenToSelectors(cssAst: CssNode, selectors: FlatSelectorList) {
function filterByMqs(selectors: FlatSelectorList) {
return selectors.filter(({ atrule }) => {
if (atrule === null) {
return ~useMqs.indexOf('');
return true;
}
// @ts-ignore
const { name, expression } = atrule;
return ~useMqs.indexOf(
expression && expression.children.first().type === 'MediaQueryList'
? [name, csstree.generate(expression)].join(' ')
: name,
);
const { name, prelude } = atrule;
const atPrelude = prelude as AtrulePrelude;
const first = atPrelude && atPrelude.children.first();
const mq = first && first.type === 'MediaQueryList';
const query = mq ? csstree.generate(atPrelude) : name;
return useMqs.includes(query);
});
}
// useMqs Array with strings of media queries that should pass (<name> <expression>)
Expand All @@ -297,16 +297,15 @@ const useMqs = ['', 'screen'];
* @return {Array} Filtered selectors that match the passed pseudo-elements and/or -classes
*/
function filterByPseudos(selectors: FlatSelectorList) {
return selectors.filter(
({ pseudos }) =>
~usePseudos.indexOf(
csstree.generate({
type: 'Selector',
children: new List<CssNode>().fromArray(
pseudos.map(pseudo => pseudo.item.data),
),
}),
),
return selectors.filter(({ pseudos }) =>
usePseudos.includes(
csstree.generate({
type: 'Selector',
children: new List<CssNode>().fromArray(
pseudos.map(pseudo => pseudo.item.data),
),
}),
),
);
}
// usePseudos Array with strings of single or sequence of pseudo-elements and/or -classes that should pass
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2017",
"lib": ["es6"],
"lib": ["es6", "es2016"],
"jsx": "react-native",
"strict": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 168ee25

Please sign in to comment.