source: trip-planner-front/node_modules/postcss-color-gray/index.cjs.js@ 8d391a1

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

initial commit

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