-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathindex.ts
155 lines (129 loc) · 4.1 KB
/
index.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import postcssProgressiveCustomProperties from '@csstools/postcss-progressive-custom-properties';
import type { Declaration, PluginCreator } from 'postcss';
import { color, colorDataFitsRGB_Gamut, serializeP3, serializeRGB, SyntaxFlag } from '@csstools/css-color-parser';
import { hasFallback, hasSupportsAtRuleAncestor } from '@csstools/utilities';
import { isFunctionNode, parseCommaSeparatedListOfComponentValues, replaceComponentValues, stringify } from '@csstools/css-parser-algorithms';
import { tokenize } from '@csstools/css-tokenizer';
type basePluginOptions = {
preserve: boolean,
subFeatures: {
displayP3: boolean
}
};
const LAB_LCH_FUNCTION_REGEX = /\b(?:lab|lch)\(/i;
const LAB_LCH_NAME_REGEX = /^(?:lab|lch)$/i;
/* Transform lab() and lch() functions in CSS. */
const basePlugin: PluginCreator<basePluginOptions> = (opts?: basePluginOptions) => {
return {
postcssPlugin: 'postcss-lab-function',
Declaration(decl: Declaration): void {
const originalValue = decl.value;
if (!(LAB_LCH_FUNCTION_REGEX.test(originalValue))) {
return;
}
if (hasFallback(decl)) {
return;
}
if (hasSupportsAtRuleAncestor(decl, LAB_LCH_FUNCTION_REGEX)) {
return;
}
const tokens = tokenize({ css: originalValue });
const replacedRGB = replaceComponentValues(
parseCommaSeparatedListOfComponentValues(tokens),
(componentValue) => {
if (!isFunctionNode(componentValue) || !LAB_LCH_NAME_REGEX.test(componentValue.getName())) {
return;
}
const colorData = color(componentValue);
if (!colorData) {
return;
}
if (
colorData.syntaxFlags.has(SyntaxFlag.Experimental) ||
colorData.syntaxFlags.has(SyntaxFlag.HasNoneKeywords) ||
colorData.syntaxFlags.has(SyntaxFlag.RelativeColorSyntax)
) {
return;
}
return serializeRGB(colorData);
},
);
const modifiedRGB = stringify(replacedRGB);
if (modifiedRGB === originalValue) {
return;
}
let modifiedP3 = modifiedRGB;
if (opts?.subFeatures.displayP3) {
modifiedP3 = stringify(replaceComponentValues(
parseCommaSeparatedListOfComponentValues(tokens),
(componentValue) => {
if (!isFunctionNode(componentValue) || !LAB_LCH_NAME_REGEX.test(componentValue.getName())) {
return;
}
const colorData = color(componentValue);
if (!colorData) {
return;
}
if (
colorData.syntaxFlags.has(SyntaxFlag.Experimental) ||
colorData.syntaxFlags.has(SyntaxFlag.HasNoneKeywords) ||
colorData.syntaxFlags.has(SyntaxFlag.RelativeColorSyntax)
) {
return;
}
if (colorDataFitsRGB_Gamut(colorData)) {
return serializeRGB(colorData);
}
return serializeP3(colorData);
},
));
}
decl.cloneBefore({ value: modifiedRGB });
if (opts?.subFeatures.displayP3 && modifiedP3 !== modifiedRGB) {
decl.cloneBefore({ value: modifiedP3 });
}
if (!opts?.preserve) {
decl.remove();
}
},
};
};
basePlugin.postcss = true;
/** postcss-lab-function plugin options */
export type pluginOptions = {
/** Preserve the original notation. default: false */
preserve?: boolean,
/** Enable "@csstools/postcss-progressive-custom-properties". default: true */
enableProgressiveCustomProperties?: boolean,
/** Toggle sub features. default: { displayP3: true } */
subFeatures?: {
/** Enable displayP3 fallbacks. default: true */
displayP3?: boolean
}
};
/* Transform lab() and lch() functions in CSS. */
const postcssPlugin: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
const options = Object.assign({
enableProgressiveCustomProperties: true,
preserve: false,
subFeatures: {
displayP3: true,
},
}, opts);
// deep merge
options.subFeatures = Object.assign({
displayP3: true,
}, options.subFeatures);
if (options.enableProgressiveCustomProperties && (options.preserve || options.subFeatures.displayP3)) {
return {
postcssPlugin: 'postcss-lab-function',
plugins: [
postcssProgressiveCustomProperties(),
basePlugin(options),
],
};
}
return basePlugin(options);
};
postcssPlugin.postcss = true;
export default postcssPlugin;