UNPKG

1.36 kBJavaScriptView Raw
1// tooling
2import { list } from 'postcss';
3import getReplacedString from './get-replaced-string';
4import transformNode from './transform-node';
5import setVariable from './set-variable';
6
7// transform @for at-rules
8export default function transformForAtrule(atrule, result, opts) {
9 // params as an array
10 const params = list.space(atrule.params);
11
12 // the statement parts (@for NAME from START to END by INCREMENT)
13 const name = params[0].trim().slice(1);
14 const start = Number(getReplacedString(params[2], atrule, result, opts));
15 const end = Number(getReplacedString(params[4], atrule, result, opts));
16 const increment = 6 in params && Number(getReplacedString(params[6], atrule, result, opts)) || 1;
17 const direction = start <= end ? 1 : -1;
18
19 // for each iteration
20 for (let incrementor = start; incrementor * direction <= end * direction; incrementor += increment * direction) {
21 // set the current iterating variable
22 setVariable(atrule, name, String(incrementor), opts);
23
24 // clone the current at-rule
25 const clone = atrule.clone({
26 parent: atrule.parent,
27 variables: atrule.variables
28 });
29
30 // transform the cloned children
31 transformNode(clone, result, opts);
32
33 // insert the cloned children before the current at-rule
34 atrule.parent.insertBefore(atrule, clone.nodes);
35 }
36
37 // remove the current at-rule
38 atrule.remove();
39}