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