1 | import postcss from 'postcss';
|
---|
2 | import valueParser from 'postcss-values-parser';
|
---|
3 |
|
---|
4 | var index = postcss.plugin('postcss-double-position-gradients', opts => {
|
---|
5 | const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : true;
|
---|
6 | return root => {
|
---|
7 | // walk every declaration
|
---|
8 | root.walkDecls(decl => {
|
---|
9 | const originalValue = decl.value; // if the declaration value contains a gradient
|
---|
10 |
|
---|
11 | if (gradientFunctionRegExp.test(originalValue)) {
|
---|
12 | const ast = valueParser(originalValue).parse(); // walk every function in the declaration value
|
---|
13 |
|
---|
14 | ast.walkFunctionNodes(fn => {
|
---|
15 | // if the function is a gradient
|
---|
16 | if (gradientFunctionNameRegExp.test(fn.value)) {
|
---|
17 | const nodes = fn.nodes.slice(1, -1); // walk every argument to the function
|
---|
18 |
|
---|
19 | nodes.forEach((node, index) => {
|
---|
20 | const node1back = Object(nodes[index - 1]);
|
---|
21 | const node2back = Object(nodes[index - 2]);
|
---|
22 | const isDoublePositionLength = node2back.type && node1back.type === 'number' && node.type === 'number'; // if the argument concludes a double-position gradient
|
---|
23 |
|
---|
24 | if (isDoublePositionLength) {
|
---|
25 | // insert the fallback colors
|
---|
26 | const color = node2back.clone();
|
---|
27 | const comma = valueParser.comma({
|
---|
28 | value: ',',
|
---|
29 | raws: {
|
---|
30 | after: ' '
|
---|
31 | }
|
---|
32 | });
|
---|
33 | fn.insertBefore(node, comma);
|
---|
34 | fn.insertBefore(node, color);
|
---|
35 | }
|
---|
36 | });
|
---|
37 | }
|
---|
38 | });
|
---|
39 | const modifiedValue = ast.toString(); // if the value has changed due to double-position gradients
|
---|
40 |
|
---|
41 | if (originalValue !== modifiedValue) {
|
---|
42 | // add the fallback value
|
---|
43 | decl.cloneBefore({
|
---|
44 | value: modifiedValue
|
---|
45 | }); // conditionally remove the double-position gradient
|
---|
46 |
|
---|
47 | if (!preserve) {
|
---|
48 | decl.remove();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | });
|
---|
53 | };
|
---|
54 | });
|
---|
55 | const gradientFunctionRegExp = /(repeating-)?(conic|linear|radial)-gradient\([\W\w]*\)/i;
|
---|
56 | const gradientFunctionNameRegExp = /^(repeating-)?(conic|linear|radial)-gradient$/i;
|
---|
57 |
|
---|
58 | export default index;
|
---|
59 | //# sourceMappingURL=index.es.mjs.map
|
---|