source: trip-planner-front/node_modules/postcss-color-gray/index.es.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.8 KB
Line 
1import postcss from 'postcss';
2import parser from 'postcss-values-parser';
3import { lab2rgb } from '@csstools/convert-colors';
4
5function _slicedToArray(arr, i) {
6 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
7}
8
9function _arrayWithHoles(arr) {
10 if (Array.isArray(arr)) return arr;
11}
12
13function _iterableToArrayLimit(arr, i) {
14 var _arr = [];
15 var _n = true;
16 var _d = false;
17 var _e = undefined;
18
19 try {
20 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
21 _arr.push(_s.value);
22
23 if (i && _arr.length === i) break;
24 }
25 } catch (err) {
26 _d = true;
27 _e = err;
28 } finally {
29 try {
30 if (!_n && _i["return"] != null) _i["return"]();
31 } finally {
32 if (_d) throw _e;
33 }
34 }
35
36 return _arr;
37}
38
39function _nonIterableRest() {
40 throw new TypeError("Invalid attempt to destructure non-iterable instance");
41}
42
43var index = postcss.plugin('postcss-color-gray', opts => root => {
44 // walk all declarations likely containing a gray() function
45 root.walkDecls(decl => {
46 if (hasGrayFunction(decl)) {
47 const originalValue = decl.value; // parse the declaration value
48
49 const ast = parser(originalValue).parse(); // walk every node in the value that contains a gray() function
50
51 ast.walk(node => {
52 const _getFunctionGrayArgs = getFunctionGrayArgs(node),
53 _getFunctionGrayArgs2 = _slicedToArray(_getFunctionGrayArgs, 2),
54 lightness = _getFunctionGrayArgs2[0],
55 alpha = _getFunctionGrayArgs2[1];
56
57 if (lightness !== undefined) {
58 // rename the gray() function to rgb()
59 node.value = 'rgb'; // convert the lab gray lightness into rgb
60
61 const _lab2rgb$map = lab2rgb(lightness, 0, 0).map(channel => Math.max(Math.min(Math.round(channel * 2.55), 255), 0)),
62 _lab2rgb$map2 = _slicedToArray(_lab2rgb$map, 3),
63 r = _lab2rgb$map2[0],
64 g = _lab2rgb$map2[1],
65 b = _lab2rgb$map2[2]; // preserve the slash nodes within rgb()
66
67
68 const openingSlash = node.first;
69 const closingSlash = node.last;
70 node.removeAll() // replace the contents of rgb with `(r,g,b`
71 .append(openingSlash).append(parser.number({
72 value: r
73 })).append(parser.comma({
74 value: ','
75 })).append(parser.number({
76 value: g
77 })).append(parser.comma({
78 value: ','
79 })).append(parser.number({
80 value: b
81 })); // if an alpha channel was defined
82
83 if (alpha < 1) {
84 // rename the rgb() function to rgba()
85 node.value += 'a';
86 node // append the contents of rgba with `,a`
87 .append(parser.comma({
88 value: ','
89 })).append(parser.number({
90 value: alpha
91 }));
92 } // append the contents of rgb/rgba with `)`
93
94
95 node.append(closingSlash);
96 }
97 });
98 const modifiedValue = ast.toString(); // if the modified value has changed from the original value
99
100 if (originalValue !== modifiedValue) {
101 // if the original gray() color is to be preserved
102 if (Object(opts).preserve) {
103 // insert the declaration value with the fallback before the current declaration
104 decl.cloneBefore({
105 value: modifiedValue
106 });
107 } else {
108 // otherwise, overwrite the declaration value with the fallback
109 decl.value = modifiedValue;
110 }
111 }
112 }
113 });
114}); // return whether a string contains a gray() function
115
116const hasGrayFunctionRegExp = /(^|[^\w-])gray\(/i;
117
118const hasGrayFunction = decl => hasGrayFunctionRegExp.test(Object(decl).value); // return whether a node matches a specific type
119
120
121const isNumber = node => Object(node).type === 'number';
122
123const isOperator = node => Object(node).type === 'operator';
124
125const isFunction = node => Object(node).type === 'func';
126
127const isCalcRegExp = /^calc$/i;
128
129const isFunctionCalc = node => isFunction(node) && isCalcRegExp.test(node.value);
130
131const isGrayRegExp = /^gray$/i;
132
133const isFunctionGrayWithArgs = node => isFunction(node) && isGrayRegExp.test(node.value) && node.nodes && node.nodes.length;
134
135const isNumberPercentage = node => isNumber(node) && node.unit === '%';
136
137const isNumberUnitless = node => isNumber(node) && node.unit === '';
138
139const isOperatorSlash = node => isOperator(node) && node.value === '/'; // return valid values from a node, otherwise undefined
140
141
142const getNumberUnitless = node => isNumberUnitless(node) ? Number(node.value) : undefined;
143
144const getOperatorSlash = node => isOperatorSlash(node) ? null : undefined;
145
146const getAlpha = node => isFunctionCalc(node) ? String(node) : isNumberUnitless(node) ? Number(node.value) : isNumberPercentage(node) ? Number(node.value) / 100 : undefined; // return valid arguments from a gray() function
147
148
149const functionalGrayArgs = [getNumberUnitless, getOperatorSlash, getAlpha];
150
151const getFunctionGrayArgs = node => {
152 const validArgs = []; // if the node is a gray() function with arguments
153
154 if (isFunctionGrayWithArgs(node)) {
155 // get all the gray() function arguments between `(` and `)`
156 const nodes = node.nodes.slice(1, -1); // validate each argument
157
158 for (const index in nodes) {
159 const arg = typeof functionalGrayArgs[index] === 'function' ? functionalGrayArgs[index](nodes[index]) : undefined; // if the argument was validated
160
161 if (arg !== undefined) {
162 // push any non-null argument to the valid arguments array
163 if (arg !== null) {
164 validArgs.push(arg);
165 }
166 } else {
167 // otherwise, return an empty array
168 return [];
169 }
170 } // return the valid arguments array
171
172
173 return validArgs;
174 } else {
175 // otherwise, return an empty array
176 return [];
177 }
178};
179
180export default index;
Note: See TracBrowser for help on using the repository browser.