[6a3a178] | 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 parser = _interopDefault(require('postcss-values-parser'));
|
---|
| 7 | var convertColors = require('@csstools/convert-colors');
|
---|
| 8 |
|
---|
| 9 | function _slicedToArray(arr, i) {
|
---|
| 10 | return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | function _arrayWithHoles(arr) {
|
---|
| 14 | if (Array.isArray(arr)) return arr;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | function _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 |
|
---|
| 43 | function _nonIterableRest() {
|
---|
| 44 | throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | var 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 |
|
---|
| 120 | const hasGrayFunctionRegExp = /(^|[^\w-])gray\(/i;
|
---|
| 121 |
|
---|
| 122 | const hasGrayFunction = decl => hasGrayFunctionRegExp.test(Object(decl).value); // return whether a node matches a specific type
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | const isNumber = node => Object(node).type === 'number';
|
---|
| 126 |
|
---|
| 127 | const isOperator = node => Object(node).type === 'operator';
|
---|
| 128 |
|
---|
| 129 | const isFunction = node => Object(node).type === 'func';
|
---|
| 130 |
|
---|
| 131 | const isCalcRegExp = /^calc$/i;
|
---|
| 132 |
|
---|
| 133 | const isFunctionCalc = node => isFunction(node) && isCalcRegExp.test(node.value);
|
---|
| 134 |
|
---|
| 135 | const isGrayRegExp = /^gray$/i;
|
---|
| 136 |
|
---|
| 137 | const isFunctionGrayWithArgs = node => isFunction(node) && isGrayRegExp.test(node.value) && node.nodes && node.nodes.length;
|
---|
| 138 |
|
---|
| 139 | const isNumberPercentage = node => isNumber(node) && node.unit === '%';
|
---|
| 140 |
|
---|
| 141 | const isNumberUnitless = node => isNumber(node) && node.unit === '';
|
---|
| 142 |
|
---|
| 143 | const isOperatorSlash = node => isOperator(node) && node.value === '/'; // return valid values from a node, otherwise undefined
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | const getNumberUnitless = node => isNumberUnitless(node) ? Number(node.value) : undefined;
|
---|
| 147 |
|
---|
| 148 | const getOperatorSlash = node => isOperatorSlash(node) ? null : undefined;
|
---|
| 149 |
|
---|
| 150 | const 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 |
|
---|
| 153 | const functionalGrayArgs = [getNumberUnitless, getOperatorSlash, getAlpha];
|
---|
| 154 |
|
---|
| 155 | const 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 |
|
---|
| 184 | module.exports = index;
|
---|