1 |
|
2 | import { list } from 'postcss';
|
3 | import getReplacedString from './get-replaced-string';
|
4 | import transformNode from './transform-node';
|
5 | import setVariable from './set-variable';
|
6 |
|
7 |
|
8 | export default function transformForAtrule(rule, opts) {
|
9 |
|
10 | if (opts.transform.includes('@for')) {
|
11 |
|
12 | const { varname, start, end, increment } = getForOpts(rule, opts);
|
13 | const direction = start <= end ? 1 : -1;
|
14 | const replacements = [];
|
15 |
|
16 |
|
17 | for (let incrementor = start; incrementor * direction <= end * direction; incrementor += increment * direction) {
|
18 |
|
19 | setVariable(rule, varname, incrementor, opts);
|
20 |
|
21 |
|
22 | const clone = rule.clone({
|
23 | parent: rule.parent,
|
24 | variables: rule.variables
|
25 | });
|
26 |
|
27 |
|
28 | transformNode(clone, opts);
|
29 |
|
30 |
|
31 | replacements.push(...clone.nodes);
|
32 | }
|
33 |
|
34 |
|
35 | rule.parent.insertBefore(rule, replacements);
|
36 |
|
37 | rule.remove();
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 | const getForOpts = (node, opts) => {
|
43 | const params = list.space(node.params);
|
44 | const varname = params[0].trim().slice(1);
|
45 | const start = Number(getReplacedString(params[2], node, opts));
|
46 | const end = Number(getReplacedString(params[4], node, opts));
|
47 | const increment = 6 in params && Number(getReplacedString(params[6], node, opts)) || 1;
|
48 |
|
49 | return { varname, start, end, increment };
|
50 | };
|