mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-05 22:56:11 +00:00
fix: handle basic css media query selectors
This commit is contained in:
37
src/css.tsx
37
src/css.tsx
@@ -13,6 +13,7 @@ import {
|
||||
} from './xml';
|
||||
import csstree, {
|
||||
Atrule,
|
||||
AtrulePrelude,
|
||||
CssNode,
|
||||
Declaration,
|
||||
DeclarationList,
|
||||
@@ -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;
|
||||
@@ -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>)
|
||||
@@ -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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2017",
|
||||
"lib": ["es6"],
|
||||
"lib": ["es6", "es2016"],
|
||||
"jsx": "react-native",
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
|
||||
Reference in New Issue
Block a user