-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
137 lines (117 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const {parse} = require("postcss-values-parser");
const {lab2rgb} = require("@csstools/convert-colors");
const Numeric = require("postcss-values-parser/lib/nodes/Numeric");
const Punctuation = require("postcss-values-parser/lib/nodes/Punctuation");
/**
* @param {{preserve?: boolean}} opts
* @returns {import('postcss').Plugin}
*/
module.exports = function creator(opts) {
const preserve = Boolean(Object(opts).preserve);
return {
postcssPlugin: 'postcss-color-gray',
// walk all declarations likely containing a gray() function
Declaration(decl) {
if (hasGrayFunction(decl)) {
const { value: originalValue } = decl;
// parse the declaration value
const ast = parse(originalValue);
// walk every node in the value that contains a gray() function
ast.walkFuncs(node => {
const [lightness, alpha] = getFunctionGrayArgs(node);
if (lightness !== undefined) {
// rename the gray() function to rgb()
node.name = 'rgb';
// convert the lab gray lightness into rgb
const [r, g, b] = lab2rgb(lightness, 0, 0).map(
channel => Math.max(Math.min(Math.round(channel * 2.55), 255), 0)
);
node.removeAll()
// replace the contents of rgb with `r,g,b`
.append(new Numeric({ value: r }))
.append(new Punctuation({ value: ',' }))
.append(new Numeric({ value: g }))
.append(new Punctuation({ value: ',' }))
.append(new Numeric({ value: b }))
// if an alpha channel was defined
if (alpha < 1) {
// rename the rgb() function to rgba()
node.name += 'a';
node
// append the contents of rgba with `,a`
.append(new Punctuation({ value: ',' }))
.append(new Numeric({ value: alpha }));
}
}
});
const modifiedValue = ast.toString();
// if the modified value has changed from the original value
if (originalValue !== modifiedValue) {
// if the original gray() color is to be preserved
if (preserve) {
// insert the declaration value with the fallback before the current declaration
decl.cloneBefore({
value: modifiedValue
});
} else {
// otherwise, overwrite the declaration value with the fallback
decl.value = modifiedValue;
}
}
}
}
}
}
module.exports.postcss = true;
// return whether a string contains a gray() function
const hasGrayFunctionRegExp = /(^|[^\w-])gray\(/i;
const hasGrayFunction = decl => hasGrayFunctionRegExp.test(Object(decl).value);
// return whether a node matches a specific type
const isNumber = node => Object(node).type === 'numeric';
const isOperator = node => Object(node).type === 'operator';
const isFunction = node => Object(node).type === 'func';
const isFunctionCalc = node => isFunction(node) && node.name === 'calc';
const isNumberPercentage = node => isNumber(node) && node.unit === '%';
const isNumberUnitless = node => isNumber(node) && node.unit === '';
const isOperatorSlash = node => isOperator(node) && node.value === '/';
// return valid values from a node, otherwise undefined
const getNumberUnitless = node => isNumberUnitless(node) ? Number(node.value) : undefined;
const getOperatorSlash = node => isOperatorSlash(node) ? null : undefined;
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
const functionalGrayArgs = [getNumberUnitless, getOperatorSlash, getAlpha];
const getFunctionGrayArgs = node => {
const validArgs = [];
// if the node is a gray() function with arguments
if (node.name === 'gray' && node.nodes && node.nodes.length) {
// get all the gray() function arguments between `(` and `)`
const nodes = node.nodes;
// validate each argument
for (const index in nodes) {
const arg = typeof functionalGrayArgs[index] === 'function'
? functionalGrayArgs[index](nodes[index])
: undefined;
// if the argument was validated
if (arg !== undefined) {
// push any non-null argument to the valid arguments array
if (arg !== null) {
validArgs.push(arg);
}
} else {
// otherwise, return an empty array
return [];
}
}
// return the valid arguments array
return validArgs;
} else {
// otherwise, return an empty array
return [];
}
}