-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmodify-gradient-component-values.ts
160 lines (132 loc) · 4.22 KB
/
modify-gradient-component-values.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
156
157
158
159
160
import type { ComponentValue, FunctionNode } from '@csstools/css-parser-algorithms';
import { gradientNameRegex } from './is-gradient';
import { interpolateColorsInColorStopsList } from './color-stop-list';
import { isCommentNode, isTokenNode, isWhitespaceNode, TokenNode, WhitespaceNode } from '@csstools/css-parser-algorithms';
import { parseColorStops } from './parse-color-stops';
import { TokenType } from '@csstools/css-tokenizer';
const colorSpaceRegex = /^(srgb|srgb-linear|lab|oklab|xyz|xyz-d50|xyz-d65|hsl|hwb|lch|oklch)$/i;
const polarColorSpaceRegex = /^(hsl|hwb|lch|oklch)$/i;
const hueInterpolationMethodRegex = /^(shorter|longer|increasing|decreasing)$/i;
const inKeywordRegex = /^in$/i;
const hueKeywordRegex = /^hue$/i;
export function modifyGradientFunctionComponentValues(gradientFunction: FunctionNode): Array<ComponentValue> | false {
const functionName = gradientFunction.getName();
if (!gradientNameRegex.test(functionName)) {
return false;
}
let colorSpaceName = 'srgb';
let inKeyword: TokenNode | null = null;
let colorSpace: TokenNode | null = null;
let hueInterpolationMethod: TokenNode | null = null;
let hueKeyword: TokenNode | null = null;
let firstComma: TokenNode | null = null;
let remainder: Array<ComponentValue> = [];
{
let i = 0;
let node = gradientFunction.value[i];
{
// Advance to "in" keyword
while (!(isTokenNode(node) && node.value[0] === TokenType.Ident && inKeywordRegex.test(node.value[4].value))) {
if (isTokenNode(node) && node.value[0] === TokenType.Comma) {
// comma before "in" keyword
return false;
}
i++;
node = gradientFunction.value[i];
}
inKeyword = node;
i++;
node = gradientFunction.value[i];
}
while (isCommentNode(node) || isWhitespaceNode(node)) {
i++;
node = gradientFunction.value[i];
}
// color space
if (
isTokenNode(node) &&
node.value[0] === TokenType.Ident &&
colorSpaceRegex.test(node.value[4].value)
) {
if (colorSpace) {
return false;
}
colorSpace = node;
colorSpaceName = node.value[4].value;
i++;
node = gradientFunction.value[i];
}
while (isCommentNode(node) || isWhitespaceNode(node)) {
i++;
node = gradientFunction.value[i];
}
// hue interpolation method
if (
isTokenNode(node) &&
node.value[0] === TokenType.Ident &&
hueInterpolationMethodRegex.test(node.value[4].value) &&
polarColorSpaceRegex.test(colorSpaceName)
) {
if (hueInterpolationMethod || !colorSpace) {
return false;
}
hueInterpolationMethod = node;
i++;
node = gradientFunction.value[i];
}
while (isCommentNode(node) || isWhitespaceNode(node)) {
i++;
node = gradientFunction.value[i];
}
// "hue" keyword
if (
isTokenNode(node) &&
node.value[0] === TokenType.Ident &&
hueKeywordRegex.test(node.value[4].value)
) {
if (hueKeyword || !colorSpace || !hueInterpolationMethod) {
return false;
}
hueKeyword = node;
i++;
node = gradientFunction.value[i];
}
// Find first comma
while (!isTokenNode(node) || node.value[0] !== TokenType.Comma) {
i++;
node = gradientFunction.value[i];
}
firstComma = node;
remainder = gradientFunction.value.slice(i + 1);
}
if (!colorSpace) {
return false;
} else if (hueInterpolationMethod && !hueKeyword) {
return false;
} else if (hueKeyword && !hueInterpolationMethod) {
return false;
}
const colorStops = parseColorStops(remainder);
if (!colorStops) {
return false;
}
const modifiedColorStops = interpolateColorsInColorStopsList(colorStops, colorSpace, hueInterpolationMethod);
if (!modifiedColorStops) {
return false;
}
const beforeColorStops = [
...gradientFunction.value.slice(0, gradientFunction.value.indexOf(inKeyword)),
...gradientFunction.value.slice(gradientFunction.value.indexOf(hueKeyword || colorSpace) + 1, gradientFunction.value.indexOf(firstComma)),
];
const hasMeaningfulPrefix = beforeColorStops.length > 0 && beforeColorStops.some((node) => !isCommentNode(node) && !isWhitespaceNode(node));
if (hasMeaningfulPrefix) {
beforeColorStops.push(
new TokenNode([TokenType.Comma, ',', -1, -1, undefined]),
new WhitespaceNode([[TokenType.Whitespace, ' ', -1, -1, undefined]]),
);
}
return [
...beforeColorStops,
...modifiedColorStops,
];
}