1 |
|
2 | import { list } from 'postcss';
|
3 | import transformNode from './transform-node';
|
4 | import getReplacedString from './get-replaced-string';
|
5 |
|
6 |
|
7 | export default function transformIfAtrule(atrule, result, opts) {
|
8 |
|
9 | const params = list.space(atrule.params);
|
10 |
|
11 |
|
12 | const left = getInterprettedString(getReplacedString(params[0] || '', atrule, result, opts));
|
13 | const operator = params[1];
|
14 | const right = getInterprettedString(getReplacedString(params[2] || '', atrule, result, opts));
|
15 |
|
16 |
|
17 | const next = Object(atrule.next());
|
18 |
|
19 |
|
20 | if (
|
21 | !operator && left ||
|
22 | operator === '==' && left === right ||
|
23 | operator === '!=' && left !== right ||
|
24 | operator === '<' && left < right ||
|
25 | operator === '<=' && left <= right ||
|
26 | operator === '>' && left > right ||
|
27 | operator === '>=' && left >= right
|
28 | ) {
|
29 |
|
30 | transformNode(atrule, result, opts);
|
31 |
|
32 |
|
33 | atrule.parent.insertBefore(atrule, atrule.nodes);
|
34 |
|
35 |
|
36 | if (isElseAtrule(next)) {
|
37 | next.remove();
|
38 | }
|
39 | } else if (isElseAtrule(next)) {
|
40 |
|
41 | transformNode(next, result, opts);
|
42 |
|
43 |
|
44 | atrule.parent.insertBefore(atrule, next.nodes);
|
45 |
|
46 |
|
47 | next.remove();
|
48 | }
|
49 |
|
50 |
|
51 | atrule.remove();
|
52 | }
|
53 |
|
54 |
|
55 | const isElseAtrule = node => 'atrule' === node.type && 'else' === node.name;
|
56 |
|
57 |
|
58 | const getInterprettedString = value => 'true' === value
|
59 | ? true
|
60 | : 'false' === value
|
61 | ? false
|
62 | : isNaN(value)
|
63 | ? value
|
64 | : Number(value);
|