-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathparse-condition.ts
More file actions
54 lines (47 loc) · 1.25 KB
/
parse-condition.ts
File metadata and controls
54 lines (47 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type {
AtRuleCondition,
ConditionDetails,
ConditionQuery,
MixedCondition,
SelectorCondition,
} from '@pandacss/types'
import { AtRule } from 'postcss'
import { safeParse } from './safe-parse'
function parseAtRule(value: string): AtRuleCondition {
// TODO this creates a new postcss.root for each media query !
const result = safeParse(value)
const rule = result.nodes[0] as AtRule
return {
type: 'at-rule',
name: rule.name,
value: rule.params,
raw: value,
params: rule.params,
}
}
export function parseCondition(condition: ConditionQuery): ConditionDetails | undefined {
if (Array.isArray(condition)) {
return {
type: 'mixed',
raw: condition,
value: condition.map(parseCondition),
} as MixedCondition
}
if (typeof condition === 'function') {
return undefined
}
if (condition.startsWith('@')) {
return parseAtRule(condition)
}
let type: ConditionDetails['type'] | undefined
if (condition.startsWith('&')) {
type = 'self-nesting'
} else if (condition.endsWith(' &')) {
type = 'parent-nesting'
} else if (condition.includes('&')) {
type = 'combinator-nesting'
}
if (type) {
return { type, value: condition, raw: condition } as SelectorCondition
}
}